1 
2 /*!
3  ***********************************************************************
4  *  \file
5  *      mbuffer.c
6  *
7  *  \brief
8  *      Frame buffer functions
9  *
10  *  \author
11  *      Main contributors (see contributors.h for copyright, address and affiliation details)
12  *      - Karsten Suehring
13  *      - Alexis Tourapis                 <alexismt@ieee.org>
14  *      - Jill Boyce                      <jill.boyce@thomson.net>
15  *      - Saurav K Bandyopadhyay          <saurav@ieee.org>
16  *      - Zhenyu Wu                       <Zhenyu.Wu@thomson.net
17  *      - Purvin Pandit                   <Purvin.Pandit@thomson.net>
18  *      - Yuwen He                        <yhe@dolby.com>
19  ***********************************************************************
20  */
21 
22 #include <limits.h>
23 
24 #include "global.h"
25 #include "erc_api.h"
26 #include "header.h"
27 #include "image.h"
28 #include "mbuffer.h"
29 #include "mbuffer_common.h"
30 #include "memalloc.h"
31 #include "output.h"
32 #include "mbuffer_mvc.h"
33 #include "fast_memory.h"
34 #include "input.h"
35 
36 static void insert_picture_in_dpb    (VideoParameters *p_Vid, FrameStore* fs, StorablePicture* p);
37 static int output_one_frame_from_dpb (DecodedPictureBuffer *p_Dpb);
38 static void gen_field_ref_ids        (VideoParameters *p_Vid, StorablePicture *p);
39 
40 #define MAX_LIST_SIZE 33
41 
42 /*!
43  ************************************************************************
44  * \brief
45  *    Print out list of pictures in DPB. Used for debug purposes.
46  ************************************************************************
47  */
dump_dpb(DecodedPictureBuffer * p_Dpb)48 void dump_dpb(DecodedPictureBuffer *p_Dpb)
49 {
50 #if DUMP_DPB
51   unsigned i;
52 
53   for (i=0; i<p_Dpb->used_size;i++)
54   {
55     printf("(");
56     printf("fn=%d  ", p_Dpb->fs[i]->frame_num);
57     if (p_Dpb->fs[i]->is_used & 1)
58     {
59       if (p_Dpb->fs[i]->top_field)
60         printf("T: poc=%d  ", p_Dpb->fs[i]->top_field->poc);
61       else
62         printf("T: poc=%d  ", p_Dpb->fs[i]->frame->top_poc);
63     }
64     if (p_Dpb->fs[i]->is_used & 2)
65     {
66       if (p_Dpb->fs[i]->bottom_field)
67         printf("B: poc=%d  ", p_Dpb->fs[i]->bottom_field->poc);
68       else
69         printf("B: poc=%d  ", p_Dpb->fs[i]->frame->bottom_poc);
70     }
71     if (p_Dpb->fs[i]->is_used == 3)
72       printf("F: poc=%d  ", p_Dpb->fs[i]->frame->poc);
73     printf("G: poc=%d)  ", p_Dpb->fs[i]->poc);
74     if (p_Dpb->fs[i]->is_reference) printf ("ref (%d) ", p_Dpb->fs[i]->is_reference);
75     if (p_Dpb->fs[i]->is_long_term) printf ("lt_ref (%d) ", p_Dpb->fs[i]->is_reference);
76     if (p_Dpb->fs[i]->is_output) printf ("out  ");
77     if (p_Dpb->fs[i]->is_used == 3)
78     {
79       if (p_Dpb->fs[i]->frame->non_existing) printf ("ne  ");
80     }
81 #if (MVC_EXTENSION_ENABLE)
82     if (p_Dpb->fs[i]->is_reference)
83       printf ("view_id (%d) ", p_Dpb->fs[i]->view_id);
84 #endif
85     printf ("\n");
86   }
87 #endif
88 }
89 
90 /*!
91  ************************************************************************
92  * \brief
93  *    Returns the size of the dpb depending on level and picture size
94  *
95  *
96  ************************************************************************
97  */
getDpbSize(VideoParameters * p_Vid,seq_parameter_set_rbsp_t * active_sps)98 int getDpbSize(VideoParameters *p_Vid, seq_parameter_set_rbsp_t *active_sps)
99 {
100   int pic_size_mb = (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);
101 
102   int size = 0;
103 
104   switch (active_sps->level_idc)
105   {
106   case 0:
107     // if there is no level defined, we expect experimental usage and return a DPB size of 16
108     return 16;
109   case 9:
110     size = 396;
111     break;
112   case 10:
113     size = 396;
114     break;
115   case 11:
116     if (!is_FREXT_profile(active_sps->profile_idc) && (active_sps->constrained_set3_flag == 1))
117       size = 396;
118     else
119       size = 900;
120     break;
121   case 12:
122     size = 2376;
123     break;
124   case 13:
125     size = 2376;
126     break;
127   case 20:
128     size = 2376;
129     break;
130   case 21:
131     size = 4752;
132     break;
133   case 22:
134     size = 8100;
135     break;
136   case 30:
137     size = 8100;
138     break;
139   case 31:
140     size = 18000;
141     break;
142   case 32:
143     size = 20480;
144     break;
145   case 40:
146     size = 32768;
147     break;
148   case 41:
149     size = 32768;
150     break;
151   case 42:
152     size = 34816;
153     break;
154   case 50:
155     size = 110400;
156     break;
157   case 51:
158     size = 184320;
159     break;
160   case 52:
161     size = 184320;
162     break;
163   case 60:
164   case 61:
165   case 62:
166     size = 696320;
167     break;
168   default:
169     error ("undefined level", 500);
170     break;
171   }
172 
173   size /= pic_size_mb;
174 #if MVC_EXTENSION_ENABLE
175   if(p_Vid->profile_idc == MVC_HIGH || p_Vid->profile_idc == STEREO_HIGH)
176   {
177     int num_views = p_Vid->active_subset_sps->num_views_minus1+1;
178     size = imin(2*size, imax(1, RoundLog2(num_views))*16)/num_views;
179   }
180   else
181 #endif
182   {
183     size = imin( size, 16);
184   }
185 
186   if (active_sps->vui_parameters_present_flag && active_sps->vui_seq_parameters.bitstream_restriction_flag)
187   {
188     int size_vui;
189     if ((int)active_sps->vui_seq_parameters.max_dec_frame_buffering > size)
190     {
191       error ("max_dec_frame_buffering larger than MaxDpbSize", 500);
192     }
193     size_vui = imax (1, active_sps->vui_seq_parameters.max_dec_frame_buffering);
194 #ifdef _DEBUG
195     if(size_vui < size)
196     {
197       printf("Warning: max_dec_frame_buffering(%d) is less than DPB size(%d) calculated from Profile/Level.\n", size_vui, size);
198     }
199 #endif
200     size = size_vui;
201   }
202 
203   return size;
204 }
205 
206 /*!
207  ************************************************************************
208  * \brief
209  *    Check then number of frames marked "used for reference" and break
210  *    if maximum is exceeded
211  *
212  ************************************************************************
213  */
check_num_ref(DecodedPictureBuffer * p_Dpb)214 void check_num_ref(DecodedPictureBuffer *p_Dpb)
215 {
216   if ((int)(p_Dpb->ltref_frames_in_buffer +  p_Dpb->ref_frames_in_buffer ) > imax(1, p_Dpb->num_ref_frames))
217   {
218     error ("Max. number of reference frames exceeded. Invalid stream.", 500);
219   }
220 }
221 
222 
223 /*!
224  ************************************************************************
225  * \brief
226  *    Allocate memory for decoded picture buffer and initialize with sane values.
227  *
228  ************************************************************************
229  */
init_dpb(VideoParameters * p_Vid,DecodedPictureBuffer * p_Dpb,int type)230 void init_dpb(VideoParameters *p_Vid, DecodedPictureBuffer *p_Dpb, int type)
231 {
232   unsigned i;
233   seq_parameter_set_rbsp_t *active_sps = p_Vid->active_sps;
234 
235   p_Dpb->p_Vid = p_Vid;
236   if (p_Dpb->init_done)
237   {
238     free_dpb(p_Dpb);
239   }
240 
241   p_Dpb->size = getDpbSize(p_Vid, active_sps) + p_Vid->p_Inp->dpb_plus[type==2? 1: 0];
242   p_Dpb->num_ref_frames = active_sps->num_ref_frames;
243 
244 #if (MVC_EXTENSION_ENABLE)
245   if ((unsigned int)active_sps->max_dec_frame_buffering < active_sps->num_ref_frames)
246 #else
247   if (p_Dpb->size < active_sps->num_ref_frames)
248 #endif
249   {
250     error ("DPB size at specified level is smaller than the specified number of reference frames. This is not allowed.\n", 1000);
251   }
252 
253   p_Dpb->used_size = 0;
254   p_Dpb->last_picture = NULL;
255 
256   p_Dpb->ref_frames_in_buffer = 0;
257   p_Dpb->ltref_frames_in_buffer = 0;
258 
259   p_Dpb->fs = calloc(p_Dpb->size, sizeof (FrameStore*));
260   if (NULL==p_Dpb->fs)
261     no_mem_exit("init_dpb: p_Dpb->fs");
262 
263   p_Dpb->fs_ref = calloc(p_Dpb->size, sizeof (FrameStore*));
264   if (NULL==p_Dpb->fs_ref)
265     no_mem_exit("init_dpb: p_Dpb->fs_ref");
266 
267   p_Dpb->fs_ltref = calloc(p_Dpb->size, sizeof (FrameStore*));
268   if (NULL==p_Dpb->fs_ltref)
269     no_mem_exit("init_dpb: p_Dpb->fs_ltref");
270 
271 #if (MVC_EXTENSION_ENABLE)
272   p_Dpb->fs_ilref = calloc(1, sizeof (FrameStore*));
273   if (NULL==p_Dpb->fs_ilref)
274     no_mem_exit("init_dpb: p_Dpb->fs_ilref");
275 #endif
276 
277   for (i = 0; i < p_Dpb->size; i++)
278   {
279     p_Dpb->fs[i]       = alloc_frame_store();
280     p_Dpb->fs_ref[i]   = NULL;
281     p_Dpb->fs_ltref[i] = NULL;
282     p_Dpb->fs[i]->layer_id = MVC_INIT_VIEW_ID;
283 #if (MVC_EXTENSION_ENABLE)
284     p_Dpb->fs[i]->view_id = MVC_INIT_VIEW_ID;
285     p_Dpb->fs[i]->inter_view_flag[0] = p_Dpb->fs[i]->inter_view_flag[1] = 0;
286     p_Dpb->fs[i]->anchor_pic_flag[0] = p_Dpb->fs[i]->anchor_pic_flag[1] = 0;
287 #endif
288   }
289 #if (MVC_EXTENSION_ENABLE)
290   if (type == 2)
291   {
292     p_Dpb->fs_ilref[0] = alloc_frame_store();
293     // These may need some cleanups
294     p_Dpb->fs_ilref[0]->view_id = MVC_INIT_VIEW_ID;
295     p_Dpb->fs_ilref[0]->inter_view_flag[0] = p_Dpb->fs_ilref[0]->inter_view_flag[1] = 0;
296     p_Dpb->fs_ilref[0]->anchor_pic_flag[0] = p_Dpb->fs_ilref[0]->anchor_pic_flag[1] = 0;
297     // given that this is in a different buffer, do we even need proc_flag anymore?
298   }
299   else
300     p_Dpb->fs_ilref[0] = NULL;
301 #endif
302 
303   /*
304   for (i = 0; i < 6; i++)
305   {
306   currSlice->listX[i] = calloc(MAX_LIST_SIZE, sizeof (StorablePicture*)); // +1 for reordering
307   if (NULL==currSlice->listX[i])
308   no_mem_exit("init_dpb: currSlice->listX[i]");
309   }
310   */
311   /* allocate a dummy storable picture */
312   if(!p_Vid->no_reference_picture)
313   {
314     p_Vid->no_reference_picture = alloc_storable_picture (p_Vid, FRAME, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, 1);
315     p_Vid->no_reference_picture->top_field    = p_Vid->no_reference_picture;
316     p_Vid->no_reference_picture->bottom_field = p_Vid->no_reference_picture;
317     p_Vid->no_reference_picture->frame        = p_Vid->no_reference_picture;
318   }
319   p_Dpb->last_output_poc = INT_MIN;
320 
321 #if (MVC_EXTENSION_ENABLE)
322   p_Dpb->last_output_view_id = -1;
323 #endif
324 
325   p_Vid->last_has_mmco_5 = 0;
326 
327   p_Dpb->init_done = 1;
328 
329   // picture error concealment
330   if(p_Vid->conceal_mode !=0 && !p_Vid->last_out_fs)
331     p_Vid->last_out_fs = alloc_frame_store();
332 
333 }
334 
re_init_dpb(VideoParameters * p_Vid,DecodedPictureBuffer * p_Dpb,int type)335 void re_init_dpb(VideoParameters *p_Vid, DecodedPictureBuffer *p_Dpb, int type)
336 {
337   int i;
338   seq_parameter_set_rbsp_t *active_sps = p_Vid->active_sps;
339   int iDpbSize;
340 
341   iDpbSize = getDpbSize(p_Vid, active_sps)+p_Vid->p_Inp->dpb_plus[type==2? 1: 0];
342   p_Dpb->num_ref_frames = active_sps->num_ref_frames;
343   if( iDpbSize > (int)p_Dpb->size)
344   {
345 #if (MVC_EXTENSION_ENABLE)
346     if ((unsigned int)active_sps->max_dec_frame_buffering < active_sps->num_ref_frames)
347 #else
348     if (p_Dpb->size < active_sps->num_ref_frames)
349 #endif
350     {
351       error ("DPB size at specified level is smaller than the specified number of reference frames. This is not allowed.\n", 1000);
352     }
353 
354     p_Dpb->fs = realloc(p_Dpb->fs, iDpbSize * sizeof (FrameStore*));
355     if (NULL==p_Dpb->fs)
356       no_mem_exit("re_init_dpb: p_Dpb->fs");
357 
358     p_Dpb->fs_ref = realloc(p_Dpb->fs_ref, iDpbSize * sizeof (FrameStore*));
359     if (NULL==p_Dpb->fs_ref)
360       no_mem_exit("re_init_dpb: p_Dpb->fs_ref");
361 
362     p_Dpb->fs_ltref = realloc(p_Dpb->fs_ltref, iDpbSize * sizeof (FrameStore*));
363     if (NULL==p_Dpb->fs_ltref)
364       no_mem_exit("re_init_dpb: p_Dpb->fs_ltref");
365 
366 #if (MVC_EXTENSION_ENABLE)
367     if(!p_Dpb->fs_ilref)
368     {
369       p_Dpb->fs_ilref = calloc(1, sizeof (FrameStore*));
370       if (NULL==p_Dpb->fs_ilref)
371         no_mem_exit("init_dpb: p_Dpb->fs_ilref");
372     }
373 #endif
374 
375     for (i = p_Dpb->size; i < iDpbSize; i++)
376     {
377       p_Dpb->fs[i]       = alloc_frame_store();
378       p_Dpb->fs_ref[i]   = NULL;
379       p_Dpb->fs_ltref[i] = NULL;
380 #if (MVC_EXTENSION_ENABLE)
381       p_Dpb->fs[i]->view_id = MVC_INIT_VIEW_ID;
382       p_Dpb->fs[i]->inter_view_flag[0] = p_Dpb->fs[i]->inter_view_flag[1] = 0;
383       p_Dpb->fs[i]->anchor_pic_flag[0] = p_Dpb->fs[i]->anchor_pic_flag[1] = 0;
384 #endif
385     }
386 
387 #if (MVC_EXTENSION_ENABLE)
388   if (type == 2 && !p_Dpb->fs_ilref[0])
389   {
390     p_Dpb->fs_ilref[0] = alloc_frame_store();
391     // These may need some cleanups
392     p_Dpb->fs_ilref[0]->view_id = MVC_INIT_VIEW_ID;
393     p_Dpb->fs_ilref[0]->inter_view_flag[0] = p_Dpb->fs_ilref[0]->inter_view_flag[1] = 0;
394     p_Dpb->fs_ilref[0]->anchor_pic_flag[0] = p_Dpb->fs_ilref[0]->anchor_pic_flag[1] = 0;
395     // given that this is in a different buffer, do we even need proc_flag anymore?
396   }
397   else
398     p_Dpb->fs_ilref[0] = NULL;
399 #endif
400 
401     p_Dpb->size = iDpbSize;
402     p_Dpb->last_output_poc = INT_MIN;
403 #if (MVC_EXTENSION_ENABLE)
404     p_Dpb->last_output_view_id = -1;
405 #endif
406     p_Dpb->init_done = 1;
407 
408   }
409 }
410 
411 /*!
412  ************************************************************************
413  * \brief
414  *    Free memory for decoded picture buffer.
415  ************************************************************************
416  */
free_dpb(DecodedPictureBuffer * p_Dpb)417 void free_dpb(DecodedPictureBuffer *p_Dpb)
418 {
419   VideoParameters *p_Vid = p_Dpb->p_Vid;
420   unsigned i;
421   if (p_Dpb->fs)
422   {
423     for (i=0; i<p_Dpb->size; i++)
424     {
425       free_frame_store(p_Dpb->fs[i]);
426     }
427     free (p_Dpb->fs);
428     p_Dpb->fs=NULL;
429   }
430 
431   if (p_Dpb->fs_ref)
432   {
433     free (p_Dpb->fs_ref);
434   }
435   if (p_Dpb->fs_ltref)
436   {
437     free (p_Dpb->fs_ltref);
438   }
439 
440 #if (MVC_EXTENSION_ENABLE)
441   if (p_Dpb->fs_ilref)
442   {
443     for (i=0; i<1; i++)
444     {
445       free_frame_store(p_Dpb->fs_ilref[i]);
446     }
447     free (p_Dpb->fs_ilref);
448     p_Dpb->fs_ilref=NULL;
449   }
450 
451   p_Dpb->last_output_view_id = -1;
452 #endif
453 
454   p_Dpb->last_output_poc = INT_MIN;
455 
456   p_Dpb->init_done = 0;
457 
458   // picture error concealment
459   if(p_Vid->conceal_mode != 0 || p_Vid->last_out_fs)
460       free_frame_store(p_Vid->last_out_fs);
461 
462   if(p_Vid->no_reference_picture)
463   {
464     free_storable_picture(p_Vid->no_reference_picture);
465     p_Vid->no_reference_picture = NULL;
466   }
467 }
468 
469 
470 /*!
471  ************************************************************************
472  * \brief
473  *    Allocate memory for decoded picture buffer frame stores and initialize with sane values.
474  *
475  * \return
476  *    the allocated FrameStore structure
477  ************************************************************************
478  */
alloc_frame_store(void)479 FrameStore* alloc_frame_store(void)
480 {
481   FrameStore *f;
482 
483   f = calloc (1, sizeof(FrameStore));
484   if (NULL==f)
485     no_mem_exit("alloc_frame_store: f");
486 
487   f->is_used      = 0;
488   f->is_reference = 0;
489   f->is_long_term = 0;
490   f->is_orig_reference = 0;
491 
492   f->is_output = 0;
493 
494   f->frame        = NULL;;
495   f->top_field    = NULL;
496   f->bottom_field = NULL;
497 
498   return f;
499 }
500 
alloc_pic_motion(PicMotionParamsOld * motion,int size_y,int size_x)501 void alloc_pic_motion(PicMotionParamsOld *motion, int size_y, int size_x)
502 {
503   motion->mb_field = calloc (size_y * size_x, sizeof(byte));
504   if (motion->mb_field == NULL)
505     no_mem_exit("alloc_storable_picture: motion->mb_field");
506 }
507 
508 /*!
509  ************************************************************************
510  * \brief
511  *    Allocate memory for a stored picture.
512  *
513  * \param p_Vid
514  *    VideoParameters
515  * \param structure
516  *    picture structure
517  * \param size_x
518  *    horizontal luma size
519  * \param size_y
520  *    vertical luma size
521  * \param size_x_cr
522  *    horizontal chroma size
523  * \param size_y_cr
524  *    vertical chroma size
525  *
526  * \return
527  *    the allocated StorablePicture structure
528  ************************************************************************
529  */
alloc_storable_picture(VideoParameters * p_Vid,PictureStructure structure,int size_x,int size_y,int size_x_cr,int size_y_cr,int is_output)530 StorablePicture* alloc_storable_picture(VideoParameters *p_Vid, PictureStructure structure, int size_x, int size_y, int size_x_cr, int size_y_cr, int is_output)
531 {
532   seq_parameter_set_rbsp_t *active_sps = p_Vid->active_sps;
533 
534   StorablePicture *s;
535   int   nplane;
536 
537   //printf ("Allocating (%s) picture (x=%d, y=%d, x_cr=%d, y_cr=%d)\n", (type == FRAME)?"FRAME":(type == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", size_x, size_y, size_x_cr, size_y_cr);
538 
539   s = calloc (1, sizeof(StorablePicture));
540   if (NULL==s)
541     no_mem_exit("alloc_storable_picture: s");
542 
543   if (structure!=FRAME)
544   {
545     size_y    /= 2;
546     size_y_cr /= 2;
547   }
548 
549   s->PicSizeInMbs = (size_x*size_y)/256;
550   s->imgUV = NULL;
551 
552   get_mem2Dpel_pad (&(s->imgY), size_y, size_x, p_Vid->iLumaPadY, p_Vid->iLumaPadX);
553   s->iLumaStride = size_x+2*p_Vid->iLumaPadX;
554   s->iLumaExpandedHeight = size_y+2*p_Vid->iLumaPadY;
555 
556   if (active_sps->chroma_format_idc != YUV400)
557   {
558     get_mem3Dpel_pad(&(s->imgUV), 2, size_y_cr, size_x_cr, p_Vid->iChromaPadY, p_Vid->iChromaPadX);
559   }
560 
561   s->iChromaStride =size_x_cr + 2*p_Vid->iChromaPadX;
562   s->iChromaExpandedHeight = size_y_cr + 2*p_Vid->iChromaPadY;
563   s->iLumaPadY   = p_Vid->iLumaPadY;
564   s->iLumaPadX   = p_Vid->iLumaPadX;
565   s->iChromaPadY = p_Vid->iChromaPadY;
566   s->iChromaPadX = p_Vid->iChromaPadX;
567 
568   s->separate_colour_plane_flag = p_Vid->separate_colour_plane_flag;
569 
570   get_mem2Dmp     ( &s->mv_info, (size_y >> BLOCK_SHIFT), (size_x >> BLOCK_SHIFT));
571   alloc_pic_motion( &s->motion , (size_y >> BLOCK_SHIFT), (size_x >> BLOCK_SHIFT));
572 
573   if( (p_Vid->separate_colour_plane_flag != 0) )
574   {
575     for( nplane=0; nplane<MAX_PLANE; nplane++ )
576     {
577       get_mem2Dmp      (&s->JVmv_info[nplane], (size_y >> BLOCK_SHIFT), (size_x >> BLOCK_SHIFT));
578       alloc_pic_motion(&s->JVmotion[nplane] , (size_y >> BLOCK_SHIFT), (size_x >> BLOCK_SHIFT));
579     }
580   }
581 
582   s->pic_num   = 0;
583   s->frame_num = 0;
584   s->long_term_frame_idx = 0;
585   s->long_term_pic_num   = 0;
586   s->used_for_reference  = 0;
587   s->is_long_term        = 0;
588   s->non_existing        = 0;
589   s->is_output           = 0;
590   s->max_slice_id        = 0;
591 #if (MVC_EXTENSION_ENABLE)
592   s->view_id = -1;
593 #endif
594 
595   s->structure=structure;
596 
597   s->size_x = size_x;
598   s->size_y = size_y;
599   s->size_x_cr = size_x_cr;
600   s->size_y_cr = size_y_cr;
601   s->size_x_m1 = size_x - 1;
602   s->size_y_m1 = size_y - 1;
603   s->size_x_cr_m1 = size_x_cr - 1;
604   s->size_y_cr_m1 = size_y_cr - 1;
605 
606   s->top_field    = p_Vid->no_reference_picture;
607   s->bottom_field = p_Vid->no_reference_picture;
608   s->frame        = p_Vid->no_reference_picture;
609 
610   s->dec_ref_pic_marking_buffer = NULL;
611 
612   s->coded_frame  = 0;
613   s->mb_aff_frame_flag  = 0;
614 
615   s->top_poc = s->bottom_poc = s->poc = 0;
616   s->seiHasTone_mapping = 0;
617 
618   if(!p_Vid->active_sps->frame_mbs_only_flag && structure != FRAME)
619   {
620     int i, j;
621     for(j = 0; j < MAX_NUM_SLICES; j++)
622     {
623       for (i = 0; i < 2; i++)
624       {
625         s->listX[j][i] = calloc(MAX_LIST_SIZE, sizeof (StorablePicture*)); // +1 for reordering
626         if (NULL==s->listX[j][i])
627         no_mem_exit("alloc_storable_picture: s->listX[i]");
628       }
629     }
630   }
631 
632   return s;
633 }
634 
635 /*!
636  ************************************************************************
637  * \brief
638  *    Free frame store memory.
639  *
640  * \param p_Vid
641  *    VideoParameters
642  * \param f
643  *    FrameStore to be freed
644  *
645  ************************************************************************
646  */
free_frame_store(FrameStore * f)647 void free_frame_store(FrameStore* f)
648 {
649   if (f)
650   {
651     if (f->frame)
652     {
653       free_storable_picture(f->frame);
654       f->frame=NULL;
655     }
656     if (f->top_field)
657     {
658       free_storable_picture(f->top_field);
659       f->top_field=NULL;
660     }
661     if (f->bottom_field)
662     {
663       free_storable_picture(f->bottom_field);
664       f->bottom_field=NULL;
665     }
666     free(f);
667   }
668 }
669 
free_pic_motion(PicMotionParamsOld * motion)670 void free_pic_motion(PicMotionParamsOld *motion)
671 {
672   if (motion->mb_field)
673   {
674     free(motion->mb_field);
675     motion->mb_field = NULL;
676   }
677 }
678 
679 
680 /*!
681  ************************************************************************
682  * \brief
683  *    Free picture memory.
684  *
685  * \param p
686  *    Picture to be freed
687  *
688  ************************************************************************
689  */
free_storable_picture(StorablePicture * p)690 void free_storable_picture(StorablePicture* p)
691 {
692   int nplane;
693   if (p)
694   {
695     if (p->mv_info)
696     {
697       free_mem2Dmp(p->mv_info);
698       p->mv_info = NULL;
699     }
700     free_pic_motion(&p->motion);
701 
702     if( (p->separate_colour_plane_flag != 0) )
703     {
704       for( nplane=0; nplane<MAX_PLANE; nplane++ )
705       {
706         if (p->JVmv_info[nplane])
707         {
708           free_mem2Dmp(p->JVmv_info[nplane]);
709           p->JVmv_info[nplane] = NULL;
710         }
711         free_pic_motion(&p->JVmotion[nplane]);
712       }
713     }
714 
715     if (p->imgY)
716     {
717       free_mem2Dpel_pad(p->imgY, p->iLumaPadY, p->iLumaPadX);
718       p->imgY = NULL;
719     }
720 
721     if (p->imgUV)
722     {
723       free_mem3Dpel_pad(p->imgUV, 2, p->iChromaPadY, p->iChromaPadX);
724       p->imgUV=NULL;
725     }
726 
727 
728     if (p->seiHasTone_mapping)
729       free(p->tone_mapping_lut);
730 
731     {
732       int i, j;
733       for(j = 0; j < MAX_NUM_SLICES; j++)
734       {
735         for(i=0; i<2; i++)
736         {
737           if(p->listX[j][i])
738           {
739             free(p->listX[j][i]);
740             p->listX[j][i] = NULL;
741           }
742         }
743       }
744     }
745     free(p);
746     p = NULL;
747   }
748 }
749 
750 /*!
751  ************************************************************************
752  * \brief
753  *    mark FrameStore unused for reference
754  *
755  ************************************************************************
756  */
unmark_for_reference(FrameStore * fs)757 void unmark_for_reference(FrameStore* fs)
758 {
759   if (fs->is_used & 1)
760   {
761     if (fs->top_field)
762     {
763       fs->top_field->used_for_reference = 0;
764     }
765   }
766   if (fs->is_used & 2)
767   {
768     if (fs->bottom_field)
769     {
770       fs->bottom_field->used_for_reference = 0;
771     }
772   }
773   if (fs->is_used == 3)
774   {
775     if (fs->top_field && fs->bottom_field)
776     {
777       fs->top_field->used_for_reference = 0;
778       fs->bottom_field->used_for_reference = 0;
779     }
780     fs->frame->used_for_reference = 0;
781   }
782 
783   fs->is_reference = 0;
784 
785   if(fs->frame)
786   {
787     free_pic_motion(&fs->frame->motion);
788   }
789 
790   if (fs->top_field)
791   {
792     free_pic_motion(&fs->top_field->motion);
793   }
794 
795   if (fs->bottom_field)
796   {
797     free_pic_motion(&fs->bottom_field->motion);
798   }
799 }
800 
801 
802 /*!
803  ************************************************************************
804  * \brief
805  *    mark FrameStore unused for reference and reset long term flags
806  *
807  ************************************************************************
808  */
unmark_for_long_term_reference(FrameStore * fs)809 void unmark_for_long_term_reference(FrameStore* fs)
810 {
811   if (fs->is_used & 1)
812   {
813     if (fs->top_field)
814     {
815       fs->top_field->used_for_reference = 0;
816       fs->top_field->is_long_term = 0;
817     }
818   }
819   if (fs->is_used & 2)
820   {
821     if (fs->bottom_field)
822     {
823       fs->bottom_field->used_for_reference = 0;
824       fs->bottom_field->is_long_term = 0;
825     }
826   }
827   if (fs->is_used == 3)
828   {
829     if (fs->top_field && fs->bottom_field)
830     {
831       fs->top_field->used_for_reference = 0;
832       fs->top_field->is_long_term = 0;
833       fs->bottom_field->used_for_reference = 0;
834       fs->bottom_field->is_long_term = 0;
835     }
836     fs->frame->used_for_reference = 0;
837     fs->frame->is_long_term = 0;
838   }
839 
840   fs->is_reference = 0;
841   fs->is_long_term = 0;
842 }
843 
844 
845 
update_pic_num(Slice * currSlice)846 void update_pic_num(Slice *currSlice)
847 {
848   unsigned int i;
849   VideoParameters *p_Vid = currSlice->p_Vid;
850   DecodedPictureBuffer *p_Dpb = currSlice->p_Dpb;
851   seq_parameter_set_rbsp_t *active_sps = p_Vid->active_sps;
852 
853   int add_top = 0, add_bottom = 0;
854   int max_frame_num = 1 << (active_sps->log2_max_frame_num_minus4 + 4);
855 
856   if (currSlice->structure == FRAME)
857   {
858     for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
859     {
860       if ( p_Dpb->fs_ref[i]->is_used==3 )
861       {
862         if ((p_Dpb->fs_ref[i]->frame->used_for_reference)&&(!p_Dpb->fs_ref[i]->frame->is_long_term))
863         {
864           if( p_Dpb->fs_ref[i]->frame_num > currSlice->frame_num )
865           {
866             p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num - max_frame_num;
867           }
868           else
869           {
870             p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num;
871           }
872           p_Dpb->fs_ref[i]->frame->pic_num = p_Dpb->fs_ref[i]->frame_num_wrap;
873         }
874       }
875     }
876     // update long_term_pic_num
877     for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++)
878     {
879       if (p_Dpb->fs_ltref[i]->is_used==3)
880       {
881         if (p_Dpb->fs_ltref[i]->frame->is_long_term)
882         {
883           p_Dpb->fs_ltref[i]->frame->long_term_pic_num = p_Dpb->fs_ltref[i]->frame->long_term_frame_idx;
884         }
885       }
886     }
887   }
888   else
889   {
890     if (currSlice->structure == TOP_FIELD)
891     {
892       add_top    = 1;
893       add_bottom = 0;
894     }
895     else
896     {
897       add_top    = 0;
898       add_bottom = 1;
899     }
900 
901     for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
902     {
903       if (p_Dpb->fs_ref[i]->is_reference)
904       {
905         if( p_Dpb->fs_ref[i]->frame_num > currSlice->frame_num )
906         {
907           p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num - max_frame_num;
908         }
909         else
910         {
911           p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num;
912         }
913         if (p_Dpb->fs_ref[i]->is_reference & 1)
914         {
915           p_Dpb->fs_ref[i]->top_field->pic_num = (2 * p_Dpb->fs_ref[i]->frame_num_wrap) + add_top;
916         }
917         if (p_Dpb->fs_ref[i]->is_reference & 2)
918         {
919           p_Dpb->fs_ref[i]->bottom_field->pic_num = (2 * p_Dpb->fs_ref[i]->frame_num_wrap) + add_bottom;
920         }
921       }
922     }
923     // update long_term_pic_num
924     for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
925     {
926       if (p_Dpb->fs_ltref[i]->is_long_term & 1)
927       {
928         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;
929       }
930       if (p_Dpb->fs_ltref[i]->is_long_term & 2)
931       {
932         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;
933       }
934     }
935   }
936 }
937 /*!
938  ************************************************************************
939  * \brief
940  *    Initialize reference lists depending on current slice type
941  *
942  ************************************************************************
943  */
init_lists_i_slice(Slice * currSlice)944 void init_lists_i_slice(Slice *currSlice)
945 {
946 
947 #if (MVC_EXTENSION_ENABLE)
948   currSlice->listinterviewidx0 = 0;
949   currSlice->listinterviewidx1 = 0;
950 #endif
951 
952   currSlice->listXsize[0] = 0;
953   currSlice->listXsize[1] = 0;
954 }
955 
956 /*!
957  ************************************************************************
958  * \brief
959  *    Initialize reference lists for a P Slice
960  *
961  ************************************************************************
962  */
init_lists_p_slice(Slice * currSlice)963 void init_lists_p_slice(Slice *currSlice)
964 {
965   VideoParameters *p_Vid = currSlice->p_Vid;
966   DecodedPictureBuffer *p_Dpb = currSlice->p_Dpb;
967 
968   unsigned int i;
969 
970   int list0idx = 0;
971   int listltidx = 0;
972 
973   FrameStore **fs_list0;
974   FrameStore **fs_listlt;
975 
976 #if (MVC_EXTENSION_ENABLE)
977   currSlice->listinterviewidx0 = 0;
978   currSlice->listinterviewidx1 = 0;
979 #endif
980 
981   if (currSlice->structure == FRAME)
982   {
983     for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
984     {
985       if (p_Dpb->fs_ref[i]->is_used==3)
986       {
987         if ((p_Dpb->fs_ref[i]->frame->used_for_reference) && (!p_Dpb->fs_ref[i]->frame->is_long_term))
988         {
989           currSlice->listX[0][list0idx++] = p_Dpb->fs_ref[i]->frame;
990         }
991       }
992     }
993     // order list 0 by PicNum
994     qsort((void *)currSlice->listX[0], list0idx, sizeof(StorablePicture*), compare_pic_by_pic_num_desc);
995     currSlice->listXsize[0] = (char) list0idx;
996     //printf("listX[0] (PicNum): "); for (i=0; i<list0idx; i++){printf ("%d  ", currSlice->listX[0][i]->pic_num);} printf("\n");
997 
998     // long term handling
999     for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
1000     {
1001       if (p_Dpb->fs_ltref[i]->is_used==3)
1002       {
1003         if (p_Dpb->fs_ltref[i]->frame->is_long_term)
1004         {
1005           currSlice->listX[0][list0idx++] = p_Dpb->fs_ltref[i]->frame;
1006         }
1007       }
1008     }
1009     qsort((void *)&currSlice->listX[0][(short) currSlice->listXsize[0]], list0idx - currSlice->listXsize[0], sizeof(StorablePicture*), compare_pic_by_lt_pic_num_asc);
1010     currSlice->listXsize[0] = (char) list0idx;
1011   }
1012   else
1013   {
1014     fs_list0 = calloc(p_Dpb->size, sizeof (FrameStore*));
1015     if (NULL==fs_list0)
1016       no_mem_exit("init_lists: fs_list0");
1017     fs_listlt = calloc(p_Dpb->size, sizeof (FrameStore*));
1018     if (NULL==fs_listlt)
1019       no_mem_exit("init_lists: fs_listlt");
1020 
1021     for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
1022     {
1023       if (p_Dpb->fs_ref[i]->is_reference)
1024       {
1025         fs_list0[list0idx++] = p_Dpb->fs_ref[i];
1026       }
1027     }
1028 
1029     qsort((void *)fs_list0, list0idx, sizeof(FrameStore*), compare_fs_by_frame_num_desc);
1030 
1031     //printf("fs_list0 (FrameNum): "); for (i=0; i<list0idx; i++){printf ("%d  ", fs_list0[i]->frame_num_wrap);} printf("\n");
1032 
1033     currSlice->listXsize[0] = 0;
1034     gen_pic_list_from_frame_list(currSlice->structure, fs_list0, list0idx, currSlice->listX[0], &currSlice->listXsize[0], 0);
1035 
1036     //printf("listX[0] (PicNum): "); for (i=0; i < currSlice->listXsize[0]; i++){printf ("%d  ", currSlice->listX[0][i]->pic_num);} printf("\n");
1037 
1038     // long term handling
1039     for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
1040     {
1041       fs_listlt[listltidx++]=p_Dpb->fs_ltref[i];
1042     }
1043 
1044     qsort((void *)fs_listlt, listltidx, sizeof(FrameStore*), compare_fs_by_lt_pic_idx_asc);
1045 
1046     gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, listltidx, currSlice->listX[0], &currSlice->listXsize[0], 1);
1047 
1048     free(fs_list0);
1049     free(fs_listlt);
1050   }
1051   currSlice->listXsize[1] = 0;
1052 
1053 
1054   // set max size
1055   currSlice->listXsize[0] = (char) imin (currSlice->listXsize[0], currSlice->num_ref_idx_active[LIST_0]);
1056   currSlice->listXsize[1] = (char) imin (currSlice->listXsize[1], currSlice->num_ref_idx_active[LIST_1]);
1057 
1058   // set the unused list entries to NULL
1059   for (i=currSlice->listXsize[0]; i< (MAX_LIST_SIZE) ; i++)
1060   {
1061     currSlice->listX[0][i] = p_Vid->no_reference_picture;
1062   }
1063   for (i=currSlice->listXsize[1]; i< (MAX_LIST_SIZE) ; i++)
1064   {
1065     currSlice->listX[1][i] = p_Vid->no_reference_picture;
1066   }
1067 
1068 #if PRINTREFLIST
1069 #if (MVC_EXTENSION_ENABLE)
1070   // print out for debug purpose
1071   if((p_Vid->profile_idc == MVC_HIGH || p_Vid->profile_idc == STEREO_HIGH) && currSlice->current_slice_nr==0)
1072   {
1073     if(currSlice->listXsize[0]>0)
1074     {
1075       printf("\n");
1076       printf(" ** (CurViewID:%d %d) %s Ref Pic List 0 ****\n", currSlice->view_id, currSlice->ThisPOC, currSlice->structure==FRAME ? "FRM":(currSlice->structure==TOP_FIELD ? "TOP":"BOT"));
1077       for(i=0; i<(unsigned int)(currSlice->listXsize[0]); i++)  //ref list 0
1078       {
1079         printf("   %2d -> POC: %4d PicNum: %4d ViewID: %d\n", i, currSlice->listX[0][i]->poc, currSlice->listX[0][i]->pic_num, currSlice->listX[0][i]->view_id);
1080       }
1081     }
1082   }
1083 #endif
1084 #endif
1085 }
1086 
1087 
1088 /*!
1089  ************************************************************************
1090  * \brief
1091  *    Initialize reference lists for a B Slice
1092  *
1093  ************************************************************************
1094  */
init_lists_b_slice(Slice * currSlice)1095 void init_lists_b_slice(Slice *currSlice)
1096 {
1097   VideoParameters *p_Vid = currSlice->p_Vid;
1098   DecodedPictureBuffer *p_Dpb = currSlice->p_Dpb;
1099 
1100   unsigned int i;
1101   int j;
1102 
1103   int list0idx = 0;
1104   int list0idx_1 = 0;
1105   int listltidx = 0;
1106 
1107   FrameStore **fs_list0;
1108   FrameStore **fs_list1;
1109   FrameStore **fs_listlt;
1110 
1111 #if (MVC_EXTENSION_ENABLE)
1112   currSlice->listinterviewidx0 = 0;
1113   currSlice->listinterviewidx1 = 0;
1114 #endif
1115 
1116   {
1117     // B-Slice
1118     if (currSlice->structure == FRAME)
1119     {
1120       for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++)
1121       {
1122         if (p_Dpb->fs_ref[i]->is_used==3)
1123         {
1124           if ((p_Dpb->fs_ref[i]->frame->used_for_reference)&&(!p_Dpb->fs_ref[i]->frame->is_long_term))
1125           {
1126             if (currSlice->framepoc >= p_Dpb->fs_ref[i]->frame->poc) //!KS use >= for error concealment
1127             {
1128               currSlice->listX[0][list0idx++] = p_Dpb->fs_ref[i]->frame;
1129             }
1130           }
1131         }
1132       }
1133       qsort((void *)currSlice->listX[0], list0idx, sizeof(StorablePicture*), compare_pic_by_poc_desc);
1134 
1135       //get the backward reference picture (POC>current POC) in list0;
1136       list0idx_1 = list0idx;
1137       for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
1138       {
1139         if (p_Dpb->fs_ref[i]->is_used==3)
1140         {
1141           if ((p_Dpb->fs_ref[i]->frame->used_for_reference)&&(!p_Dpb->fs_ref[i]->frame->is_long_term))
1142           {
1143             if (currSlice->framepoc < p_Dpb->fs_ref[i]->frame->poc)
1144             {
1145               currSlice->listX[0][list0idx++] = p_Dpb->fs_ref[i]->frame;
1146             }
1147           }
1148         }
1149       }
1150       qsort((void *)&currSlice->listX[0][list0idx_1], list0idx-list0idx_1, sizeof(StorablePicture*), compare_pic_by_poc_asc);
1151 
1152       for (j=0; j<list0idx_1; j++)
1153       {
1154         currSlice->listX[1][list0idx-list0idx_1+j]=currSlice->listX[0][j];
1155       }
1156       for (j=list0idx_1; j<list0idx; j++)
1157       {
1158         currSlice->listX[1][j-list0idx_1]=currSlice->listX[0][j];
1159       }
1160 
1161       currSlice->listXsize[0] = currSlice->listXsize[1] = (char) list0idx;
1162 
1163       //printf("listX[0] (PicNum): "); for (i=0; i<currSlice->listXsize[0]; i++){printf ("%d  ", currSlice->listX[0][i]->pic_num);} printf("\n");
1164       //printf("listX[1] (PicNum): "); for (i=0; i<currSlice->listXsize[1]; i++){printf ("%d  ", currSlice->listX[1][i]->pic_num);} printf("\n");
1165       //printf("currSlice->listX[0] currPoc=%d (Poc): ", p_Vid->framepoc); for (i=0; i<currSlice->listXsize[0]; i++){printf ("%d  ", currSlice->listX[0][i]->poc);} printf("\n");
1166       //printf("currSlice->listX[1] currPoc=%d (Poc): ", p_Vid->framepoc); for (i=0; i<currSlice->listXsize[1]; i++){printf ("%d  ", currSlice->listX[1][i]->poc);} printf("\n");
1167 
1168       // long term handling
1169       for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
1170       {
1171         if (p_Dpb->fs_ltref[i]->is_used==3)
1172         {
1173           if (p_Dpb->fs_ltref[i]->frame->is_long_term)
1174           {
1175             currSlice->listX[0][list0idx]   = p_Dpb->fs_ltref[i]->frame;
1176             currSlice->listX[1][list0idx++] = p_Dpb->fs_ltref[i]->frame;
1177           }
1178         }
1179       }
1180       qsort((void *)&currSlice->listX[0][(short) currSlice->listXsize[0]], list0idx - currSlice->listXsize[0], sizeof(StorablePicture*), compare_pic_by_lt_pic_num_asc);
1181       qsort((void *)&currSlice->listX[1][(short) currSlice->listXsize[0]], list0idx - currSlice->listXsize[0], sizeof(StorablePicture*), compare_pic_by_lt_pic_num_asc);
1182       currSlice->listXsize[0] = currSlice->listXsize[1] = (char) list0idx;
1183     }
1184     else
1185     {
1186       fs_list0 = calloc(p_Dpb->size, sizeof (FrameStore*));
1187       if (NULL==fs_list0)
1188         no_mem_exit("init_lists: fs_list0");
1189       fs_list1 = calloc(p_Dpb->size, sizeof (FrameStore*));
1190       if (NULL==fs_list1)
1191         no_mem_exit("init_lists: fs_list1");
1192       fs_listlt = calloc(p_Dpb->size, sizeof (FrameStore*));
1193       if (NULL==fs_listlt)
1194         no_mem_exit("init_lists: fs_listlt");
1195 
1196       currSlice->listXsize[0] = 0;
1197       currSlice->listXsize[1] = 1;
1198 
1199       for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
1200       {
1201         if (p_Dpb->fs_ref[i]->is_used)
1202         {
1203           if (currSlice->ThisPOC >= p_Dpb->fs_ref[i]->poc)
1204           {
1205             fs_list0[list0idx++] = p_Dpb->fs_ref[i];
1206           }
1207         }
1208       }
1209       qsort((void *)fs_list0, list0idx, sizeof(FrameStore*), compare_fs_by_poc_desc);
1210       list0idx_1 = list0idx;
1211       for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
1212       {
1213         if (p_Dpb->fs_ref[i]->is_used)
1214         {
1215           if (currSlice->ThisPOC < p_Dpb->fs_ref[i]->poc)
1216           {
1217             fs_list0[list0idx++] = p_Dpb->fs_ref[i];
1218           }
1219         }
1220       }
1221       qsort((void *)&fs_list0[list0idx_1], list0idx-list0idx_1, sizeof(FrameStore*), compare_fs_by_poc_asc);
1222 
1223       for (j=0; j<list0idx_1; j++)
1224       {
1225         fs_list1[list0idx-list0idx_1+j]=fs_list0[j];
1226       }
1227       for (j=list0idx_1; j<list0idx; j++)
1228       {
1229         fs_list1[j-list0idx_1]=fs_list0[j];
1230       }
1231 
1232       //printf("fs_list0 currPoc=%d (Poc): ", currSlice->ThisPOC); for (i=0; i<list0idx; i++){printf ("%d  ", fs_list0[i]->poc);} printf("\n");
1233       //printf("fs_list1 currPoc=%d (Poc): ", currSlice->ThisPOC); for (i=0; i<list0idx; i++){printf ("%d  ", fs_list1[i]->poc);} printf("\n");
1234 
1235       currSlice->listXsize[0] = 0;
1236       currSlice->listXsize[1] = 0;
1237       gen_pic_list_from_frame_list(currSlice->structure, fs_list0, list0idx, currSlice->listX[0], &currSlice->listXsize[0], 0);
1238       gen_pic_list_from_frame_list(currSlice->structure, fs_list1, list0idx, currSlice->listX[1], &currSlice->listXsize[1], 0);
1239 
1240       //printf("currSlice->listX[0] currPoc=%d (Poc): ", p_Vid->framepoc); for (i=0; i<currSlice->listXsize[0]; i++){printf ("%d  ", currSlice->listX[0][i]->poc);} printf("\n");
1241       //printf("currSlice->listX[1] currPoc=%d (Poc): ", p_Vid->framepoc); for (i=0; i<currSlice->listXsize[1]; i++){printf ("%d  ", currSlice->listX[1][i]->poc);} printf("\n");
1242 
1243       // long term handling
1244       for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
1245       {
1246         fs_listlt[listltidx++]=p_Dpb->fs_ltref[i];
1247       }
1248 
1249       qsort((void *)fs_listlt, listltidx, sizeof(FrameStore*), compare_fs_by_lt_pic_idx_asc);
1250 
1251       gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, listltidx, currSlice->listX[0], &currSlice->listXsize[0], 1);
1252       gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, listltidx, currSlice->listX[1], &currSlice->listXsize[1], 1);
1253 
1254       free(fs_list0);
1255       free(fs_list1);
1256       free(fs_listlt);
1257     }
1258   }
1259 
1260   if ((currSlice->listXsize[0] == currSlice->listXsize[1]) && (currSlice->listXsize[0] > 1))
1261   {
1262     // check if lists are identical, if yes swap first two elements of currSlice->listX[1]
1263     int diff=0;
1264     for (j = 0; j< currSlice->listXsize[0]; j++)
1265     {
1266       if (currSlice->listX[0][j] != currSlice->listX[1][j])
1267       {
1268         diff = 1;
1269         break;
1270       }
1271     }
1272     if (!diff)
1273     {
1274       StorablePicture *tmp_s = currSlice->listX[1][0];
1275       currSlice->listX[1][0]=currSlice->listX[1][1];
1276       currSlice->listX[1][1]=tmp_s;
1277     }
1278   }
1279 
1280   // set max size
1281   currSlice->listXsize[0] = (char) imin (currSlice->listXsize[0], currSlice->num_ref_idx_active[LIST_0]);
1282   currSlice->listXsize[1] = (char) imin (currSlice->listXsize[1], currSlice->num_ref_idx_active[LIST_1]);
1283 
1284   // set the unused list entries to NULL
1285   for (i=currSlice->listXsize[0]; i< (MAX_LIST_SIZE) ; i++)
1286   {
1287     currSlice->listX[0][i] = p_Vid->no_reference_picture;
1288   }
1289   for (i=currSlice->listXsize[1]; i< (MAX_LIST_SIZE) ; i++)
1290   {
1291     currSlice->listX[1][i] = p_Vid->no_reference_picture;
1292   }
1293 
1294 #if PRINTREFLIST
1295 #if (MVC_EXTENSION_ENABLE)
1296   // print out for debug purpose
1297   if((p_Vid->profile_idc == MVC_HIGH || p_Vid->profile_idc == STEREO_HIGH) && currSlice->current_slice_nr==0)
1298   {
1299     if((currSlice->listXsize[0]>0) || (currSlice->listXsize[1]>0))
1300       printf("\n");
1301     if(currSlice->listXsize[0]>0)
1302     {
1303       printf(" ** (CurViewID:%d %d) %s Ref Pic List 0 ****\n", currSlice->view_id, currSlice->ThisPOC, currSlice->structure==FRAME ? "FRM":(currSlice->structure==TOP_FIELD ? "TOP":"BOT"));
1304       for(i=0; i<(unsigned int)(currSlice->listXsize[0]); i++)  //ref list 0
1305       {
1306         printf("   %2d -> POC: %4d PicNum: %4d ViewID: %d\n", i, currSlice->listX[0][i]->poc, currSlice->listX[0][i]->pic_num, currSlice->listX[0][i]->view_id);
1307       }
1308     }
1309     if(currSlice->listXsize[1]>0)
1310     {
1311       printf(" ** (CurViewID:%d %d) %s Ref Pic List 1 ****\n", currSlice->view_id, currSlice->ThisPOC, currSlice->structure==FRAME ? "FRM":(currSlice->structure==TOP_FIELD ? "TOP":"BOT"));
1312       for(i=0; i<(unsigned int)(currSlice->listXsize[1]); i++)  //ref list 1
1313       {
1314         printf("   %2d -> POC: %4d PicNum: %4d ViewID: %d\n", i, currSlice->listX[1][i]->poc, currSlice->listX[1][i]->pic_num, currSlice->listX[1][i]->view_id);
1315       }
1316     }
1317   }
1318 #endif
1319 #endif
1320 }
1321 
1322 /*!
1323  ************************************************************************
1324  * \brief
1325  *    Initialize listX[2..5] from lists 0 and 1
1326  *    listX[2]: list0 for current_field==top
1327  *    listX[3]: list1 for current_field==top
1328  *    listX[4]: list0 for current_field==bottom
1329  *    listX[5]: list1 for current_field==bottom
1330  *
1331  ************************************************************************
1332  */
init_mbaff_lists(VideoParameters * p_Vid,Slice * currSlice)1333 void init_mbaff_lists(VideoParameters *p_Vid, Slice *currSlice)
1334 {
1335   unsigned j;
1336   int i;
1337 
1338   for (i=2;i<6;i++)
1339   {
1340     for (j=0; j<MAX_LIST_SIZE; j++)
1341     {
1342       currSlice->listX[i][j] = p_Vid->no_reference_picture;
1343     }
1344     currSlice->listXsize[i]=0;
1345   }
1346 
1347   for (i = 0; i < currSlice->listXsize[0]; i++)
1348   {
1349     currSlice->listX[2][2*i  ] = currSlice->listX[0][i]->top_field;
1350     currSlice->listX[2][2*i+1] = currSlice->listX[0][i]->bottom_field;
1351     currSlice->listX[4][2*i  ] = currSlice->listX[0][i]->bottom_field;
1352     currSlice->listX[4][2*i+1] = currSlice->listX[0][i]->top_field;
1353   }
1354   currSlice->listXsize[2] = currSlice->listXsize[4] = currSlice->listXsize[0] * 2;
1355 
1356   for (i = 0; i < currSlice->listXsize[1]; i++)
1357   {
1358     currSlice->listX[3][2*i  ] = currSlice->listX[1][i]->top_field;
1359     currSlice->listX[3][2*i+1] = currSlice->listX[1][i]->bottom_field;
1360     currSlice->listX[5][2*i  ] = currSlice->listX[1][i]->bottom_field;
1361     currSlice->listX[5][2*i+1] = currSlice->listX[1][i]->top_field;
1362   }
1363   currSlice->listXsize[3] = currSlice->listXsize[5] = currSlice->listXsize[1] * 2;
1364 }
1365 
1366  /*!
1367  ************************************************************************
1368  * \brief
1369  *    Returns short term pic with given picNum
1370  *
1371  ************************************************************************
1372  */
get_short_term_pic(Slice * currSlice,DecodedPictureBuffer * p_Dpb,int picNum)1373 StorablePicture*  get_short_term_pic(Slice *currSlice, DecodedPictureBuffer *p_Dpb, int picNum)
1374 {
1375   unsigned i;
1376 
1377   for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++)
1378   {
1379     if (currSlice->structure == FRAME)
1380     {
1381       if (p_Dpb->fs_ref[i]->is_reference == 3)
1382         if ((!p_Dpb->fs_ref[i]->frame->is_long_term)&&(p_Dpb->fs_ref[i]->frame->pic_num == picNum))
1383           return p_Dpb->fs_ref[i]->frame;
1384     }
1385     else
1386     {
1387       if (p_Dpb->fs_ref[i]->is_reference & 1)
1388         if ((!p_Dpb->fs_ref[i]->top_field->is_long_term)&&(p_Dpb->fs_ref[i]->top_field->pic_num == picNum))
1389           return p_Dpb->fs_ref[i]->top_field;
1390       if (p_Dpb->fs_ref[i]->is_reference & 2)
1391         if ((!p_Dpb->fs_ref[i]->bottom_field->is_long_term)&&(p_Dpb->fs_ref[i]->bottom_field->pic_num == picNum))
1392           return p_Dpb->fs_ref[i]->bottom_field;
1393     }
1394   }
1395 
1396   return currSlice->p_Vid->no_reference_picture;
1397 }
1398 
1399 
1400 
1401 #if (!MVC_EXTENSION_ENABLE)
1402 /*!
1403  ************************************************************************
1404  * \brief
1405  *    Reordering process for short-term reference pictures
1406  *
1407  ************************************************************************
1408  */
reorder_short_term(Slice * currSlice,int cur_list,int num_ref_idx_lX_active_minus1,int picNumLX,int * refIdxLX)1409 static void reorder_short_term(Slice *currSlice, int cur_list, int num_ref_idx_lX_active_minus1, int picNumLX, int *refIdxLX)
1410 {
1411   StorablePicture **RefPicListX = currSlice->listX[cur_list];
1412   int cIdx, nIdx;
1413 
1414   StorablePicture *picLX;
1415 
1416   picLX = get_short_term_pic(currSlice, currSlice->p_Dpb, picNumLX);
1417 
1418   for( cIdx = num_ref_idx_lX_active_minus1+1; cIdx > *refIdxLX; cIdx-- )
1419     RefPicListX[ cIdx ] = RefPicListX[ cIdx - 1];
1420 
1421   RefPicListX[ (*refIdxLX)++ ] = picLX;
1422 
1423   nIdx = *refIdxLX;
1424 
1425   for( cIdx = *refIdxLX; cIdx <= num_ref_idx_lX_active_minus1+1; cIdx++ )
1426   {
1427     if (RefPicListX[ cIdx ])
1428       if( (RefPicListX[ cIdx ]->is_long_term ) ||  (RefPicListX[ cIdx ]->pic_num != picNumLX ))
1429         RefPicListX[ nIdx++ ] = RefPicListX[ cIdx ];
1430   }
1431 }
1432 
1433 
1434 /*!
1435  ************************************************************************
1436  * \brief
1437  *    Reordering process for long-term reference pictures
1438  *
1439  ************************************************************************
1440  */
reorder_long_term(Slice * currSlice,StorablePicture ** RefPicListX,int num_ref_idx_lX_active_minus1,int LongTermPicNum,int * refIdxLX)1441 static void reorder_long_term(Slice *currSlice, StorablePicture **RefPicListX, int num_ref_idx_lX_active_minus1, int LongTermPicNum, int *refIdxLX)
1442 {
1443   int cIdx, nIdx;
1444 
1445   StorablePicture *picLX;
1446 
1447   picLX = get_long_term_pic(currSlice, currSlice->p_Dpb, LongTermPicNum);
1448 
1449   for( cIdx = num_ref_idx_lX_active_minus1+1; cIdx > *refIdxLX; cIdx-- )
1450     RefPicListX[ cIdx ] = RefPicListX[ cIdx - 1];
1451 
1452   RefPicListX[ (*refIdxLX)++ ] = picLX;
1453 
1454   nIdx = *refIdxLX;
1455 
1456   for( cIdx = *refIdxLX; cIdx <= num_ref_idx_lX_active_minus1+1; cIdx++ )
1457   {
1458     if (RefPicListX[ cIdx ])
1459     {
1460       if( (!RefPicListX[ cIdx ]->is_long_term ) ||  (RefPicListX[ cIdx ]->long_term_pic_num != LongTermPicNum ))
1461         RefPicListX[ nIdx++ ] = RefPicListX[ cIdx ];
1462     }
1463   }
1464 }
1465 #endif
1466 
1467 /*!
1468  ************************************************************************
1469  * \brief
1470  *    Reordering process for reference picture lists
1471  *
1472  ************************************************************************
1473  */
reorder_ref_pic_list(Slice * currSlice,int cur_list)1474 void reorder_ref_pic_list(Slice *currSlice, int cur_list)
1475 {
1476   int *modification_of_pic_nums_idc = currSlice->modification_of_pic_nums_idc[cur_list];
1477   int *abs_diff_pic_num_minus1 = currSlice->abs_diff_pic_num_minus1[cur_list];
1478   int *long_term_pic_idx = currSlice->long_term_pic_idx[cur_list];
1479   int num_ref_idx_lX_active_minus1 = currSlice->num_ref_idx_active[cur_list] - 1;
1480 
1481   VideoParameters *p_Vid = currSlice->p_Vid;
1482   int i;
1483 
1484   int maxPicNum, currPicNum, picNumLXNoWrap, picNumLXPred, picNumLX;
1485   int refIdxLX = 0;
1486 
1487   if (currSlice->structure==FRAME)
1488   {
1489     maxPicNum  = p_Vid->max_frame_num;
1490     currPicNum = currSlice->frame_num;
1491   }
1492   else
1493   {
1494     maxPicNum  = 2 * p_Vid->max_frame_num;
1495     currPicNum = 2 * currSlice->frame_num + 1;
1496   }
1497 
1498   picNumLXPred = currPicNum;
1499 
1500   for (i=0; modification_of_pic_nums_idc[i]!=3; i++)
1501   {
1502     if (modification_of_pic_nums_idc[i]>3)
1503       error ("Invalid modification_of_pic_nums_idc command", 500);
1504 
1505     if (modification_of_pic_nums_idc[i] < 2)
1506     {
1507       if (modification_of_pic_nums_idc[i] == 0)
1508       {
1509         if( picNumLXPred - ( abs_diff_pic_num_minus1[i] + 1 ) < 0 )
1510           picNumLXNoWrap = picNumLXPred - ( abs_diff_pic_num_minus1[i] + 1 ) + maxPicNum;
1511         else
1512           picNumLXNoWrap = picNumLXPred - ( abs_diff_pic_num_minus1[i] + 1 );
1513       }
1514       else // (modification_of_pic_nums_idc[i] == 1)
1515       {
1516         if( picNumLXPred + ( abs_diff_pic_num_minus1[i] + 1 )  >=  maxPicNum )
1517           picNumLXNoWrap = picNumLXPred + ( abs_diff_pic_num_minus1[i] + 1 ) - maxPicNum;
1518         else
1519           picNumLXNoWrap = picNumLXPred + ( abs_diff_pic_num_minus1[i] + 1 );
1520       }
1521       picNumLXPred = picNumLXNoWrap;
1522 
1523       if( picNumLXNoWrap > currPicNum )
1524         picNumLX = picNumLXNoWrap - maxPicNum;
1525       else
1526         picNumLX = picNumLXNoWrap;
1527 
1528 #if (MVC_EXTENSION_ENABLE)
1529       reorder_short_term(currSlice, cur_list, num_ref_idx_lX_active_minus1, picNumLX, &refIdxLX, -1);
1530 #else
1531       reorder_short_term(currSlice, cur_list, num_ref_idx_lX_active_minus1, picNumLX, &refIdxLX);
1532 #endif
1533     }
1534     else //(modification_of_pic_nums_idc[i] == 2)
1535     {
1536 #if (MVC_EXTENSION_ENABLE)
1537       reorder_long_term(currSlice, currSlice->listX[cur_list], num_ref_idx_lX_active_minus1, long_term_pic_idx[i], &refIdxLX, -1);
1538 #else
1539       reorder_long_term(currSlice, currSlice->listX[cur_list], num_ref_idx_lX_active_minus1, long_term_pic_idx[i], &refIdxLX);
1540 #endif
1541     }
1542 
1543   }
1544   // that's a definition
1545   currSlice->listXsize[cur_list] = (char) (num_ref_idx_lX_active_minus1 + 1);
1546 }
1547 
1548 
1549 
1550 
1551 /*!
1552  ************************************************************************
1553  * \brief
1554  *    Perform Memory management for idr pictures
1555  *
1556  ************************************************************************
1557  */
idr_memory_management(DecodedPictureBuffer * p_Dpb,StorablePicture * p)1558 void idr_memory_management(DecodedPictureBuffer *p_Dpb, StorablePicture* p)
1559 {
1560   uint32 i;
1561 
1562   if (p->no_output_of_prior_pics_flag)
1563   {
1564     // free all stored pictures
1565     for (i=0; i<p_Dpb->used_size; i++)
1566     {
1567       // reset all reference settings
1568       free_frame_store(p_Dpb->fs[i]);
1569       p_Dpb->fs[i] = alloc_frame_store();
1570     }
1571     for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++)
1572     {
1573       p_Dpb->fs_ref[i]=NULL;
1574     }
1575     for (i=0; i<p_Dpb->ltref_frames_in_buffer; i++)
1576     {
1577       p_Dpb->fs_ltref[i]=NULL;
1578     }
1579     p_Dpb->used_size=0;
1580   }
1581   else
1582   {
1583     flush_dpb(p_Dpb);
1584   }
1585   p_Dpb->last_picture = NULL;
1586 
1587   update_ref_list(p_Dpb);
1588   update_ltref_list(p_Dpb);
1589   p_Dpb->last_output_poc = INT_MIN;
1590 
1591   if (p->long_term_reference_flag)
1592   {
1593     p_Dpb->max_long_term_pic_idx = 0;
1594     p->is_long_term           = 1;
1595     p->long_term_frame_idx    = 0;
1596   }
1597   else
1598   {
1599     p_Dpb->max_long_term_pic_idx = -1;
1600     p->is_long_term           = 0;
1601   }
1602 
1603 #if (MVC_EXTENSION_ENABLE)
1604   p_Dpb->last_output_view_id = -1;
1605 #endif
1606 
1607 }
1608 
1609 /*!
1610  ************************************************************************
1611  * \brief
1612  *    Perform Sliding window decoded reference picture marking process
1613  *
1614  ************************************************************************
1615  */
sliding_window_memory_management(DecodedPictureBuffer * p_Dpb,StorablePicture * p)1616 static void sliding_window_memory_management(DecodedPictureBuffer *p_Dpb, StorablePicture* p)
1617 {
1618   uint32 i;
1619 
1620   assert (!p->idr_flag);
1621 
1622   // if this is a reference pic with sliding window, unmark first ref frame
1623   if (p_Dpb->ref_frames_in_buffer == imax(1, p_Dpb->num_ref_frames) - p_Dpb->ltref_frames_in_buffer)
1624   {
1625     for (i = 0; i < p_Dpb->used_size; i++)
1626     {
1627       if (p_Dpb->fs[i]->is_reference && (!(p_Dpb->fs[i]->is_long_term)))
1628       {
1629         unmark_for_reference(p_Dpb->fs[i]);
1630         update_ref_list(p_Dpb);
1631         break;
1632       }
1633     }
1634   }
1635 
1636   p->is_long_term = 0;
1637 }
1638 
1639 
1640 /*!
1641  ************************************************************************
1642  * \brief
1643  *    Perform Adaptive memory control decoded reference picture marking process
1644  ************************************************************************
1645  */
adaptive_memory_management(DecodedPictureBuffer * p_Dpb,StorablePicture * p)1646 static void adaptive_memory_management(DecodedPictureBuffer *p_Dpb, StorablePicture* p)
1647 {
1648   DecRefPicMarking_t *tmp_drpm;
1649   VideoParameters *p_Vid = p_Dpb->p_Vid;
1650 
1651   p_Vid->last_has_mmco_5 = 0;
1652 
1653   assert (!p->idr_flag);
1654   assert (p->adaptive_ref_pic_buffering_flag);
1655 
1656   while (p->dec_ref_pic_marking_buffer)
1657   {
1658     tmp_drpm = p->dec_ref_pic_marking_buffer;
1659     switch (tmp_drpm->memory_management_control_operation)
1660     {
1661       case 0:
1662         if (tmp_drpm->Next != NULL)
1663         {
1664           error ("memory_management_control_operation = 0 not last operation in buffer", 500);
1665         }
1666         break;
1667       case 1:
1668         mm_unmark_short_term_for_reference(p_Dpb, p, tmp_drpm->difference_of_pic_nums_minus1);
1669         update_ref_list(p_Dpb);
1670         break;
1671       case 2:
1672         mm_unmark_long_term_for_reference(p_Dpb, p, tmp_drpm->long_term_pic_num);
1673         update_ltref_list(p_Dpb);
1674         break;
1675       case 3:
1676         mm_assign_long_term_frame_idx(p_Dpb, p, tmp_drpm->difference_of_pic_nums_minus1, tmp_drpm->long_term_frame_idx);
1677         update_ref_list(p_Dpb);
1678         update_ltref_list(p_Dpb);
1679         break;
1680       case 4:
1681         mm_update_max_long_term_frame_idx (p_Dpb, tmp_drpm->max_long_term_frame_idx_plus1);
1682         update_ltref_list(p_Dpb);
1683         break;
1684       case 5:
1685         mm_unmark_all_short_term_for_reference(p_Dpb);
1686         mm_unmark_all_long_term_for_reference(p_Dpb);
1687         p_Vid->last_has_mmco_5 = 1;
1688         break;
1689       case 6:
1690         mm_mark_current_picture_long_term(p_Dpb, p, tmp_drpm->long_term_frame_idx);
1691         check_num_ref(p_Dpb);
1692         break;
1693       default:
1694         error ("invalid memory_management_control_operation in buffer", 500);
1695     }
1696     p->dec_ref_pic_marking_buffer = tmp_drpm->Next;
1697     free (tmp_drpm);
1698   }
1699   if ( p_Vid->last_has_mmco_5 )
1700   {
1701     p->pic_num = p->frame_num = 0;
1702 
1703     switch (p->structure)
1704     {
1705     case TOP_FIELD:
1706       {
1707         //p->poc = p->top_poc = p_Vid->toppoc =0;
1708         p->poc = p->top_poc = 0;
1709         break;
1710       }
1711     case BOTTOM_FIELD:
1712       {
1713         //p->poc = p->bottom_poc = p_Vid->bottompoc = 0;
1714         p->poc = p->bottom_poc = 0;
1715         break;
1716       }
1717     case FRAME:
1718       {
1719         p->top_poc    -= p->poc;
1720         p->bottom_poc -= p->poc;
1721 
1722         //p_Vid->toppoc = p->top_poc;
1723         //p_Vid->bottompoc = p->bottom_poc;
1724 
1725         p->poc = imin (p->top_poc, p->bottom_poc);
1726         //p_Vid->framepoc = p->poc;
1727         break;
1728       }
1729     }
1730     //currSlice->ThisPOC = p->poc;
1731 #if (MVC_EXTENSION_ENABLE)
1732     if(p->view_id == 0)
1733     {
1734       flush_dpb(p_Vid->p_Dpb_layer[0]);
1735       flush_dpb(p_Vid->p_Dpb_layer[1]);
1736     }
1737     else
1738     {
1739       flush_dpb(p_Dpb);
1740     }
1741 #else
1742     flush_dpb(p_Dpb);
1743 #endif
1744   }
1745 }
1746 
1747 
1748 /*!
1749  ************************************************************************
1750  * \brief
1751  *    Store a picture in DPB. This includes cheking for space in DPB and
1752  *    flushing frames.
1753  *    If we received a frame, we need to check for a new store, if we
1754  *    got a field, check if it's the second field of an already allocated
1755  *    store.
1756  *
1757  * \param p_Vid
1758  *    VideoParameters
1759  * \param p
1760  *    Picture to be stored
1761  *
1762  ************************************************************************
1763  */
store_picture_in_dpb(DecodedPictureBuffer * p_Dpb,StorablePicture * p)1764 void store_picture_in_dpb(DecodedPictureBuffer *p_Dpb, StorablePicture* p)
1765 {
1766   VideoParameters *p_Vid = p_Dpb->p_Vid;
1767   unsigned i;
1768   int poc, pos;
1769   // picture error concealment
1770 
1771   // diagnostics
1772   //printf ("Storing (%s) non-ref pic with frame_num #%d\n", (p->type == FRAME)?"FRAME":(p->type == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", p->pic_num);
1773   // if frame, check for new store,
1774   assert (p!=NULL);
1775 
1776   p_Vid->last_has_mmco_5=0;
1777   p_Vid->last_pic_bottom_field = (p->structure == BOTTOM_FIELD);
1778 
1779   if (p->idr_flag)
1780   {
1781     idr_memory_management(p_Dpb, p);
1782   // picture error concealment
1783     memset(p_Vid->pocs_in_dpb, 0, sizeof(int)*100);
1784   }
1785   else
1786   {
1787     // adaptive memory management
1788     if (p->used_for_reference && (p->adaptive_ref_pic_buffering_flag))
1789       adaptive_memory_management(p_Dpb, p);
1790   }
1791 
1792   if ((p->structure==TOP_FIELD)||(p->structure==BOTTOM_FIELD))
1793   {
1794     // check for frame store with same pic_number
1795     if (p_Dpb->last_picture)
1796     {
1797       if ((int)p_Dpb->last_picture->frame_num == p->pic_num)
1798       {
1799         if (((p->structure==TOP_FIELD)&&(p_Dpb->last_picture->is_used==2))||((p->structure==BOTTOM_FIELD)&&(p_Dpb->last_picture->is_used==1)))
1800         {
1801           if ((p->used_for_reference && (p_Dpb->last_picture->is_orig_reference!=0))||
1802               (!p->used_for_reference && (p_Dpb->last_picture->is_orig_reference==0)))
1803           {
1804             insert_picture_in_dpb(p_Vid, p_Dpb->last_picture, p);
1805             update_ref_list(p_Dpb);
1806             update_ltref_list(p_Dpb);
1807             dump_dpb(p_Dpb);
1808             p_Dpb->last_picture = NULL;
1809             return;
1810           }
1811         }
1812       }
1813     }
1814   }
1815 
1816   // this is a frame or a field which has no stored complementary field
1817 
1818   // sliding window, if necessary
1819   if ((!p->idr_flag)&&(p->used_for_reference && (!p->adaptive_ref_pic_buffering_flag)))
1820   {
1821     sliding_window_memory_management(p_Dpb, p);
1822   }
1823 
1824   // picture error concealment
1825   if(p_Vid->conceal_mode != 0)
1826   {
1827     for(i=0;i<p_Dpb->size;i++)
1828       if(p_Dpb->fs[i]->is_reference)
1829         p_Dpb->fs[i]->concealment_reference = 1;
1830   }
1831 
1832   // first try to remove unused frames
1833   if (p_Dpb->used_size==p_Dpb->size)
1834   {
1835     // picture error concealment
1836     if (p_Vid->conceal_mode != 0)
1837       conceal_non_ref_pics(p_Dpb, 2);
1838 
1839     remove_unused_frame_from_dpb(p_Dpb);
1840 
1841     if(p_Vid->conceal_mode != 0)
1842       sliding_window_poc_management(p_Dpb, p);
1843   }
1844 
1845   // then output frames until one can be removed
1846   while (p_Dpb->used_size == p_Dpb->size)
1847   {
1848     // non-reference frames may be output directly
1849     if (!p->used_for_reference)
1850     {
1851       get_smallest_poc(p_Dpb, &poc, &pos);
1852       if ((-1==pos) || (p->poc < poc))
1853       {
1854 #if (_DEBUG && MVC_EXTENSION_ENABLE)
1855         if((p_Vid->profile_idc >= MVC_HIGH))
1856           printf("Display order might not be correct, %d, %d\n", p->view_id, p->poc);
1857 #endif
1858 #if (MVC_EXTENSION_ENABLE)
1859         direct_output(p_Vid, p, p_Vid->p_out_mvc[p_Dpb->layer_id]);
1860 #else
1861         direct_output(p_Vid, p, p_Vid->p_out);
1862 #endif
1863         return;
1864       }
1865     }
1866     // flush a frame
1867     output_one_frame_from_dpb(p_Dpb);
1868   }
1869 
1870   // check for duplicate frame number in short term reference buffer
1871   if ((p->used_for_reference)&&(!p->is_long_term))
1872   {
1873     for (i=0; i<p_Dpb->ref_frames_in_buffer; i++)
1874     {
1875       if (p_Dpb->fs_ref[i]->frame_num == p->frame_num)
1876       {
1877         error("duplicate frame_num in short-term reference picture buffer", 500);
1878       }
1879     }
1880   }
1881   // store at end of buffer
1882   insert_picture_in_dpb(p_Vid, p_Dpb->fs[p_Dpb->used_size],p);
1883 
1884   // picture error concealment
1885   if (p->idr_flag)
1886   {
1887     p_Vid->earlier_missing_poc = 0;
1888   }
1889 
1890   if (p->structure != FRAME)
1891   {
1892     p_Dpb->last_picture = p_Dpb->fs[p_Dpb->used_size];
1893   }
1894   else
1895   {
1896     p_Dpb->last_picture = NULL;
1897   }
1898 
1899   p_Dpb->used_size++;
1900 
1901   if(p_Vid->conceal_mode != 0)
1902     p_Vid->pocs_in_dpb[p_Dpb->used_size-1] = p->poc;
1903 
1904   update_ref_list(p_Dpb);
1905   update_ltref_list(p_Dpb);
1906 
1907   check_num_ref(p_Dpb);
1908 
1909   dump_dpb(p_Dpb);
1910 }
1911 
1912 
1913 /*!
1914  ************************************************************************
1915  * \brief
1916  *    Insert the picture into the DPB. A free DPB position is necessary
1917  *    for frames, .
1918  *
1919  * \param p_Vid
1920  *    VideoParameters
1921  * \param fs
1922  *    FrameStore into which the picture will be inserted
1923  * \param p
1924  *    StorablePicture to be inserted
1925  *
1926  ************************************************************************
1927  */
insert_picture_in_dpb(VideoParameters * p_Vid,FrameStore * fs,StorablePicture * p)1928 static void insert_picture_in_dpb(VideoParameters *p_Vid, FrameStore* fs, StorablePicture* p)
1929 {
1930   InputParameters *p_Inp = p_Vid->p_Inp;
1931 //  printf ("insert (%s) pic with frame_num #%d, poc %d\n", (p->structure == FRAME)?"FRAME":(p->structure == TOP_FIELD)?"TOP_FIELD":"BOTTOM_FIELD", p->pic_num, p->poc);
1932   assert (p!=NULL);
1933   assert (fs!=NULL);
1934   switch (p->structure)
1935   {
1936   case FRAME:
1937     fs->frame = p;
1938     fs->is_used = 3;
1939     if (p->used_for_reference)
1940     {
1941       fs->is_reference = 3;
1942       fs->is_orig_reference = 3;
1943       if (p->is_long_term)
1944       {
1945         fs->is_long_term = 3;
1946         fs->long_term_frame_idx = p->long_term_frame_idx;
1947       }
1948     }
1949     fs->layer_id = p->layer_id;
1950 #if (MVC_EXTENSION_ENABLE)
1951     fs->view_id = p->view_id;
1952     fs->inter_view_flag[0] = fs->inter_view_flag[1] = p->inter_view_flag;
1953     fs->anchor_pic_flag[0] = fs->anchor_pic_flag[1] = p->anchor_pic_flag;
1954 #endif
1955     // generate field views
1956     dpb_split_field(p_Vid, fs);
1957     break;
1958   case TOP_FIELD:
1959     fs->top_field = p;
1960     fs->is_used |= 1;
1961     fs->layer_id = p->layer_id;
1962 #if (MVC_EXTENSION_ENABLE)
1963     fs->view_id = p->view_id;
1964     fs->inter_view_flag[0] = p->inter_view_flag;
1965     fs->anchor_pic_flag[0] = p->anchor_pic_flag;
1966 #endif
1967     if (p->used_for_reference)
1968     {
1969       fs->is_reference |= 1;
1970       fs->is_orig_reference |= 1;
1971       if (p->is_long_term)
1972       {
1973         fs->is_long_term |= 1;
1974         fs->long_term_frame_idx = p->long_term_frame_idx;
1975       }
1976     }
1977     if (fs->is_used == 3)
1978     {
1979       // generate frame view
1980       dpb_combine_field(p_Vid, fs);
1981     }
1982     else
1983     {
1984       fs->poc = p->poc;
1985     }
1986     gen_field_ref_ids(p_Vid, p);
1987     break;
1988   case BOTTOM_FIELD:
1989     fs->bottom_field = p;
1990     fs->is_used |= 2;
1991     fs->layer_id = p->layer_id;
1992 #if (MVC_EXTENSION_ENABLE)
1993     fs->view_id = p->view_id;
1994     fs->inter_view_flag[1] = p->inter_view_flag;
1995     fs->anchor_pic_flag[1] = p->anchor_pic_flag;
1996 #endif
1997     if (p->used_for_reference)
1998     {
1999       fs->is_reference |= 2;
2000       fs->is_orig_reference |= 2;
2001       if (p->is_long_term)
2002       {
2003         fs->is_long_term |= 2;
2004         fs->long_term_frame_idx = p->long_term_frame_idx;
2005       }
2006     }
2007     if (fs->is_used == 3)
2008     {
2009       // generate frame view
2010       dpb_combine_field(p_Vid, fs);
2011     }
2012     else
2013     {
2014       fs->poc = p->poc;
2015     }
2016     gen_field_ref_ids(p_Vid, p);
2017     break;
2018   }
2019   fs->frame_num = p->pic_num;
2020   fs->recovery_frame = p->recovery_frame;
2021 
2022   fs->is_output = p->is_output;
2023 
2024   if (fs->is_used==3)
2025   {
2026     calculate_frame_no(p_Vid, p);
2027     if (-1 != p_Vid->p_ref && !p_Inp->silent)
2028       find_snr(p_Vid, fs->frame, &p_Vid->p_ref);
2029   }
2030 }
2031 
2032 
2033 /*!
2034  ************************************************************************
2035  * \brief
2036  *    remove one frame from DPB
2037  ************************************************************************
2038  */
remove_frame_from_dpb(DecodedPictureBuffer * p_Dpb,int pos)2039 void remove_frame_from_dpb(DecodedPictureBuffer *p_Dpb, int pos)
2040 {
2041   FrameStore* fs = p_Dpb->fs[pos];
2042   FrameStore* tmp;
2043   unsigned i;
2044 
2045   //printf ("remove frame with frame_num #%d\n", fs->frame_num);
2046   switch (fs->is_used)
2047   {
2048   case 3:
2049     free_storable_picture(fs->frame);
2050     free_storable_picture(fs->top_field);
2051     free_storable_picture(fs->bottom_field);
2052     fs->frame=NULL;
2053     fs->top_field=NULL;
2054     fs->bottom_field=NULL;
2055     break;
2056   case 2:
2057     free_storable_picture(fs->bottom_field);
2058     fs->bottom_field=NULL;
2059     break;
2060   case 1:
2061     free_storable_picture(fs->top_field);
2062     fs->top_field=NULL;
2063     break;
2064   case 0:
2065     break;
2066   default:
2067     error("invalid frame store type",500);
2068   }
2069   fs->is_used = 0;
2070   fs->is_long_term = 0;
2071   fs->is_reference = 0;
2072   fs->is_orig_reference = 0;
2073 
2074   // move empty framestore to end of buffer
2075   tmp = p_Dpb->fs[pos];
2076 
2077   for (i=pos; i<p_Dpb->used_size-1;i++)
2078   {
2079     p_Dpb->fs[i] = p_Dpb->fs[i+1];
2080   }
2081   p_Dpb->fs[p_Dpb->used_size-1] = tmp;
2082   p_Dpb->used_size--;
2083 }
2084 
2085 
2086 
2087 
2088 /*!
2089  ************************************************************************
2090  * \brief
2091  *    Output one picture stored in the DPB.
2092  ************************************************************************
2093  */
output_one_frame_from_dpb(DecodedPictureBuffer * p_Dpb)2094 static int output_one_frame_from_dpb(DecodedPictureBuffer *p_Dpb)
2095 {
2096   VideoParameters *p_Vid = p_Dpb->p_Vid;
2097   int poc, pos;
2098   //diagnostics
2099   if (p_Dpb->used_size < 1)
2100   {
2101     error("Cannot output frame, DPB empty.",150);
2102   }
2103 
2104   // find smallest POC
2105   get_smallest_poc(p_Dpb, &poc, &pos);
2106 
2107   if(pos==-1)
2108   {
2109     return 0;
2110   }
2111 
2112   // call the output function
2113 //  printf ("output frame with frame_num #%d, poc %d (dpb. p_Dpb->size=%d, p_Dpb->used_size=%d)\n", p_Dpb->fs[pos]->frame_num, p_Dpb->fs[pos]->frame->poc, p_Dpb->size, p_Dpb->used_size);
2114 
2115   // picture error concealment
2116   if(p_Vid->conceal_mode != 0)
2117   {
2118     if(p_Dpb->last_output_poc == 0)
2119     {
2120       write_lost_ref_after_idr(p_Dpb, pos);
2121     }
2122 #if (MVC_EXTENSION_ENABLE)
2123     write_lost_non_ref_pic(p_Dpb, poc, p_Vid->p_out_mvc[p_Dpb->layer_id]);
2124 #else
2125     write_lost_non_ref_pic(p_Dpb, poc, p_Vid->p_out);
2126 #endif
2127   }
2128 
2129 // JVT-P072 ends
2130 
2131 #if (MVC_EXTENSION_ENABLE)
2132   write_stored_frame(p_Vid, p_Dpb->fs[pos], p_Vid->p_out_mvc[p_Dpb->layer_id]);
2133 #else
2134   write_stored_frame(p_Vid, p_Dpb->fs[pos], p_Vid->p_out);
2135 #endif
2136 
2137   // picture error concealment
2138   if(p_Vid->conceal_mode == 0)
2139   {
2140     if (p_Dpb->last_output_poc >= poc)
2141     {
2142       error ("output POC must be in ascending order", 150);
2143     }
2144   }
2145 
2146   p_Dpb->last_output_poc = poc;
2147 
2148   // free frame store and move empty store to end of buffer
2149   if (!is_used_for_reference(p_Dpb->fs[pos]))
2150   {
2151     remove_frame_from_dpb(p_Dpb, pos);
2152   }
2153   return 1;
2154 }
2155 
2156 
2157 
2158 /*!
2159  ************************************************************************
2160  * \brief
2161  *    All stored picture are output. Should be called to empty the buffer
2162  ************************************************************************
2163  */
flush_dpb(DecodedPictureBuffer * p_Dpb)2164 void flush_dpb(DecodedPictureBuffer *p_Dpb)
2165 {
2166   VideoParameters *p_Vid = p_Dpb->p_Vid;
2167   uint32 i;
2168 
2169   // diagnostics
2170   // printf("Flush remaining frames from the dpb. p_Dpb->size=%d, p_Dpb->used_size=%d\n",p_Dpb->size,p_Dpb->used_size);
2171   if(!p_Dpb->init_done)
2172     return;
2173 //  if(p_Vid->conceal_mode == 0)
2174   if (p_Vid->conceal_mode != 0)
2175     conceal_non_ref_pics(p_Dpb, 0);
2176 
2177   // mark all frames unused
2178   for (i=0; i<p_Dpb->used_size; i++)
2179   {
2180 #if MVC_EXTENSION_ENABLE
2181     assert( p_Dpb->fs[i]->view_id == p_Dpb->layer_id);
2182 #endif
2183     unmark_for_reference (p_Dpb->fs[i]);
2184   }
2185 
2186   while (remove_unused_frame_from_dpb(p_Dpb)) ;
2187 
2188   // output frames in POC order
2189   while (p_Dpb->used_size && output_one_frame_from_dpb(p_Dpb)) ;
2190 
2191   p_Dpb->last_output_poc = INT_MIN;
2192 }
2193 
2194 #if (MVC_EXTENSION_ENABLE)
flush_dpbs(DecodedPictureBuffer ** p_Dpb_layers,int nLayers)2195 void flush_dpbs(DecodedPictureBuffer **p_Dpb_layers, int nLayers)
2196 {
2197   VideoParameters *p_Vid = p_Dpb_layers[0]->p_Vid;
2198   DecodedPictureBuffer *p_Dpb;
2199   int i, j, used_size;
2200 
2201   // diagnostics
2202   // printf("Flush remaining frames from the dpb. p_Dpb->size=%d, p_Dpb->used_size=%d\n",p_Dpb->size,p_Dpb->used_size);
2203 
2204 //  if(p_Vid->conceal_mode == 0)
2205   if (p_Vid->conceal_mode != 0)
2206   {
2207     conceal_non_ref_pics(p_Dpb_layers[0], 0);
2208   }
2209 
2210   // mark all frames unused
2211   for(j=0; j<nLayers; j++)
2212   {
2213     p_Dpb = p_Dpb_layers[j];
2214     if(p_Dpb->init_done)
2215     {
2216       for (i=0; i<(int)p_Dpb->used_size; i++)
2217       {
2218         unmark_for_reference (p_Dpb->fs[i]);
2219       }
2220       while (remove_unused_frame_from_dpb(p_Dpb)) ;
2221     }
2222   }
2223   // output frames in POC order
2224   used_size = p_Dpb_layers[0]->used_size;
2225   for(j=1; j<nLayers; j++)
2226   {
2227     p_Dpb = p_Dpb_layers[j];
2228     if(p_Dpb->init_done)
2229     {
2230       if(p_Dpb->used_size &&  (p_Dpb->used_size != used_size))
2231       {
2232         assert(!"DPB used_size is not equal!");
2233       }
2234       if((int)p_Dpb->used_size > used_size)
2235       {
2236         used_size = (int)p_Dpb->used_size;
2237       }
2238     }
2239   }
2240   while (used_size)
2241   {
2242     for(j=0; j<nLayers; j++)
2243     {
2244       p_Dpb = p_Dpb_layers[j];
2245       if(p_Dpb->used_size)
2246         output_one_frame_from_dpb(p_Dpb);
2247     }
2248     used_size--;
2249   }
2250   for(j=0; j<nLayers; j++)
2251   {
2252     p_Dpb = p_Dpb_layers[j];
2253     p_Dpb->last_output_poc = INT_MIN;
2254   }
2255 }
2256 #endif
2257 
gen_field_ref_ids(VideoParameters * p_Vid,StorablePicture * p)2258 static void gen_field_ref_ids(VideoParameters *p_Vid, StorablePicture *p)
2259 {
2260   int i,j;
2261    //! Generate Frame parameters from field information.
2262 
2263   //copy the list;
2264   for(j=0; j<p_Vid->iSliceNumOfCurrPic; j++)
2265   {
2266     if(p->listX[j][LIST_0])
2267     {
2268       p->listXsize[j][LIST_0] =  p_Vid->ppSliceList[j]->listXsize[LIST_0];
2269       for(i=0; i<p->listXsize[j][LIST_0]; i++)
2270         p->listX[j][LIST_0][i] = p_Vid->ppSliceList[j]->listX[LIST_0][i];
2271     }
2272     if(p->listX[j][LIST_1])
2273     {
2274       p->listXsize[j][LIST_1] =  p_Vid->ppSliceList[j]->listXsize[LIST_1];
2275       for(i=0; i<p->listXsize[j][LIST_1]; i++)
2276         p->listX[j][LIST_1][i] = p_Vid->ppSliceList[j]->listX[LIST_1][i];
2277     }
2278   }
2279 }
2280 
2281 /*!
2282  ************************************************************************
2283  * \brief
2284  *    Extract top field from a frame
2285  ************************************************************************
2286  */
dpb_split_field(VideoParameters * p_Vid,FrameStore * fs)2287 void dpb_split_field(VideoParameters *p_Vid, FrameStore *fs)
2288 {
2289   int i, j, ii, jj, jj4;
2290   int idiv,jdiv;
2291   int currentmb;
2292   int twosz16 = 2 * (fs->frame->size_x >> 4);
2293   StorablePicture *fs_top = NULL, *fs_btm = NULL;
2294   StorablePicture *frame = fs->frame;
2295 
2296   fs->poc = frame->poc;
2297 
2298   if (!frame->frame_mbs_only_flag)
2299   {
2300     fs_top = fs->top_field    = alloc_storable_picture(p_Vid, TOP_FIELD,    frame->size_x, frame->size_y, frame->size_x_cr, frame->size_y_cr, 1);
2301     fs_btm = fs->bottom_field = alloc_storable_picture(p_Vid, BOTTOM_FIELD, frame->size_x, frame->size_y, frame->size_x_cr, frame->size_y_cr, 1);
2302 
2303     for (i = 0; i < (frame->size_y >> 1); i++)
2304     {
2305       memcpy(fs_top->imgY[i], frame->imgY[i*2], frame->size_x*sizeof(imgpel));
2306     }
2307 
2308     for (i = 0; i< (frame->size_y_cr >> 1); i++)
2309     {
2310       memcpy(fs_top->imgUV[0][i], frame->imgUV[0][i*2], frame->size_x_cr*sizeof(imgpel));
2311       memcpy(fs_top->imgUV[1][i], frame->imgUV[1][i*2], frame->size_x_cr*sizeof(imgpel));
2312     }
2313 
2314     for (i = 0; i < (frame->size_y>>1); i++)
2315     {
2316       memcpy(fs_btm->imgY[i], frame->imgY[i*2 + 1], frame->size_x*sizeof(imgpel));
2317     }
2318 
2319     for (i = 0; i < (frame->size_y_cr>>1); i++)
2320     {
2321       memcpy(fs_btm->imgUV[0][i], frame->imgUV[0][i*2 + 1], frame->size_x_cr*sizeof(imgpel));
2322       memcpy(fs_btm->imgUV[1][i], frame->imgUV[1][i*2 + 1], frame->size_x_cr*sizeof(imgpel));
2323     }
2324 
2325     fs_top->poc = frame->top_poc;
2326     fs_btm->poc = frame->bottom_poc;
2327 
2328 #if (MVC_EXTENSION_ENABLE)
2329     fs_top->view_id = frame->view_id;
2330     fs_btm->view_id = frame->view_id;
2331 #endif
2332 
2333     fs_top->frame_poc =  frame->frame_poc;
2334 
2335     fs_top->bottom_poc = fs_btm->bottom_poc =  frame->bottom_poc;
2336     fs_top->top_poc    = fs_btm->top_poc    =  frame->top_poc;
2337     fs_btm->frame_poc  = frame->frame_poc;
2338 
2339     fs_top->used_for_reference = fs_btm->used_for_reference
2340                                       = frame->used_for_reference;
2341     fs_top->is_long_term = fs_btm->is_long_term
2342                                 = frame->is_long_term;
2343     fs->long_term_frame_idx = fs_top->long_term_frame_idx
2344                             = fs_btm->long_term_frame_idx
2345                             = frame->long_term_frame_idx;
2346 
2347     fs_top->coded_frame = fs_btm->coded_frame = 1;
2348     fs_top->mb_aff_frame_flag = fs_btm->mb_aff_frame_flag
2349                         = frame->mb_aff_frame_flag;
2350 
2351     frame->top_field    = fs_top;
2352     frame->bottom_field = fs_btm;
2353     frame->frame         = frame;
2354     fs_top->bottom_field = fs_btm;
2355     fs_top->frame        = frame;
2356     fs_top->top_field = fs_top;
2357     fs_btm->top_field = fs_top;
2358     fs_btm->frame     = frame;
2359     fs_btm->bottom_field = fs_btm;
2360 
2361 #if (MVC_EXTENSION_ENABLE)
2362     fs_top->view_id = fs_btm->view_id = fs->view_id;
2363     fs_top->inter_view_flag = fs->inter_view_flag[0];
2364     fs_btm->inter_view_flag = fs->inter_view_flag[1];
2365 #endif
2366 
2367     fs_top->chroma_format_idc = fs_btm->chroma_format_idc = frame->chroma_format_idc;
2368     fs_top->iCodingType = fs_btm->iCodingType = frame->iCodingType;
2369     if(frame->used_for_reference)
2370     {
2371       pad_dec_picture(p_Vid, fs_top);
2372       pad_dec_picture(p_Vid, fs_btm);
2373     }
2374   }
2375   else
2376   {
2377     fs->top_field       = NULL;
2378     fs->bottom_field    = NULL;
2379     frame->top_field    = NULL;
2380     frame->bottom_field = NULL;
2381     frame->frame = frame;
2382   }
2383 
2384   if (!frame->frame_mbs_only_flag)
2385   {
2386     if (frame->mb_aff_frame_flag)
2387     {
2388       PicMotionParamsOld *frm_motion = &frame->motion;
2389       for (j=0 ; j< (frame->size_y >> 3); j++)
2390       {
2391         jj = (j >> 2)*8 + (j & 0x03);
2392         jj4 = jj + 4;
2393         jdiv = (j >> 1);
2394         for (i=0 ; i < (frame->size_x>>2); i++)
2395         {
2396           idiv = (i >> 2);
2397 
2398           currentmb = twosz16*(jdiv >> 1)+ (idiv)*2 + (jdiv & 0x01);
2399           // Assign field mvs attached to MB-Frame buffer to the proper buffer
2400           if (frm_motion->mb_field[currentmb])
2401           {
2402             fs_btm->mv_info[j][i].mv[LIST_0] = frame->mv_info[jj4][i].mv[LIST_0];
2403             fs_btm->mv_info[j][i].mv[LIST_1] = frame->mv_info[jj4][i].mv[LIST_1];
2404             fs_btm->mv_info[j][i].ref_idx[LIST_0] = frame->mv_info[jj4][i].ref_idx[LIST_0];
2405             if(fs_btm->mv_info[j][i].ref_idx[LIST_0] >=0)
2406               fs_btm->mv_info[j][i].ref_pic[LIST_0] = p_Vid->ppSliceList[frame->mv_info[jj4][i].slice_no]->listX[4][(short) fs_btm->mv_info[j][i].ref_idx[LIST_0]];
2407             else
2408               fs_btm->mv_info[j][i].ref_pic[LIST_0] = NULL;
2409             fs_btm->mv_info[j][i].ref_idx[LIST_1] = frame->mv_info[jj4][i].ref_idx[LIST_1];
2410             if(fs_btm->mv_info[j][i].ref_idx[LIST_1] >=0)
2411               fs_btm->mv_info[j][i].ref_pic[LIST_1] = p_Vid->ppSliceList[frame->mv_info[jj4][i].slice_no]->listX[5][(short) fs_btm->mv_info[j][i].ref_idx[LIST_1]];
2412             else
2413               fs_btm->mv_info[j][i].ref_pic[LIST_1] = NULL;
2414 
2415             fs_top->mv_info[j][i].mv[LIST_0] = frame->mv_info[jj][i].mv[LIST_0];
2416             fs_top->mv_info[j][i].mv[LIST_1] = frame->mv_info[jj][i].mv[LIST_1];
2417             fs_top->mv_info[j][i].ref_idx[LIST_0] = frame->mv_info[jj][i].ref_idx[LIST_0];
2418             if(fs_top->mv_info[j][i].ref_idx[LIST_0] >=0)
2419               fs_top->mv_info[j][i].ref_pic[LIST_0] = p_Vid->ppSliceList[frame->mv_info[jj][i].slice_no]->listX[2][(short) fs_top->mv_info[j][i].ref_idx[LIST_0]];
2420             else
2421               fs_top->mv_info[j][i].ref_pic[LIST_0] = NULL;
2422             fs_top->mv_info[j][i].ref_idx[LIST_1] = frame->mv_info[jj][i].ref_idx[LIST_1];
2423             if(fs_top->mv_info[j][i].ref_idx[LIST_1] >=0)
2424               fs_top->mv_info[j][i].ref_pic[LIST_1] = p_Vid->ppSliceList[frame->mv_info[jj][i].slice_no]->listX[3][(short) fs_top->mv_info[j][i].ref_idx[LIST_1]];
2425             else
2426               fs_top->mv_info[j][i].ref_pic[LIST_1] = NULL;
2427           }
2428         }
2429       }
2430     }
2431 
2432       //! Generate field MVs from Frame MVs
2433     for (j=0 ; j < (frame->size_y >> 3) ; j++)
2434     {
2435       jj = 2* RSD(j);
2436       jdiv = (j >> 1);
2437       for (i=0 ; i < (frame->size_x >> 2) ; i++)
2438       {
2439         ii = RSD(i);
2440         idiv = (i >> 2);
2441 
2442         currentmb = twosz16 * (jdiv >> 1)+ (idiv)*2 + (jdiv & 0x01);
2443 
2444         if (!frame->mb_aff_frame_flag  || !frame->motion.mb_field[currentmb])
2445         {
2446           fs_top->mv_info[j][i].mv[LIST_0] = fs_btm->mv_info[j][i].mv[LIST_0] = frame->mv_info[jj][ii].mv[LIST_0];
2447           fs_top->mv_info[j][i].mv[LIST_1] = fs_btm->mv_info[j][i].mv[LIST_1] = frame->mv_info[jj][ii].mv[LIST_1];
2448 
2449           // Scaling of references is done here since it will not affect spatial direct (2*0 =0)
2450           if (frame->mv_info[jj][ii].ref_idx[LIST_0] == -1)
2451           {
2452             fs_top->mv_info[j][i].ref_idx[LIST_0] = fs_btm->mv_info[j][i].ref_idx[LIST_0] = - 1;
2453             fs_top->mv_info[j][i].ref_pic[LIST_0] = fs_btm->mv_info[j][i].ref_pic[LIST_0] = NULL;
2454           }
2455           else
2456           {
2457             fs_top->mv_info[j][i].ref_idx[LIST_0] = fs_btm->mv_info[j][i].ref_idx[LIST_0] = frame->mv_info[jj][ii].ref_idx[LIST_0];
2458             fs_top->mv_info[j][i].ref_pic[LIST_0] = fs_btm->mv_info[j][i].ref_pic[LIST_0] = p_Vid->ppSliceList[frame->mv_info[jj][ii].slice_no]->listX[LIST_0][(short) frame->mv_info[jj][ii].ref_idx[LIST_0]];
2459           }
2460 
2461           if (frame->mv_info[jj][ii].ref_idx[LIST_1] == -1)
2462           {
2463             fs_top->mv_info[j][i].ref_idx[LIST_1] = fs_btm->mv_info[j][i].ref_idx[LIST_1] = - 1;
2464             fs_top->mv_info[j][i].ref_pic[LIST_1] = fs_btm->mv_info[j][i].ref_pic[LIST_1] = NULL;
2465           }
2466           else
2467           {
2468             fs_top->mv_info[j][i].ref_idx[LIST_1] = fs_btm->mv_info[j][i].ref_idx[LIST_1] = frame->mv_info[jj][ii].ref_idx[LIST_1];
2469             fs_top->mv_info[j][i].ref_pic[LIST_1] = fs_btm->mv_info[j][i].ref_pic[LIST_1] = p_Vid->ppSliceList[frame->mv_info[jj][ii].slice_no]->listX[LIST_1][(short) frame->mv_info[jj][ii].ref_idx[LIST_1]];
2470           }
2471         }
2472       }
2473     }
2474   }
2475 }
2476 
2477 
2478 /*!
2479  ************************************************************************
2480  * \brief
2481  *    Generate a frame from top and bottom fields,
2482  *    YUV components and display information only
2483  ************************************************************************
2484  */
dpb_combine_field_yuv(VideoParameters * p_Vid,FrameStore * fs)2485 void dpb_combine_field_yuv(VideoParameters *p_Vid, FrameStore *fs)
2486 {
2487   int i, j;
2488 
2489   if (!fs->frame)
2490   {
2491     fs->frame = alloc_storable_picture(p_Vid, 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, 1);
2492   }
2493 
2494   for (i=0; i<fs->top_field->size_y; i++)
2495   {
2496     memcpy(fs->frame->imgY[i*2],     fs->top_field->imgY[i]   , fs->top_field->size_x * sizeof(imgpel));     // top field
2497     memcpy(fs->frame->imgY[i*2 + 1], fs->bottom_field->imgY[i], fs->bottom_field->size_x * sizeof(imgpel)); // bottom field
2498   }
2499 
2500   for (j = 0; j < 2; j++)
2501   {
2502     for (i=0; i<fs->top_field->size_y_cr; i++)
2503     {
2504       memcpy(fs->frame->imgUV[j][i*2],     fs->top_field->imgUV[j][i],    fs->top_field->size_x_cr*sizeof(imgpel));
2505       memcpy(fs->frame->imgUV[j][i*2 + 1], fs->bottom_field->imgUV[j][i], fs->bottom_field->size_x_cr*sizeof(imgpel));
2506     }
2507   }
2508   fs->poc=fs->frame->poc =fs->frame->frame_poc = imin (fs->top_field->poc, fs->bottom_field->poc);
2509 
2510   fs->bottom_field->frame_poc=fs->top_field->frame_poc=fs->frame->poc;
2511 
2512   fs->bottom_field->top_poc=fs->frame->top_poc=fs->top_field->poc;
2513   fs->top_field->bottom_poc=fs->frame->bottom_poc=fs->bottom_field->poc;
2514 
2515   fs->frame->used_for_reference = (fs->top_field->used_for_reference && fs->bottom_field->used_for_reference );
2516   fs->frame->is_long_term = (fs->top_field->is_long_term && fs->bottom_field->is_long_term );
2517 
2518   if (fs->frame->is_long_term)
2519     fs->frame->long_term_frame_idx = fs->long_term_frame_idx;
2520 
2521   fs->frame->top_field    = fs->top_field;
2522   fs->frame->bottom_field = fs->bottom_field;
2523   fs->frame->frame = fs->frame;
2524 
2525   fs->frame->coded_frame = 0;
2526 
2527   fs->frame->chroma_format_idc = fs->top_field->chroma_format_idc;
2528   fs->frame->frame_cropping_flag = fs->top_field->frame_cropping_flag;
2529   if (fs->frame->frame_cropping_flag)
2530   {
2531     fs->frame->frame_crop_top_offset = fs->top_field->frame_crop_top_offset;
2532     fs->frame->frame_crop_bottom_offset = fs->top_field->frame_crop_bottom_offset;
2533     fs->frame->frame_crop_left_offset = fs->top_field->frame_crop_left_offset;
2534     fs->frame->frame_crop_right_offset = fs->top_field->frame_crop_right_offset;
2535   }
2536 
2537   fs->top_field->frame = fs->bottom_field->frame = fs->frame;
2538   fs->top_field->top_field = fs->top_field;
2539   fs->top_field->bottom_field = fs->bottom_field;
2540   fs->bottom_field->top_field = fs->top_field;
2541   fs->bottom_field->bottom_field = fs->bottom_field;
2542   if(fs->top_field->used_for_reference || fs->bottom_field->used_for_reference)
2543   {
2544     pad_dec_picture(p_Vid, fs->frame);
2545   }
2546 
2547 }
2548 
2549 
2550 /*!
2551  ************************************************************************
2552  * \brief
2553  *    Generate a frame from top and bottom fields
2554  ************************************************************************
2555  */
dpb_combine_field(VideoParameters * p_Vid,FrameStore * fs)2556 void dpb_combine_field(VideoParameters *p_Vid, FrameStore *fs)
2557 {
2558   int i,j, jj, jj4, k, l;
2559 
2560   dpb_combine_field_yuv(p_Vid, fs);
2561 
2562 #if (MVC_EXTENSION_ENABLE)
2563   fs->frame->view_id = fs->view_id;
2564 #endif
2565   fs->frame->iCodingType = fs->top_field->iCodingType; //FIELD_CODING;
2566    //! Use inference flag to remap mvs/references
2567 
2568   //! Generate Frame parameters from field information.
2569 
2570   for (j=0 ; j < (fs->top_field->size_y >> 2) ; j++)
2571   {
2572     jj = (j<<1);
2573     jj4 = jj + 1;
2574     for (i=0 ; i< (fs->top_field->size_x >> 2) ; i++)
2575     {
2576       fs->frame->mv_info[jj][i].mv[LIST_0] = fs->top_field->mv_info[j][i].mv[LIST_0];
2577       fs->frame->mv_info[jj][i].mv[LIST_1] = fs->top_field->mv_info[j][i].mv[LIST_1];
2578 
2579       fs->frame->mv_info[jj][i].ref_idx[LIST_0] = fs->top_field->mv_info[j][i].ref_idx[LIST_0];
2580       fs->frame->mv_info[jj][i].ref_idx[LIST_1] = fs->top_field->mv_info[j][i].ref_idx[LIST_1];
2581 
2582       /* bug: top field list doesnot exist.*/
2583       l = fs->top_field->mv_info[j][i].slice_no;
2584       k = fs->top_field->mv_info[j][i].ref_idx[LIST_0];
2585       fs->frame->mv_info[jj][i].ref_pic[LIST_0] = k>=0? fs->top_field->listX[l][LIST_0][k]: NULL;
2586       k = fs->top_field->mv_info[j][i].ref_idx[LIST_1];
2587       fs->frame->mv_info[jj][i].ref_pic[LIST_1] = k>=0? fs->top_field->listX[l][LIST_1][k]: NULL;
2588 
2589       //! association with id already known for fields.
2590       fs->frame->mv_info[jj4][i].mv[LIST_0] = fs->bottom_field->mv_info[j][i].mv[LIST_0];
2591       fs->frame->mv_info[jj4][i].mv[LIST_1] = fs->bottom_field->mv_info[j][i].mv[LIST_1];
2592 
2593       fs->frame->mv_info[jj4][i].ref_idx[LIST_0]  = fs->bottom_field->mv_info[j][i].ref_idx[LIST_0];
2594       fs->frame->mv_info[jj4][i].ref_idx[LIST_1]  = fs->bottom_field->mv_info[j][i].ref_idx[LIST_1];
2595       l = fs->bottom_field->mv_info[j][i].slice_no;
2596 
2597       k = fs->bottom_field->mv_info[j][i].ref_idx[LIST_0];
2598       fs->frame->mv_info[jj4][i].ref_pic[LIST_0] = k>=0? fs->bottom_field->listX[l][LIST_0][k]: NULL;
2599       k = fs->bottom_field->mv_info[j][i].ref_idx[LIST_1];
2600       fs->frame->mv_info[jj4][i].ref_pic[LIST_1] = k>=0? fs->bottom_field->listX[l][LIST_1][k]: NULL;
2601     }
2602   }
2603 }
2604 
2605 
2606 /*!
2607  ************************************************************************
2608  * \brief
2609  *    Allocate memory for buffering of reference picture reordering commands
2610  ************************************************************************
2611  */
alloc_ref_pic_list_reordering_buffer(Slice * currSlice)2612 void alloc_ref_pic_list_reordering_buffer(Slice *currSlice)
2613 {
2614   if (currSlice->slice_type != I_SLICE && currSlice->slice_type != SI_SLICE)
2615   {
2616     int size = currSlice->num_ref_idx_active[LIST_0] + 1;
2617     if ((currSlice->modification_of_pic_nums_idc[LIST_0] = calloc(size ,sizeof(int)))==NULL)
2618        no_mem_exit("alloc_ref_pic_list_reordering_buffer: modification_of_pic_nums_idc_l0");
2619     if ((currSlice->abs_diff_pic_num_minus1[LIST_0] = calloc(size,sizeof(int)))==NULL)
2620        no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_pic_num_minus1_l0");
2621     if ((currSlice->long_term_pic_idx[LIST_0] = calloc(size,sizeof(int)))==NULL)
2622        no_mem_exit("alloc_ref_pic_list_reordering_buffer: long_term_pic_idx_l0");
2623 #if (MVC_EXTENSION_ENABLE)
2624     if ((currSlice->abs_diff_view_idx_minus1[LIST_0] = calloc(size,sizeof(int)))==NULL)
2625        no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_view_idx_minus1_l0");
2626 #endif
2627   }
2628   else
2629   {
2630     currSlice->modification_of_pic_nums_idc[LIST_0] = NULL;
2631     currSlice->abs_diff_pic_num_minus1[LIST_0] = NULL;
2632     currSlice->long_term_pic_idx[LIST_0] = NULL;
2633 #if (MVC_EXTENSION_ENABLE)
2634     currSlice->abs_diff_view_idx_minus1[LIST_0] = NULL;
2635 #endif
2636   }
2637 
2638   if (currSlice->slice_type == B_SLICE)
2639   {
2640     int size = currSlice->num_ref_idx_active[LIST_1] + 1;
2641     if ((currSlice->modification_of_pic_nums_idc[LIST_1] = calloc(size,sizeof(int)))==NULL)
2642       no_mem_exit("alloc_ref_pic_list_reordering_buffer: modification_of_pic_nums_idc_l1");
2643     if ((currSlice->abs_diff_pic_num_minus1[LIST_1] = calloc(size,sizeof(int)))==NULL)
2644       no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_pic_num_minus1_l1");
2645     if ((currSlice->long_term_pic_idx[LIST_1] = calloc(size,sizeof(int)))==NULL)
2646       no_mem_exit("alloc_ref_pic_list_reordering_buffer: long_term_pic_idx_l1");
2647 #if (MVC_EXTENSION_ENABLE)
2648     if ((currSlice->abs_diff_view_idx_minus1[LIST_1] = calloc(size,sizeof(int)))==NULL)
2649       no_mem_exit("alloc_ref_pic_list_reordering_buffer: abs_diff_view_idx_minus1_l1");
2650 #endif
2651   }
2652   else
2653   {
2654     currSlice->modification_of_pic_nums_idc[LIST_1] = NULL;
2655     currSlice->abs_diff_pic_num_minus1[LIST_1] = NULL;
2656     currSlice->long_term_pic_idx[LIST_1] = NULL;
2657 #if (MVC_EXTENSION_ENABLE)
2658     currSlice->abs_diff_view_idx_minus1[LIST_1] = NULL;
2659 #endif
2660   }
2661 }
2662 
2663 
2664 /*!
2665  ************************************************************************
2666  * \brief
2667  *    Free memory for buffering of reference picture reordering commands
2668  ************************************************************************
2669  */
free_ref_pic_list_reordering_buffer(Slice * currSlice)2670 void free_ref_pic_list_reordering_buffer(Slice *currSlice)
2671 {
2672   if (currSlice->modification_of_pic_nums_idc[LIST_0])
2673     free(currSlice->modification_of_pic_nums_idc[LIST_0]);
2674   if (currSlice->abs_diff_pic_num_minus1[LIST_0])
2675     free(currSlice->abs_diff_pic_num_minus1[LIST_0]);
2676   if (currSlice->long_term_pic_idx[LIST_0])
2677     free(currSlice->long_term_pic_idx[LIST_0]);
2678 
2679   currSlice->modification_of_pic_nums_idc[LIST_0] = NULL;
2680   currSlice->abs_diff_pic_num_minus1[LIST_0] = NULL;
2681   currSlice->long_term_pic_idx[LIST_0] = NULL;
2682 
2683   if (currSlice->modification_of_pic_nums_idc[LIST_1])
2684     free(currSlice->modification_of_pic_nums_idc[LIST_1]);
2685   if (currSlice->abs_diff_pic_num_minus1[LIST_1])
2686     free(currSlice->abs_diff_pic_num_minus1[LIST_1]);
2687   if (currSlice->long_term_pic_idx[LIST_1])
2688     free(currSlice->long_term_pic_idx[LIST_1]);
2689 
2690   currSlice->modification_of_pic_nums_idc[LIST_1] = NULL;
2691   currSlice->abs_diff_pic_num_minus1[LIST_1] = NULL;
2692   currSlice->long_term_pic_idx[LIST_1] = NULL;
2693 
2694 #if (MVC_EXTENSION_ENABLE)
2695   if (currSlice->abs_diff_view_idx_minus1[LIST_0])
2696     free(currSlice->abs_diff_view_idx_minus1[LIST_0]);
2697   currSlice->abs_diff_view_idx_minus1[LIST_0] = NULL;
2698   if (currSlice->abs_diff_view_idx_minus1[LIST_1])
2699     free(currSlice->abs_diff_view_idx_minus1[LIST_1]);
2700   currSlice->abs_diff_view_idx_minus1[LIST_1] = NULL;
2701 #endif
2702 }
2703 
2704 /*!
2705  ************************************************************************
2706  * \brief
2707  *      Tian Dong
2708  *          June 13, 2002, Modified on July 30, 2003
2709  *
2710  *      If a gap in frame_num is found, try to fill the gap
2711  * \param p_Vid
2712  *    VideoParameters structure
2713  *
2714  ************************************************************************
2715  */
fill_frame_num_gap(VideoParameters * p_Vid,Slice * currSlice)2716 void fill_frame_num_gap(VideoParameters *p_Vid, Slice *currSlice)
2717 {
2718   seq_parameter_set_rbsp_t *active_sps = p_Vid->active_sps;
2719 
2720   int CurrFrameNum;
2721   int UnusedShortTermFrameNum;
2722   StorablePicture *picture = NULL;
2723   int tmp1 = currSlice->delta_pic_order_cnt[0];
2724   int tmp2 = currSlice->delta_pic_order_cnt[1];
2725   currSlice->delta_pic_order_cnt[0] = currSlice->delta_pic_order_cnt[1] = 0;
2726 
2727   printf("A gap in frame number is found, try to fill it.\n");
2728 
2729   UnusedShortTermFrameNum = (p_Vid->pre_frame_num + 1) % p_Vid->max_frame_num;
2730   CurrFrameNum = currSlice->frame_num; //p_Vid->frame_num;
2731 
2732   while (CurrFrameNum != UnusedShortTermFrameNum)
2733   {
2734     picture = alloc_storable_picture (p_Vid, FRAME, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, 1);
2735     picture->coded_frame = 1;
2736     picture->pic_num = UnusedShortTermFrameNum;
2737     picture->frame_num = UnusedShortTermFrameNum;
2738     picture->non_existing = 1;
2739     picture->is_output = 1;
2740     picture->used_for_reference = 1;
2741     picture->adaptive_ref_pic_buffering_flag = 0;
2742 #if (MVC_EXTENSION_ENABLE)
2743     picture->view_id = currSlice->view_id;
2744 #endif
2745 
2746     currSlice->frame_num = UnusedShortTermFrameNum;
2747     if (active_sps->pic_order_cnt_type!=0)
2748     {
2749       decode_poc(p_Vid, p_Vid->ppSliceList[0]);
2750     }
2751     picture->top_poc    = currSlice->toppoc;
2752     picture->bottom_poc = currSlice->bottompoc;
2753     picture->frame_poc  = currSlice->framepoc;
2754     picture->poc        = currSlice->framepoc;
2755 
2756     store_picture_in_dpb(currSlice->p_Dpb, picture);
2757 
2758     picture=NULL;
2759     p_Vid->pre_frame_num = UnusedShortTermFrameNum;
2760     UnusedShortTermFrameNum = (UnusedShortTermFrameNum + 1) % p_Vid->max_frame_num;
2761   }
2762   currSlice->delta_pic_order_cnt[0] = tmp1;
2763   currSlice->delta_pic_order_cnt[1] = tmp2;
2764   currSlice->frame_num = CurrFrameNum;
2765 }
2766 
2767 
2768 /*!
2769  ************************************************************************
2770  * \brief
2771  *    Compute co-located motion info
2772  *
2773  ************************************************************************
2774  */
compute_colocated(Slice * currSlice,StorablePicture ** listX[6])2775 void compute_colocated (Slice *currSlice, StorablePicture **listX[6])
2776 {
2777   int i, j;
2778 
2779   VideoParameters *p_Vid = currSlice->p_Vid;
2780 
2781   if (currSlice->direct_spatial_mv_pred_flag == 0)
2782   {
2783     for (j = 0; j < 2 + (currSlice->mb_aff_frame_flag * 4); j += 2)
2784     {
2785       for (i = 0; i < currSlice->listXsize[j];i++)
2786       {
2787         int prescale, iTRb, iTRp;
2788 
2789         if (j==0)
2790         {
2791           iTRb = iClip3( -128, 127, p_Vid->dec_picture->poc - listX[LIST_0 + j][i]->poc );
2792         }
2793         else if (j == 2)
2794         {
2795           iTRb = iClip3( -128, 127, p_Vid->dec_picture->top_poc - listX[LIST_0 + j][i]->poc );
2796         }
2797         else
2798         {
2799           iTRb = iClip3( -128, 127, p_Vid->dec_picture->bottom_poc - listX[LIST_0 + j][i]->poc );
2800         }
2801 
2802         iTRp = iClip3( -128, 127,  listX[LIST_1 + j][0]->poc - listX[LIST_0 + j][i]->poc);
2803 
2804         if (iTRp!=0)
2805         {
2806           prescale = ( 16384 + iabs( iTRp / 2 ) ) / iTRp;
2807           currSlice->mvscale[j][i] = iClip3( -1024, 1023, ( iTRb * prescale + 32 ) >> 6 ) ;
2808         }
2809         else
2810         {
2811           currSlice->mvscale[j][i] = 9999;
2812         }
2813       }
2814     }
2815   }
2816 }
2817 
2818 
2819 #if (MVC_EXTENSION_ENABLE)
GetMaxDecFrameBuffering(VideoParameters * p_Vid)2820 int GetMaxDecFrameBuffering(VideoParameters *p_Vid)
2821 {
2822   int i, j, iMax, iMax_1 = 0, iMax_2 = 0;
2823   subset_seq_parameter_set_rbsp_t *curr_subset_sps;
2824   seq_parameter_set_rbsp_t *curr_sps;
2825 
2826   curr_subset_sps = p_Vid->SubsetSeqParSet;
2827   curr_sps = p_Vid->SeqParSet;
2828   for(i=0; i<MAXSPS; i++)
2829   {
2830     if(curr_subset_sps->Valid && curr_subset_sps->sps.seq_parameter_set_id < MAXSPS)
2831     {
2832       j = curr_subset_sps->sps.max_dec_frame_buffering;
2833 
2834       if (curr_subset_sps->sps.vui_parameters_present_flag && curr_subset_sps->sps.vui_seq_parameters.bitstream_restriction_flag)
2835       {
2836         if ((int)curr_subset_sps->sps.vui_seq_parameters.max_dec_frame_buffering > j)
2837         {
2838           error ("max_dec_frame_buffering larger than MaxDpbSize", 500);
2839         }
2840         j = imax (1, curr_subset_sps->sps.vui_seq_parameters.max_dec_frame_buffering);
2841       }
2842 
2843       if(j > iMax_2)
2844         iMax_2 = j;
2845     }
2846 
2847     if(curr_sps->Valid)
2848     {
2849       j = curr_sps->max_dec_frame_buffering;
2850 
2851       if (curr_sps->vui_parameters_present_flag && curr_sps->vui_seq_parameters.bitstream_restriction_flag)
2852       {
2853         if ((int)curr_sps->vui_seq_parameters.max_dec_frame_buffering > j)
2854         {
2855           error ("max_dec_frame_buffering larger than MaxDpbSize", 500);
2856         }
2857         j = imax (1, curr_sps->vui_seq_parameters.max_dec_frame_buffering);
2858       }
2859 
2860       if(j > iMax_1)
2861         iMax_1 = j;
2862     }
2863     curr_subset_sps++;
2864     curr_sps++;
2865   }
2866 
2867   if (iMax_1 > 0 && iMax_2 > 0)
2868     iMax = iMax_1 + iMax_2;
2869   else
2870     iMax = (iMax_1 >0? iMax_1*2 : iMax_2*2);
2871   return iMax;
2872 }
2873 
is_view_id_in_ref_view_list(int view_id,int * ref_view_id,int num_ref_views)2874 static int is_view_id_in_ref_view_list(int view_id, int *ref_view_id, int num_ref_views)
2875 {
2876    int i;
2877    for(i=0; i<num_ref_views; i++)
2878    {
2879      if(view_id == ref_view_id[i])
2880        break;
2881    }
2882 
2883    return (num_ref_views && (i<num_ref_views));
2884 }
2885 
append_interview_list(DecodedPictureBuffer * p_Dpb,PictureStructure currPicStructure,int list_idx,FrameStore ** list,int * listXsize,int currPOC,int curr_view_id,int anchor_pic_flag)2886 void append_interview_list(DecodedPictureBuffer *p_Dpb,
2887                            PictureStructure currPicStructure, //0: frame; 1:top field; 2: bottom field;
2888                            int list_idx,
2889                            FrameStore **list,
2890                            int *listXsize,
2891                            int currPOC,
2892                            int curr_view_id,
2893                            int anchor_pic_flag)
2894 {
2895   VideoParameters *p_Vid = p_Dpb->p_Vid;
2896   int iVOIdx = curr_view_id;
2897   int pic_avail;
2898   int poc = 0;
2899   int fld_idx;
2900   int num_ref_views, *ref_view_id;
2901   FrameStore *fs = p_Dpb->fs_ilref[0];
2902 
2903 
2904   if(iVOIdx <0)
2905     printf("Error: iVOIdx: %d is not less than 0\n", iVOIdx);
2906 
2907   if(anchor_pic_flag)
2908   {
2909     num_ref_views = list_idx? p_Vid->active_subset_sps->num_anchor_refs_l1[iVOIdx] : p_Vid->active_subset_sps->num_anchor_refs_l0[iVOIdx];
2910     ref_view_id   = list_idx? p_Vid->active_subset_sps->anchor_ref_l1[iVOIdx]:p_Vid->active_subset_sps->anchor_ref_l0[iVOIdx];
2911   }
2912   else
2913   {
2914     num_ref_views = list_idx? p_Vid->active_subset_sps->num_non_anchor_refs_l1[iVOIdx] : p_Vid->active_subset_sps->num_non_anchor_refs_l0[iVOIdx];
2915     ref_view_id = list_idx? p_Vid->active_subset_sps->non_anchor_ref_l1[iVOIdx]:p_Vid->active_subset_sps->non_anchor_ref_l0[iVOIdx];
2916   }
2917 
2918   //  if(num_ref_views <= 0)
2919   //    printf("Error: iNumOfRefViews: %d is not larger than 0\n", num_ref_views);
2920 
2921   if(currPicStructure == BOTTOM_FIELD)
2922     fld_idx = 1;
2923   else
2924     fld_idx = 0;
2925 
2926     if(currPicStructure==FRAME)
2927     {
2928       pic_avail = (fs->is_used == 3);
2929       if (pic_avail)
2930         poc = fs->frame->poc;
2931     }
2932     else if(currPicStructure==TOP_FIELD)
2933     {
2934       pic_avail = fs->is_used & 1;
2935       if (pic_avail)
2936         poc = fs->top_field->poc;
2937     }
2938     else if(currPicStructure==BOTTOM_FIELD)
2939     {
2940       pic_avail = fs->is_used & 2;
2941       if (pic_avail)
2942         poc = fs->bottom_field->poc;
2943     }
2944     else
2945       pic_avail =0;
2946 
2947     if(pic_avail && fs->inter_view_flag[fld_idx])
2948     {
2949       if(poc == currPOC)
2950       {
2951         if(is_view_id_in_ref_view_list(fs->view_id, ref_view_id, num_ref_views))
2952         {
2953           //add one inter-view reference;
2954           list[*listXsize] = fs;
2955           //next;
2956           (*listXsize)++;
2957         }
2958       }
2959     }
2960 }
2961 
2962 #endif
2963 
process_picture_in_dpb_s(VideoParameters * p_Vid,StorablePicture * p_pic)2964 void process_picture_in_dpb_s(VideoParameters *p_Vid, StorablePicture *p_pic)
2965 {
2966   //InputParameters *p_Inp = p_Vid->p_Inp;
2967   ImageData *p_img_out = &p_Vid->tempData3;
2968   imgpel***  d_img;
2969   int i;
2970 
2971   if(p_Vid->tempData3.frm_data[0] == NULL)
2972     init_img_data( p_Vid, &(p_Vid->tempData3), p_Vid->active_sps);
2973 
2974   if (p_pic->structure == FRAME)
2975   {
2976     d_img = p_img_out->frm_data;
2977   }
2978   else //If reference picture is a field, then frm_data will actually contain field data and therefore top/bottom stride is set accordingly.
2979   {
2980     if (p_pic->structure == TOP_FIELD)
2981     {
2982       d_img = p_img_out->top_data;
2983     }
2984     else
2985     {
2986       d_img = p_img_out->bot_data;
2987     }
2988   }
2989 
2990   for(i=0; i<p_pic->size_y; i++)
2991     memcpy(d_img[0][i], p_pic->imgY[i], p_pic->size_x*sizeof(imgpel));
2992   if (p_Vid->yuv_format != YUV400)
2993   {
2994     for(i=0; i<p_pic->size_y_cr; i++)
2995       memcpy(d_img[1][i], p_pic->imgUV[0][i], p_pic->size_x_cr * sizeof(imgpel));
2996     for(i=0; i<p_pic->size_y_cr; i++)
2997       memcpy(d_img[2][i], p_pic->imgUV[1][i], p_pic->size_x_cr * sizeof(imgpel));
2998   }
2999 }
3000 
init_img_data(VideoParameters * p_Vid,ImageData * p_ImgData,seq_parameter_set_rbsp_t * sps)3001 int init_img_data(VideoParameters *p_Vid, ImageData *p_ImgData, seq_parameter_set_rbsp_t *sps)
3002 {
3003   InputParameters *p_Inp = p_Vid->p_Inp;
3004   int memory_size = 0;
3005   int nplane;
3006 
3007   // allocate memory for reference frame buffers: p_ImgData->frm_data
3008   p_ImgData->format           = p_Inp->output;
3009   p_ImgData->format.width[0]  = p_Vid->width;
3010   p_ImgData->format.width[1]  = p_Vid->width_cr;
3011   p_ImgData->format.width[2]  = p_Vid->width_cr;
3012   p_ImgData->format.height[0] = p_Vid->height;
3013   p_ImgData->format.height[1] = p_Vid->height_cr;
3014   p_ImgData->format.height[2] = p_Vid->height_cr;
3015   p_ImgData->format.yuv_format          = (ColorFormat) sps->chroma_format_idc;
3016   p_ImgData->format.auto_crop_bottom    = p_Inp->output.auto_crop_bottom;
3017   p_ImgData->format.auto_crop_right     = p_Inp->output.auto_crop_right;
3018   p_ImgData->format.auto_crop_bottom_cr = p_Inp->output.auto_crop_bottom_cr;
3019   p_ImgData->format.auto_crop_right_cr  = p_Inp->output.auto_crop_right_cr;
3020   p_ImgData->frm_stride[0]    = p_Vid->width;
3021   p_ImgData->frm_stride[1]    = p_ImgData->frm_stride[2] = p_Vid->width_cr;
3022   p_ImgData->top_stride[0] = p_ImgData->bot_stride[0] = p_ImgData->frm_stride[0] << 1;
3023   p_ImgData->top_stride[1] = p_ImgData->top_stride[2] = p_ImgData->bot_stride[1] = p_ImgData->bot_stride[2] = p_ImgData->frm_stride[1] << 1;
3024 
3025   if( sps->separate_colour_plane_flag )
3026   {
3027     for( nplane=0; nplane < MAX_PLANE; nplane++ )
3028     {
3029       memory_size += get_mem2Dpel(&(p_ImgData->frm_data[nplane]), p_Vid->height, p_Vid->width);
3030     }
3031   }
3032   else
3033   {
3034     memory_size += get_mem2Dpel(&(p_ImgData->frm_data[0]), p_Vid->height, p_Vid->width);
3035 
3036     if (p_Vid->yuv_format != YUV400)
3037     {
3038       int i, j, k;
3039       memory_size += get_mem2Dpel(&(p_ImgData->frm_data[1]), p_Vid->height_cr, p_Vid->width_cr);
3040       memory_size += get_mem2Dpel(&(p_ImgData->frm_data[2]), p_Vid->height_cr, p_Vid->width_cr);
3041 
3042       if (sizeof(imgpel) == sizeof(unsigned char))
3043       {
3044         for (k = 1; k < 3; k++)
3045           fast_memset(p_ImgData->frm_data[k][0], 128, p_Vid->height_cr * p_Vid->width_cr * sizeof(imgpel));
3046       }
3047       else
3048       {
3049         imgpel mean_val;
3050 
3051         for (k = 1; k < 3; k++)
3052         {
3053           mean_val = (imgpel) ((p_Vid->max_pel_value_comp[k] + 1) >> 1);
3054 
3055           for (j = 0; j < p_Vid->height_cr; j++)
3056           {
3057             for (i = 0; i < p_Vid->width_cr; i++)
3058               p_ImgData->frm_data[k][j][i] = mean_val;
3059           }
3060         }
3061       }
3062     }
3063   }
3064 
3065   if (!p_Vid->active_sps->frame_mbs_only_flag)
3066   {
3067     // allocate memory for field reference frame buffers
3068     memory_size += init_top_bot_planes(p_ImgData->frm_data[0], p_Vid->height, &(p_ImgData->top_data[0]), &(p_ImgData->bot_data[0]));
3069 
3070     if (p_Vid->yuv_format != YUV400)
3071     {
3072       memory_size += 4*(sizeof(imgpel**));
3073 
3074       memory_size += init_top_bot_planes(p_ImgData->frm_data[1], p_Vid->height_cr, &(p_ImgData->top_data[1]), &(p_ImgData->bot_data[1]));
3075       memory_size += init_top_bot_planes(p_ImgData->frm_data[2], p_Vid->height_cr, &(p_ImgData->top_data[2]), &(p_ImgData->bot_data[2]));
3076     }
3077   }
3078 
3079   return memory_size;
3080 }
3081 
free_img_data(VideoParameters * p_Vid,ImageData * p_ImgData)3082 void free_img_data(VideoParameters *p_Vid, ImageData *p_ImgData)
3083 {
3084   if ( p_Vid->separate_colour_plane_flag )
3085   {
3086     int nplane;
3087 
3088     for( nplane=0; nplane<MAX_PLANE; nplane++ )
3089     {
3090       if (p_ImgData->frm_data[nplane])
3091       {
3092         free_mem2Dpel(p_ImgData->frm_data[nplane]);      // free ref frame buffers
3093         p_ImgData->frm_data[nplane] = NULL;
3094       }
3095     }
3096   }
3097   else
3098   {
3099     if (p_ImgData->frm_data[0])
3100     {
3101       free_mem2Dpel(p_ImgData->frm_data[0]);      // free ref frame buffers
3102       p_ImgData->frm_data[0] = NULL;
3103     }
3104 
3105     if (p_ImgData->format.yuv_format != YUV400)
3106     {
3107       if (p_ImgData->frm_data[1])
3108       {
3109         free_mem2Dpel(p_ImgData->frm_data[1]);
3110         p_ImgData->frm_data[1] = NULL;
3111       }
3112       if (p_ImgData->frm_data[2])
3113       {
3114         free_mem2Dpel(p_ImgData->frm_data[2]);
3115         p_ImgData->frm_data[2] = NULL;
3116       }
3117     }
3118   }
3119 
3120   if (!p_Vid->active_sps->frame_mbs_only_flag)
3121   {
3122     free_top_bot_planes(p_ImgData->top_data[0], p_ImgData->bot_data[0]);
3123 
3124     if (p_ImgData->format.yuv_format != YUV400)
3125     {
3126       free_top_bot_planes(p_ImgData->top_data[1], p_ImgData->bot_data[1]);
3127       free_top_bot_planes(p_ImgData->top_data[2], p_ImgData->bot_data[2]);
3128     }
3129   }
3130 }
3131 
copy_img_data(imgpel * out_img,imgpel * in_img,int ostride,int istride,unsigned int size_y,unsigned int size_x)3132 static inline void copy_img_data(imgpel *out_img, imgpel *in_img, int ostride, int istride, unsigned int size_y, unsigned int size_x)
3133 {
3134   unsigned int i;
3135   for(i = 0; i < size_y; i++)
3136   {
3137     fast_memcpy(out_img, in_img, size_x);
3138     out_img += ostride;
3139     in_img += istride;
3140   }
3141 }
3142 
3143 /*!
3144  ************************************************************************
3145  * \brief
3146  *    Remove a picture from DPB which is no longer needed.
3147  ************************************************************************
3148  */
3149 #if !MVC_EXTENSION_ENABLE
remove_unused_proc_pic_from_dpb(DecodedPictureBuffer * p_Dpb)3150 int remove_unused_proc_pic_from_dpb(DecodedPictureBuffer *p_Dpb)
3151 {
3152   assert(!"The function is not available\n");
3153   return 0;
3154 }
3155 #endif
3156 
3157 /*!
3158  ************************************************************************
3159  * \brief
3160  *    Store a processed picture in DPB. This includes cheking for space in DPB and
3161  *    flushing frames.
3162  *    If we received a frame, we need to check for a new store, if we
3163  *    got a field, check if it's the second field of an already allocated
3164  *    store.
3165  *
3166  * \param p_Vid
3167  *    VideoParameters
3168  * \param p
3169  *    Picture to be stored
3170  *
3171  ************************************************************************
3172  */
3173 #if (MVC_EXTENSION_ENABLE)
store_proc_picture_in_dpb(DecodedPictureBuffer * p_Dpb,StorablePicture * p)3174 void store_proc_picture_in_dpb(DecodedPictureBuffer *p_Dpb, StorablePicture* p)
3175 {
3176   VideoParameters *p_Vid = p_Dpb->p_Vid;
3177   FrameStore *fs = p_Dpb->fs_ilref[0];
3178   if(p_Dpb->used_size_il>0 && fs->is_used==3)
3179   {
3180     //checking;
3181 #ifdef _DEBUG
3182     if(p->structure==FRAME)
3183       assert(fs->frame->frame_poc != p->poc);
3184     else if(p->structure==TOP_FIELD)
3185       assert(fs->top_field->top_poc != p->poc);
3186     else if(p->structure==BOTTOM_FIELD)
3187       assert(fs->bottom_field->bottom_poc != p->poc);
3188 #endif
3189     if(fs->frame)
3190     {
3191       free_storable_picture(fs->frame);
3192       fs->frame = NULL;
3193     }
3194     if(fs->top_field)
3195     {
3196       free_storable_picture(fs->top_field);
3197       fs->top_field = NULL;
3198     }
3199     if(fs->bottom_field)
3200     {
3201       free_storable_picture(fs->bottom_field);
3202       fs->bottom_field = NULL;
3203     }
3204     fs->is_used = 0;
3205     fs->is_reference = 0;
3206     p_Dpb->used_size_il--;
3207   }
3208 #ifdef _DEBUG
3209   if(fs->is_used>0)
3210   {
3211     //checking;
3212     if(p->structure==FRAME)
3213       assert(fs->frame == NULL);
3214     else if(p->structure==TOP_FIELD)
3215       assert(fs->top_field == NULL);
3216     else if(p->structure==BOTTOM_FIELD)
3217       assert(fs->bottom_field == NULL);
3218   }
3219 #endif
3220 
3221   insert_picture_in_dpb(p_Vid, fs, p);
3222   if((p->structure==FRAME && fs->is_used == 3) || (p->structure!=FRAME && fs->is_used && fs->is_used <3))
3223    p_Dpb->used_size_il++;
3224 }
3225 
3226 /*!
3227  ************************************************************************
3228  * \brief
3229  *    Clone an encoded frame picture structure
3230  ************************************************************************
3231  */
clone_storable_picture(VideoParameters * p_Vid,StorablePicture * p_pic)3232 StorablePicture * clone_storable_picture( VideoParameters *p_Vid, StorablePicture *p_pic )
3233 {
3234   int i, j;
3235   int nplane;
3236   int *istride = NULL;
3237   int ostride[2];
3238   imgpel ***img_in = NULL;
3239 
3240   StorablePicture *p_stored_pic = alloc_storable_picture (p_Vid, (PictureStructure) p_Vid->structure, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, 0);
3241 
3242   p_stored_pic->pic_num = p_pic->pic_num;
3243   p_stored_pic->frame_num = p_pic->frame_num;
3244   p_stored_pic->long_term_frame_idx = p_pic->long_term_frame_idx;
3245   p_stored_pic->long_term_pic_num = p_pic->long_term_pic_num;
3246   //p_stored_pic->used_for_reference = p_pic->used_for_reference;
3247   p_stored_pic->is_long_term = 0;
3248   p_stored_pic->non_existing = p_pic->non_existing;
3249   p_stored_pic->max_slice_id = p_pic->max_slice_id;
3250   p_stored_pic->structure = p_pic->structure;
3251   p_stored_pic->size_x = p_pic->size_x;
3252   p_stored_pic->size_y = p_pic->size_y;
3253   p_stored_pic->size_x_cr = p_pic->size_x_cr;
3254   p_stored_pic->size_y_cr = p_pic->size_y_cr;
3255   p_stored_pic->size_x_m1 = p_pic->size_x - 1;
3256   p_stored_pic->size_y_m1 = p_pic->size_y - 1;
3257   p_stored_pic->size_x_cr_m1 = p_pic->size_x_cr - 1;
3258   p_stored_pic->size_y_cr_m1 = p_pic->size_y_cr - 1;
3259 
3260   p_stored_pic->mb_aff_frame_flag = p_pic->mb_aff_frame_flag;
3261   p_stored_pic->seiHasTone_mapping = p_pic->seiHasTone_mapping;
3262   p_stored_pic->poc         = p_pic->poc;
3263   p_stored_pic->top_poc     = p_pic->top_poc;
3264   p_stored_pic->bottom_poc  = p_pic->bottom_poc;
3265   p_stored_pic->frame_poc   = p_pic->frame_poc;
3266   p_stored_pic->pic_num     = p_pic->pic_num;
3267   p_stored_pic->frame_num   = p_pic->frame_num;
3268   p_stored_pic->coded_frame = 1;
3269   p_stored_pic->qp = p_pic->qp;
3270   p_stored_pic->slice_qp_delta = p_pic->slice_qp_delta;
3271   p_stored_pic->chroma_qp_offset[0] = p_pic->chroma_qp_offset[0];
3272   p_stored_pic->chroma_qp_offset[1] = p_pic->chroma_qp_offset[1];
3273 
3274   p_stored_pic->slice_type = p_pic->slice_type;
3275   p_stored_pic->idr_flag = p_pic->idr_flag;
3276   p_stored_pic->no_output_of_prior_pics_flag = p_pic->no_output_of_prior_pics_flag;
3277   p_stored_pic->long_term_reference_flag = 0;
3278   p_stored_pic->adaptive_ref_pic_buffering_flag = 0;
3279   p_stored_pic->dec_ref_pic_marking_buffer = NULL;
3280   p_stored_pic->PicWidthInMbs = p_pic->PicWidthInMbs;
3281   p_stored_pic->recovery_frame = p_pic->recovery_frame;
3282   p_stored_pic->chroma_format_idc = p_pic->chroma_format_idc;
3283   p_stored_pic->frame_mbs_only_flag = p_pic->frame_mbs_only_flag;
3284   p_stored_pic->frame_cropping_flag = p_pic->frame_cropping_flag;
3285 
3286   if (p_stored_pic->frame_cropping_flag)
3287   {
3288     p_stored_pic->frame_crop_left_offset   = p_pic->frame_crop_left_offset;
3289     p_stored_pic->frame_crop_right_offset  = p_pic->frame_crop_right_offset;
3290     p_stored_pic->frame_crop_top_offset    = p_pic->frame_crop_top_offset;
3291     p_stored_pic->frame_crop_bottom_offset = p_pic->frame_crop_bottom_offset;
3292   }
3293 
3294   // store BL reconstruction
3295   //memcpy((void *)p_stored_pic->imgY[0], (void *)p_Vid->tempData3.frm_data[0][0], p_pic->size_x * p_pic->size_y * sizeof(imgpel));
3296 
3297   ostride[0] = p_stored_pic->iLumaStride;
3298   ostride[1] = p_stored_pic->iChromaStride;
3299   if (p_stored_pic->structure == FRAME)
3300   {
3301     istride = p_Vid->tempData3.frm_stride;
3302     img_in  = p_Vid->tempData3.frm_data;
3303   }
3304   else if (p_stored_pic->structure == TOP_FIELD)
3305   {
3306     istride = p_Vid->tempData3.top_stride;
3307     img_in  = p_Vid->tempData3.top_data;
3308   }
3309   else
3310   {
3311     istride = p_Vid->tempData3.bot_stride;
3312     img_in  = p_Vid->tempData3.bot_data;
3313   }
3314 
3315   copy_img_data(&p_stored_pic->imgY[0][0], &img_in[0][0][0], ostride[0], istride[0], p_pic->size_y, p_pic->size_x * sizeof(imgpel));
3316 
3317   pad_buf(*p_stored_pic->imgY, p_stored_pic->size_x, p_stored_pic->size_y, p_stored_pic->iLumaStride, p_Vid->iLumaPadX, p_Vid->iLumaPadY);
3318 
3319   if (p_Vid->active_sps->chroma_format_idc != YUV400)
3320   {
3321     //memcpy((void *)p_stored_pic->imgUV[0][0], (void *)p_Vid->tempData3.frm_data[1][0], p_pic->size_x_cr * p_pic->size_y_cr * sizeof(imgpel));
3322     //memcpy((void *)p_stored_pic->imgUV[1][0], (void *)p_Vid->tempData3.frm_data[2][0], p_pic->size_x_cr * p_pic->size_y_cr * sizeof(imgpel));
3323     copy_img_data(&p_stored_pic->imgUV[0][0][0], &img_in[1][0][0], ostride[1], istride[1], p_pic->size_y_cr, p_pic->size_x_cr*sizeof(imgpel));
3324     pad_buf(*p_stored_pic->imgUV[0], p_stored_pic->size_x_cr, p_stored_pic->size_y_cr, p_stored_pic->iChromaStride, p_Vid->iChromaPadX, p_Vid->iChromaPadY);
3325     copy_img_data(&p_stored_pic->imgUV[1][0][0], &img_in[2][0][0], ostride[1], istride[2], p_pic->size_y_cr, p_pic->size_x_cr*sizeof(imgpel));
3326     pad_buf(*p_stored_pic->imgUV[1], p_stored_pic->size_x_cr, p_stored_pic->size_y_cr, p_stored_pic->iChromaStride, p_Vid->iChromaPadX, p_Vid->iChromaPadY);
3327   }
3328 
3329   for (j = 0; j < (p_pic->size_y >> BLOCK_SHIFT); j++)
3330   {
3331     //PicMotionParams *mv_info = p_stored_pic->mv_info[j];
3332     char *ref_idx = p_stored_pic->mv_info[j][0].ref_idx;
3333     for (i = 0; i < (p_pic->size_x >> BLOCK_SHIFT); i++)
3334     {
3335       //*((short *)&((mv_info++)->ref_idx[LIST_0])) = -1;
3336       *((short *) ref_idx) = -1;
3337       ref_idx += sizeof(PicMotionParams);
3338     }
3339   }
3340 
3341   if( (p_Vid->separate_colour_plane_flag != 0) )
3342   {
3343     for( nplane=0; nplane<MAX_PLANE; nplane++ )
3344     {
3345       for (j = 0; j < (p_pic->size_y >> BLOCK_SHIFT); j++)
3346       {
3347         for (i = 0; i < (p_pic->size_x >> BLOCK_SHIFT); i++)
3348         {
3349           p_stored_pic->JVmv_info[nplane][j][i].ref_idx[LIST_0] = -1;
3350           p_stored_pic->JVmv_info[nplane][j][i].ref_idx[LIST_1] = -1;
3351         }
3352       }
3353     }
3354   }
3355 
3356   // MVC-related parameters
3357   p_stored_pic->inter_view_flag = p_pic->inter_view_flag;
3358   p_stored_pic->anchor_pic_flag = 0;
3359   p_stored_pic->view_id = 0;
3360   p_stored_pic->proc_flag = 1;
3361   p_stored_pic->is_output = 1;
3362   p_stored_pic->used_for_reference = 1;
3363 
3364   return p_stored_pic;
3365 }
3366 #endif
3367 
3368 
3369