1 
2 /*!
3  *************************************************************************************
4  * \file image.c
5  *
6  * \brief
7  *    Code one image/slice
8  *
9  * \author
10  *    Main contributors (see contributors.h for copyright, address and affiliation details)
11  *     - Inge Lille-Langoy               <inge.lille-langoy@telenor.com>
12  *     - Rickard Sjoberg                 <rickard.sjoberg@era.ericsson.se>
13  *     - Jani Lainema                    <jani.lainema@nokia.com>
14  *     - Sebastian Purreiter             <sebastian.purreiter@mch.siemens.de>
15  *     - Byeong-Moon Jeon                <jeonbm@lge.com>
16  *     - Yoon-Seong Soh                  <yunsung@lge.com>
17  *     - Thomas Stockhammer              <stockhammer@ei.tum.de>
18  *     - Detlev Marpe
19  *     - Guido Heising
20  *     - Thomas Wedi                     <wedi@tnt.uni-hannover.de>
21  *     - Ragip Kurceren                  <ragip.kurceren@nokia.com>
22  *     - Antti Hallapuro                 <antti.hallapuro@nokia.com>
23  *     - Alexis Michael Tourapis         <alexismt@ieee.org>
24  *     - Athanasios Leontaris            <aleon@dolby.com>
25  *     - Yan Ye                          <yye@dolby.com>
26  *************************************************************************************
27  */
28 #include "contributors.h"
29 
30 #include <math.h>
31 #include <time.h>
32 
33 #include "global.h"
34 
35 #include "filehandle.h"
36 #include "mbuffer.h"
37 #include "img_luma.h"
38 #include "img_chroma.h"
39 #include "img_distortion.h"
40 #include "intrarefresh.h"
41 #include "slice.h"
42 #include "fmo.h"
43 #include "sei.h"
44 #include "memalloc.h"
45 #include "fast_memory.h"
46 #include "nalu.h"
47 #include "ratectl.h"
48 #include "mb_access.h"
49 #include "context_ini.h"
50 #include "biariencode.h"
51 #include "enc_statistics.h"
52 #include "conformance.h"
53 #include "report.h"
54 
55 #include "q_matrix.h"
56 #include "q_offsets.h"
57 #include "wp.h"
58 #include "input.h"
59 #include "image.h"
60 #include "errdo.h"
61 #include "img_process.h"
62 #include "rdopt.h"
63 #include "sei.h"
64 #include "configfile.h"
65 #include "mbuffer.h"
66 
67 #include "resize.h"
68 #include "md_common.h"
69 #include "me_epzs_common.h"
70 #include "me_hme.h"
71 
72 extern void UpdateDecoders            (VideoParameters *p_Vid, InputParameters *p_Inp, StorablePicture *enc_pic);
73 
74 extern void DeblockFrame              (VideoParameters *p_Vid, imgpel **, imgpel ***);
75 
76 static void code_a_picture            (VideoParameters *p_Vid, Picture *pic);
77 static void field_picture             (VideoParameters *p_Vid, Picture *top, Picture *bottom);
78 static void prepare_enc_frame_picture (VideoParameters *p_Vid, StorablePicture **stored_pic);
79 #if (MVC_EXTENSION_ENABLE)
80 static void writeout_picture          (VideoParameters *p_Vid, Picture *pic, int is_bottom);
81 #else
82 static void writeout_picture          (VideoParameters *p_Vid, Picture *pic);
83 #endif
84 static byte picture_structure_decision(VideoParameters *p_Vid, Picture *frame, Picture *top, Picture *bot);
85 static void distortion_fld            (VideoParameters *p_Vid, InputParameters *p_Inp, Picture *field_pic, ImageData *imgData);
86 
87 static void field_mode_buffer (VideoParameters *p_Vid);
88 static void frame_mode_buffer (VideoParameters *p_Vid);
89 static void init_frame        (VideoParameters *p_Vid, InputParameters *p_Inp);
90 static void init_field        (VideoParameters *p_Vid, InputParameters *p_Inp);
91 static void put_buffer_frame  (VideoParameters *p_Vid);
92 static void put_buffer_top    (VideoParameters *p_Vid);
93 static void put_buffer_bot    (VideoParameters *p_Vid);
94 
95 static void ReportFirstframe   (VideoParameters *p_Vid, int64 tmp_time);
96 static void ReportI            (VideoParameters *p_Vid, int64 tmp_time);
97 static void ReportP            (VideoParameters *p_Vid, int64 tmp_time);
98 static void ReportB            (VideoParameters *p_Vid, int64 tmp_time);
99 static void ReportNALNonVLCBits(VideoParameters *p_Vid, int64 tmp_time);
100 
101 extern void rd_picture_coding(VideoParameters *p_Vid, InputParameters *p_Inp);
102 
MbAffPostProc(VideoParameters * p_Vid)103 void MbAffPostProc(VideoParameters *p_Vid)
104 {
105   imgpel temp[32][16];
106 
107   StorablePicture *p_Enc_Pic = p_Vid->enc_picture;
108   imgpel ** imgY  = p_Enc_Pic->imgY;
109   imgpel ***imgUV = p_Enc_Pic->imgUV;
110   short i, y, x0, y0, uv;
111 
112   if (p_Vid->yuv_format != YUV400)
113   {
114     for (i=0; i<(int)p_Vid->PicSizeInMbs; i+=2)
115     {
116       if (p_Enc_Pic->motion.mb_field[i])
117       {
118         get_mb_pos(p_Vid, i, p_Vid->mb_size[IS_LUMA], &x0, &y0);
119         for (y=0; y<(2*MB_BLOCK_SIZE);y++)
120           memcpy(&temp[y],&imgY[y0+y][x0], MB_BLOCK_SIZE * sizeof(imgpel));
121 
122         for (y=0; y<MB_BLOCK_SIZE;y++)
123         {
124           memcpy(&imgY[y0+(2*y)][x0],temp[y], MB_BLOCK_SIZE * sizeof(imgpel));
125           memcpy(&imgY[y0+(2*y + 1)][x0],temp[y+ MB_BLOCK_SIZE], MB_BLOCK_SIZE * sizeof(imgpel));
126         }
127 
128         x0 = x0 / (16/p_Vid->mb_cr_size_x);
129         y0 = y0 / (16/p_Vid->mb_cr_size_y);
130 
131         for (uv=0; uv<2; uv++)
132         {
133           for (y=0; y < (2 * p_Vid->mb_cr_size_y); y++)
134             memcpy(&temp[y],&imgUV[uv][y0+y][x0], p_Vid->mb_cr_size_x * sizeof(imgpel));
135 
136           for (y=0; y<p_Vid->mb_cr_size_y;y++)
137           {
138             memcpy(&imgUV[uv][y0+(2*y)][x0],temp[y], p_Vid->mb_cr_size_x * sizeof(imgpel));
139             memcpy(&imgUV[uv][y0+(2*y + 1)][x0],temp[y+ p_Vid->mb_cr_size_y], p_Vid->mb_cr_size_x * sizeof(imgpel));
140           }
141         }
142       }
143     }
144   }
145   else
146   {
147     for (i=0; i<(int)p_Vid->PicSizeInMbs; i+=2)
148     {
149       if (p_Enc_Pic->motion.mb_field[i])
150       {
151         get_mb_pos(p_Vid, i, p_Vid->mb_size[IS_LUMA], &x0, &y0);
152         for (y=0; y<(2*MB_BLOCK_SIZE);y++)
153           memcpy(&temp[y],&imgY[y0+y][x0], MB_BLOCK_SIZE * sizeof(imgpel));
154 
155         for (y=0; y<MB_BLOCK_SIZE;y++)
156         {
157           memcpy(&imgY[y0+(2*y)][x0],temp[y], MB_BLOCK_SIZE * sizeof(imgpel));
158           memcpy(&imgY[y0+(2*y + 1)][x0],temp[y+ MB_BLOCK_SIZE], MB_BLOCK_SIZE * sizeof(imgpel));
159         }
160       }
161     }
162   }
163 }
164 
165 /*!
166  ************************************************************************
167  * \brief
168  *    Sets slice type
169  *
170  ************************************************************************
171  */
set_slice_type(VideoParameters * p_Vid,InputParameters * p_Inp,int slice_type)172 void set_slice_type(VideoParameters *p_Vid, InputParameters *p_Inp, int slice_type)
173 {
174   p_Vid->type    = (short) slice_type;            // set slice type
175   p_Vid->RCMinQP = p_Inp->RCMinQP[p_Vid->type];
176   p_Vid->RCMaxQP = p_Inp->RCMaxQP[p_Vid->type];
177 }
178 
code_a_plane(VideoParameters * p_Vid,InputParameters * p_Inp)179 static void code_a_plane(VideoParameters *p_Vid, InputParameters *p_Inp)
180 {
181   unsigned int NumberOfCodedMBs = 0;
182   int SliceGroup = 0;
183   DistMetric distortion;
184 
185   // The slice_group_change_cycle can be changed here.
186   // FmoInit() is called before coding each picture, frame or field
187   p_Vid->slice_group_change_cycle=1;
188   FmoInit(p_Vid, p_Vid->active_pps, p_Vid->active_sps);
189   FmoStartPicture (p_Vid);           //! picture level initialization of FMO
190 
191   CalculateQuant4x4Param (p_Vid);
192   CalculateOffset4x4Param(p_Vid);
193 
194   if(p_Inp->Transform8x8Mode)
195   {
196     CalculateQuant8x8Param (p_Vid);
197     CalculateOffset8x8Param(p_Vid);
198   }
199 
200   reset_pic_bin_count(p_Vid);
201   p_Vid->bytes_in_picture = 0;
202 
203   while (NumberOfCodedMBs < p_Vid->PicSizeInMbs)       // loop over slices
204   {
205     // Encode one SLice Group
206     while (!FmoSliceGroupCompletelyCoded (p_Vid, SliceGroup))
207     {
208       // Encode the current slice
209       if (!p_Vid->mb_aff_frame_flag)
210         NumberOfCodedMBs += encode_one_slice (p_Vid, SliceGroup, NumberOfCodedMBs);
211       else
212         NumberOfCodedMBs += encode_one_slice_MBAFF (p_Vid, SliceGroup, NumberOfCodedMBs);
213 
214       FmoSetLastMacroblockInSlice (p_Vid, p_Vid->current_mb_nr);
215       // Proceed to next slice
216       p_Vid->current_slice_nr++;
217       p_Vid->p_Stats->bit_slice = 0;
218     }
219     // Proceed to next SliceGroup
220     SliceGroup++;
221   }
222   FmoEndPicture ();
223 
224   if ((p_Inp->SkipDeBlockNonRef == 0) || (p_Vid->nal_reference_idc != 0))
225   {
226     if(p_Inp->RDPictureDeblocking && !p_Vid->TurnDBOff)
227     {
228       find_distortion(p_Vid, &p_Vid->imgData);
229       distortion.value[0] = p_Vid->p_Dist->metric[SSE].value[0];
230       distortion.value[1] = p_Vid->p_Dist->metric[SSE].value[1];
231       distortion.value[2] = p_Vid->p_Dist->metric[SSE].value[2];
232     }
233     else
234       distortion.value[0] = distortion.value[1] = distortion.value[2] = 0;
235 
236     DeblockFrame (p_Vid, p_Vid->enc_picture->imgY, p_Vid->enc_picture->imgUV); //comment out to disable deblocking filter
237 
238     if(p_Inp->RDPictureDeblocking && !p_Vid->TurnDBOff)
239     {
240       find_distortion(p_Vid, &p_Vid->imgData);
241       if(distortion.value[0]+distortion.value[1]+distortion.value[2] <
242         p_Vid->p_Dist->metric[SSE].value[0]+
243         p_Vid->p_Dist->metric[SSE].value[1]+
244         p_Vid->p_Dist->metric[SSE].value[2])
245       {
246         p_Vid->EvaluateDBOff = 1;
247       }
248     }
249   }
250 
251 }
252 /*!
253  ************************************************************************
254  * \brief
255  *    Encodes a picture
256  *
257  *    This is the main picture coding loop.. It is called by all this
258  *    frame and field coding stuff after the p_Vid-> elements have been
259  *    set up.  Not sure whether it is useful for MB-adaptive frame/field
260  *    coding
261  ************************************************************************
262  */
code_a_picture(VideoParameters * p_Vid,Picture * pic)263 static void code_a_picture(VideoParameters *p_Vid, Picture *pic)
264 {
265   InputParameters *p_Inp = p_Vid->p_Inp;
266   int pl;
267 
268   p_Vid->currentPicture = pic;
269   p_Vid->currentPicture->idr_flag = get_idr_flag(p_Vid);
270 
271   pic->no_slices = 0;
272 
273   RandomIntraNewPicture (p_Vid);     //! Allocates forced INTRA MBs (even for fields!)
274   if( (p_Inp->separate_colour_plane_flag != 0) )
275   {
276     for( pl=0; pl<MAX_PLANE; pl++ )
277     {
278       p_Vid->current_mb_nr = 0;
279       p_Vid->current_slice_nr = 0;
280       p_Vid->SumFrameQP = 0;
281       p_Vid->num_ref_idx_l0_active = 0;
282       p_Vid->num_ref_idx_l1_active = 0;
283 
284       p_Vid->colour_plane_id = (char) pl;
285 
286       code_a_plane(p_Vid, p_Inp);
287     }
288   }
289   else
290   {
291     code_a_plane(p_Vid, p_Inp);
292   }
293 
294   if (p_Vid->mb_aff_frame_flag)
295     MbAffPostProc(p_Vid);
296 
297 }
298 
299 /*!
300  ************************************************************************
301  * \brief
302  *    Determine whether picture is coded as IDR
303  ************************************************************************
304  */
get_idr_flag(VideoParameters * p_Vid)305 byte get_idr_flag( VideoParameters *p_Vid )
306 {
307   int idr_flag;
308   FrameUnitStruct *p_cur_frm = p_Vid->p_curr_frm_struct;
309 
310   switch( p_Vid->structure )
311   {
312   default:
313   case FRAME:
314     idr_flag = p_cur_frm->p_frame_pic->idr_flag;
315     break;
316   case TOP_FIELD:
317     idr_flag = p_cur_frm->p_top_fld_pic->idr_flag;
318     break;
319   case BOTTOM_FIELD:
320     idr_flag = p_cur_frm->p_bot_fld_pic->idr_flag;
321     break;
322   }
323 
324   return (byte) idr_flag;
325 }
326 
327 /*!
328  ************************************************************************
329  * \brief
330  *    Determine whether picture is coded as IDR or Openg GOP Intra
331  ************************************************************************
332  */
get_random_access_flag(VideoParameters * p_Vid)333 byte get_random_access_flag( VideoParameters *p_Vid )
334 {
335   int random_access;
336   FrameUnitStruct *p_cur_frm = p_Vid->p_curr_frm_struct;
337 
338   switch( p_Vid->structure )
339   {
340   default:
341   case FRAME:
342     random_access = p_cur_frm->p_frame_pic->random_access;
343     break;
344   case TOP_FIELD:
345     random_access = p_cur_frm->p_top_fld_pic->random_access;
346     break;
347   case BOTTOM_FIELD:
348     random_access = p_cur_frm->p_bot_fld_pic->random_access;
349     break;
350   }
351 
352   return (byte) random_access;
353 }
354 
355 /*!
356  ************************************************************************
357  * \brief
358  *    Update global stats
359  ************************************************************************
360  */
update_global_stats(InputParameters * p_Inp,StatParameters * gl_stats,StatParameters * cur_stats)361 void update_global_stats(InputParameters *p_Inp, StatParameters *gl_stats, StatParameters *cur_stats)
362 {
363   int i, j, k;
364 
365   if (p_Inp->skip_gl_stats == 0)
366   {
367     for (i = 0; i < 4; i++)
368     {
369       gl_stats->intra_chroma_mode[i]    += cur_stats->intra_chroma_mode[i];
370     }
371 
372     for (i = 0; i < 5; i++)
373     {
374       gl_stats->quant[i]                 += cur_stats->quant[i];
375       gl_stats->num_macroblocks[i]       += cur_stats->num_macroblocks[i];
376       gl_stats->bit_use_mb_type [i]      += cur_stats->bit_use_mb_type[i];
377       gl_stats->bit_use_header  [i]      += cur_stats->bit_use_header[i];
378       gl_stats->tmp_bit_use_cbp [i]      += cur_stats->tmp_bit_use_cbp[i];
379       gl_stats->bit_use_coeffC  [i]      += cur_stats->bit_use_coeffC[i];
380       gl_stats->bit_use_coeff[0][i]      += cur_stats->bit_use_coeff[0][i];
381       gl_stats->bit_use_coeff[1][i]      += cur_stats->bit_use_coeff[1][i];
382       gl_stats->bit_use_coeff[2][i]      += cur_stats->bit_use_coeff[2][i];
383       gl_stats->bit_use_delta_quant[i]   += cur_stats->bit_use_delta_quant[i];
384       gl_stats->bit_use_stuffing_bits[i] += cur_stats->bit_use_stuffing_bits[i];
385 
386       for (k = 0; k < 2; k++)
387         gl_stats->b8_mode_0_use[i][k] += cur_stats->b8_mode_0_use[i][k];
388 
389       for (j = 0; j < 15; j++)
390       {
391         gl_stats->mode_use[i][j]     += cur_stats->mode_use[i][j];
392         gl_stats->bit_use_mode[i][j] += cur_stats->bit_use_mode[i][j];
393         for (k = 0; k < 2; k++)
394           gl_stats->mode_use_transform[i][j][k] += cur_stats->mode_use_transform[i][j][k];
395       }
396     }
397   }
398 }
399 
storeRedundantFrame(VideoParameters * p_Vid)400 static void storeRedundantFrame(VideoParameters *p_Vid)
401 {
402   int j, k;
403   if(p_Vid->key_frame)
404   {
405     for(j=0; j<p_Vid->height; j++)
406     {
407       memcpy(p_Vid->imgY_tmp[j], p_Vid->enc_frame_picture[0]->imgY[j], p_Vid->width * sizeof(imgpel));
408     }
409 
410     for (k = 0; k < 2; k++)
411     {
412       for(j=0; j<p_Vid->height_cr; j++)
413       {
414         memcpy(p_Vid->imgUV_tmp[k][j], p_Vid->enc_frame_picture[0]->imgUV[k][j], p_Vid->width_cr * sizeof(imgpel));
415       }
416     }
417   }
418 
419   if(p_Vid->redundant_coding)
420   {
421     for(j=0; j<p_Vid->height; j++)
422     {
423       memcpy(p_Vid->enc_frame_picture[0]->imgY[j], p_Vid->imgY_tmp[j], p_Vid->width * sizeof(imgpel));
424     }
425     for (k = 0; k < 2; k++)
426     {
427       for(j=0; j<p_Vid->height_cr; j++)
428       {
429         memcpy(p_Vid->enc_frame_picture[0]->imgUV[k][j], p_Vid->imgUV_tmp[k][j], p_Vid->width_cr * sizeof(imgpel));
430       }
431     }
432   }
433 }
434 /*!
435  ************************************************************************
436  * \brief
437  *    Free storable pictures
438  ************************************************************************
439  */
free_pictures(VideoParameters * p_Vid,int dummy)440 void free_pictures(VideoParameters *p_Vid, int dummy)
441 {
442   int i;
443   for (i = 0; i < 6; i++)
444   {
445     if(p_Vid->enc_picture != p_Vid->enc_frame_picture[i])
446       free_storable_picture(p_Vid, p_Vid->enc_frame_picture[i]);
447   }
448 }
449 
450 #if (MVC_EXTENSION_ENABLE)
451 /*!
452  ************************************************************************
453  * \brief
454  *    Write EL NALUs for field coded pictures
455  ************************************************************************
456  */
write_el_field_vcl_nalu(VideoParameters * p_Vid)457 void write_el_field_vcl_nalu(VideoParameters *p_Vid)
458 {
459   int temp1_nal_reference_idc;
460   int temp1_non_idr_flag[2];
461   int temp1_priority_id;
462   int temp1_view_id;
463   int temp1_temporal_id;
464   int temp1_anchor_pic_flag[2];
465   int temp1_inter_view_flag[2];
466 
467   if(p_Vid->view_id == 1)
468   {
469     int temp_view_id = p_Vid->view_id;
470 
471     p_Vid->view_id = 0;
472     p_Vid->structure = TOP_FIELD;
473     write_non_vcl_nalu_mvc(p_Vid);
474 
475     //backup view_id 1
476     temp1_nal_reference_idc   = p_Vid->nal_reference_idc;
477     temp1_non_idr_flag[0]     = p_Vid->non_idr_flag[0];
478     temp1_non_idr_flag[1]     = p_Vid->non_idr_flag[1];
479     temp1_priority_id         = p_Vid->priority_id;
480     temp1_view_id             = temp_view_id;
481     temp1_temporal_id         = p_Vid->temporal_id;
482     temp1_anchor_pic_flag[0]  = p_Vid->anchor_pic_flag[0];
483     temp1_anchor_pic_flag[1]  = p_Vid->anchor_pic_flag[1];
484     temp1_inter_view_flag[0]  = p_Vid->inter_view_flag[0];
485     temp1_inter_view_flag[1]  = p_Vid->inter_view_flag[1];
486 
487     //restore view_id 0
488     p_Vid->nal_reference_idc  = p_Vid->temp0_nal_reference_idc;
489     p_Vid->non_idr_flag[0]    = p_Vid->temp0_non_idr_flag[0];
490     p_Vid->non_idr_flag[1]    = p_Vid->temp0_non_idr_flag[1];
491     p_Vid->priority_id        = p_Vid->temp0_priority_id;
492     p_Vid->view_id            = p_Vid->temp0_view_id;
493     p_Vid->temporal_id        = p_Vid->temp0_temporal_id;
494     p_Vid->anchor_pic_flag[0] = p_Vid->temp0_anchor_pic_flag[0];
495     p_Vid->anchor_pic_flag[1] = p_Vid->temp0_anchor_pic_flag[1];
496     p_Vid->inter_view_flag[0] = p_Vid->temp0_inter_view_flag[0];
497     p_Vid->inter_view_flag[1] = p_Vid->temp0_inter_view_flag[1];
498     writeout_picture (p_Vid, p_Vid->field_pic1[0], 0);  //top
499 
500     //restore view_id 1
501     p_Vid->nal_reference_idc  = temp1_nal_reference_idc;
502     p_Vid->non_idr_flag[0]    = temp1_non_idr_flag[0];
503     p_Vid->non_idr_flag[1]    = temp1_non_idr_flag[1];
504     p_Vid->priority_id        = temp1_priority_id;
505     p_Vid->view_id            = temp1_view_id;
506     p_Vid->temporal_id        = temp1_temporal_id;
507     p_Vid->anchor_pic_flag[0] = temp1_anchor_pic_flag[0];
508     p_Vid->anchor_pic_flag[1] = temp1_anchor_pic_flag[1];
509     p_Vid->inter_view_flag[0] = temp1_inter_view_flag[0];
510     p_Vid->inter_view_flag[1] = temp1_inter_view_flag[1];
511     write_non_vcl_nalu_mvc( p_Vid );
512     writeout_picture (p_Vid, p_Vid->field_pic2[0], 0);  //top
513 
514     //restore view_id 0
515     p_Vid->nal_reference_idc  = p_Vid->temp0_nal_reference_idc;
516     p_Vid->non_idr_flag[0]    = p_Vid->temp0_non_idr_flag[0];
517     p_Vid->non_idr_flag[1]    = p_Vid->temp0_non_idr_flag[1];
518     p_Vid->priority_id        = p_Vid->temp0_priority_id;
519     p_Vid->view_id            = p_Vid->temp0_view_id;
520     p_Vid->temporal_id        = p_Vid->temp0_temporal_id;
521     p_Vid->anchor_pic_flag[0] = p_Vid->temp0_anchor_pic_flag[0];
522     p_Vid->anchor_pic_flag[1] = p_Vid->temp0_anchor_pic_flag[1];
523     p_Vid->inter_view_flag[0] = p_Vid->temp0_inter_view_flag[0];
524     p_Vid->inter_view_flag[1] = p_Vid->temp0_inter_view_flag[1];
525     p_Vid->structure = BOTTOM_FIELD;
526     write_non_vcl_nalu_mvc( p_Vid );
527     writeout_picture (p_Vid, p_Vid->field_pic1[1], 1);  //bot
528 
529     //restore view_id 1
530     p_Vid->nal_reference_idc  = temp1_nal_reference_idc;
531     p_Vid->non_idr_flag[0]    = temp1_non_idr_flag[0];
532     p_Vid->non_idr_flag[1]    = temp1_non_idr_flag[1];
533     p_Vid->priority_id        = temp1_priority_id;
534     p_Vid->view_id            = temp1_view_id;
535     p_Vid->temporal_id        = temp1_temporal_id;
536     p_Vid->anchor_pic_flag[0] = temp1_anchor_pic_flag[0];
537     p_Vid->anchor_pic_flag[1] = temp1_anchor_pic_flag[1];
538     p_Vid->inter_view_flag[0] = temp1_inter_view_flag[0];
539     p_Vid->inter_view_flag[1] = temp1_inter_view_flag[1];
540     write_non_vcl_nalu_mvc( p_Vid );
541     writeout_picture (p_Vid, p_Vid->field_pic2[1], 1);  //bot
542   }
543   else  // store the first view nal unit
544   {
545     p_Vid->temp0_nal_reference_idc   = p_Vid->nal_reference_idc;
546     p_Vid->temp0_non_idr_flag[0]     = p_Vid->non_idr_flag[0];
547     p_Vid->temp0_non_idr_flag[1]     = p_Vid->non_idr_flag[1];
548     p_Vid->temp0_priority_id         = p_Vid->priority_id;
549     p_Vid->temp0_view_id             = p_Vid->view_id;
550     p_Vid->temp0_temporal_id         = p_Vid->temporal_id;
551     p_Vid->temp0_anchor_pic_flag[0]  = p_Vid->anchor_pic_flag[0];
552     p_Vid->temp0_anchor_pic_flag[1]  = p_Vid->anchor_pic_flag[1];
553     p_Vid->temp0_inter_view_flag[0]  = p_Vid->inter_view_flag[0];
554     p_Vid->temp0_inter_view_flag[1]  = p_Vid->inter_view_flag[1];
555   }
556 }
557 
558 /*!
559  ************************************************************************
560  * \brief
561  *    Write coded frame (one frame picture or two field pictures)
562  ************************************************************************
563  */
write_frame_picture(VideoParameters * p_Vid)564 void write_frame_picture(VideoParameters *p_Vid)
565 {
566   InputParameters *p_Inp = p_Vid->p_Inp;
567 
568   if (p_Vid->fld_flag)            // field mode (use field when fld_flag=1 only)
569   {
570     field_mode_buffer (p_Vid);
571 
572     if(p_Inp->num_of_views==1)
573     {
574       p_Vid->structure = TOP_FIELD;
575       write_non_vcl_nalu(p_Vid);
576       writeout_picture  (p_Vid, p_Vid->field_pic1[0], 0);
577       p_Vid->structure = BOTTOM_FIELD;
578       write_non_vcl_nalu_bot_fld(p_Vid);
579       writeout_picture  (p_Vid, p_Vid->field_pic1[1], 1);
580     }
581     else
582     {
583       write_el_field_vcl_nalu(p_Vid);
584     }
585   }
586   else                          //frame mode
587   {
588     frame_mode_buffer (p_Vid);
589     p_Vid->structure = FRAME;
590 
591 #if (MVC_EXTENSION_ENABLE)
592     if ( p_Inp->num_of_views == 2 )
593     {
594       write_non_vcl_nalu_mvc(p_Vid);
595     }
596     else
597 #endif
598     write_non_vcl_nalu(p_Vid);
599     if (p_Vid->type==SI_SLICE)
600     {
601       writeout_picture  (p_Vid, p_Vid->frame_pic_si, 0);
602     }
603     else
604     {
605       writeout_picture  (p_Vid, p_Vid->p_frame_pic, 0);
606     }
607   }
608 }
609 #else
write_frame_picture(VideoParameters * p_Vid)610 void write_frame_picture(VideoParameters *p_Vid)
611 {
612   InputParameters *p_Inp = p_Vid->p_Inp;
613   if (p_Vid->fld_flag)            // field mode (use field when fld_flag=1 only)
614   {
615     p_Vid->structure = TOP_FIELD;
616     field_mode_buffer (p_Vid);
617     write_non_vcl_nalu(p_Vid);
618     writeout_picture  (p_Vid, p_Vid->field_pic[0]);
619     p_Vid->structure = BOTTOM_FIELD;
620     write_non_vcl_nalu_bot_fld(p_Vid);
621     writeout_picture  (p_Vid, p_Vid->field_pic[1]);
622   }
623   else                          //frame mode
624   {
625     frame_mode_buffer (p_Vid);
626 
627     write_non_vcl_nalu(p_Vid);
628     if (p_Vid->type==SI_SLICE)
629     {
630       writeout_picture  (p_Vid, p_Vid->frame_pic_si);
631     }
632     else
633     {
634       writeout_picture  (p_Vid, p_Vid->p_frame_pic);
635     }
636   }
637 }
638 #endif
639 
640 /*!
641  ************************************************************************
642  * \brief
643  *    Read input data while considering and performing a 3:2 pulldown
644  *    process.
645  ************************************************************************
646  */
read_input_data_32pulldown(VideoParameters * p_Vid)647 static int read_input_data_32pulldown(VideoParameters *p_Vid)
648 {
649   InputParameters *p_Inp = p_Vid->p_Inp;
650   int file_read = 0;
651   int frm_no_in_file_first = 0;
652   int frm_no_in_file_second = 0;
653   int pull_down_offset = p_Inp->enable_32_pulldown == 1 ? 1 : 2;
654 
655 #if (MVC_EXTENSION_ENABLE)
656     if(p_Inp->num_of_views==2 && p_Vid->view_id == 1)
657     {
658       frm_no_in_file_first = ((p_Vid->frm_no_in_file * 4 + pull_down_offset)/ 5);
659       frm_no_in_file_second = ((p_Vid->frm_no_in_file * 4 + 3)/ 5);
660 
661       // Read frame data (first frame)
662       file_read = read_one_frame (p_Vid, &p_Inp->input_file2, frm_no_in_file_first, p_Inp->infile_header, &p_Inp->source, &p_Inp->output, p_Vid->imgData0.frm_data);
663       if ( !file_read )
664       {
665         // end of file or stream found: trigger error handling
666         get_number_of_frames (p_Inp, &p_Inp->input_file2);
667         fprintf(stdout, "\nIncorrect FramesToBeEncoded: actual number is %6d frames!\n", p_Inp->no_frames );
668         return 0;
669       }
670       pad_borders (p_Inp->output, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, p_Vid->imgData0.frm_data);
671 
672       // Read frame data (second frame)
673       file_read = read_one_frame (p_Vid, &p_Inp->input_file2, frm_no_in_file_second, p_Inp->infile_header, &p_Inp->source, &p_Inp->output, p_Vid->imgData4.frm_data);
674       if ( !file_read )
675       {
676         // end of file or stream found: trigger error handling
677         get_number_of_frames (p_Inp, &p_Inp->input_file2);
678         fprintf(stdout, "\nIncorrect FramesToBeEncoded: actual number is %6d frames!\n", p_Inp->no_frames );
679         return 0;
680       }
681       pad_borders (p_Inp->output, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, p_Vid->imgData4.frm_data);
682     }
683     else
684 #endif
685     {
686       frm_no_in_file_first = ((p_Vid->frm_no_in_file * 4 + pull_down_offset)/ 5);
687       frm_no_in_file_second = ((p_Vid->frm_no_in_file * 4 + 3)/ 5);
688 
689       // Read frame data (first frame)
690       file_read = read_one_frame (p_Vid, &p_Inp->input_file1, frm_no_in_file_first, p_Inp->infile_header, &p_Inp->source, &p_Inp->output, p_Vid->imgData0.frm_data);
691       if ( !file_read )
692       {
693         // end of file or stream found: trigger error handling
694         get_number_of_frames (p_Inp, &p_Inp->input_file1);
695         fprintf(stdout, "\nIncorrect FramesToBeEncoded: actual number is %6d frames!\n", p_Inp->no_frames );
696         return 0;
697       }
698       pad_borders (p_Inp->output, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, p_Vid->imgData0.frm_data);
699 
700       // Read frame data (second frame)
701       file_read = read_one_frame (p_Vid, &p_Inp->input_file1, frm_no_in_file_second, p_Inp->infile_header, &p_Inp->source, &p_Inp->output, p_Vid->imgData4.frm_data);
702       if ( !file_read )
703       {
704         // end of file or stream found: trigger error handling
705         get_number_of_frames (p_Inp, &p_Inp->input_file1);
706         fprintf(stdout, "\nIncorrect FramesToBeEncoded: actual number is %6d frames!\n", p_Inp->no_frames );
707         return 0;
708       }
709       pad_borders (p_Inp->output, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, p_Vid->imgData4.frm_data);
710     }
711 
712 
713     return 1;
714 }
715 
read_input_data(VideoParameters * p_Vid)716 static int read_input_data(VideoParameters *p_Vid)
717 {
718   InputParameters *p_Inp = p_Vid->p_Inp;
719   int file_read = 0;
720 
721 #if (MVC_EXTENSION_ENABLE)
722     if(p_Inp->num_of_views==2 && p_Vid->view_id == 1)
723     {
724       file_read = read_one_frame (p_Vid, &p_Inp->input_file2, p_Vid->frm_no_in_file, p_Inp->infile_header, &p_Inp->source, &p_Inp->output, p_Vid->imgData0.frm_data);
725       if ( !file_read )
726       {
727         // end of file or stream found: trigger error handling
728         get_number_of_frames (p_Inp, &p_Inp->input_file2);
729         fprintf(stdout, "\nIncorrect FramesToBeEncoded: actual number is %6d frames!\n", p_Inp->no_frames );
730         return 0;
731       }
732       pad_borders (p_Inp->output, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, p_Vid->imgData0.frm_data);
733     }
734     else
735 #endif
736     {
737       file_read = read_one_frame (p_Vid, &p_Inp->input_file1, p_Vid->frm_no_in_file, p_Inp->infile_header, &p_Inp->source, &p_Inp->output, p_Vid->imgData0.frm_data);
738       if ( !file_read )
739       {
740         // end of file or stream found: trigger error handling
741         get_number_of_frames (p_Inp, &p_Inp->input_file1);
742         fprintf(stdout, "\nIncorrect FramesToBeEncoded: actual number is %6d frames!\n", p_Inp->no_frames );
743         return 0;
744       }
745       pad_borders (p_Inp->output, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, p_Vid->imgData0.frm_data);
746     }
747 
748   return 1;
749 }
750 
perform_encode_field(VideoParameters * p_Vid)751 void perform_encode_field(VideoParameters *p_Vid)
752 {
753   InputParameters *p_Inp = p_Vid->p_Inp;
754 
755   //Rate control
756   if ( p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE )
757     p_Vid->p_rc_gen->FieldControl = 1;
758 
759   p_Vid->field_picture = 1;  // we encode fields
760 
761 #if (MVC_EXTENSION_ENABLE)
762   field_picture (p_Vid, p_Vid->field_pic_ptr[0], p_Vid->field_pic_ptr[1]);
763 #else
764   field_picture (p_Vid, p_Vid->field_pic[0], p_Vid->field_pic[1]);
765 #endif
766   p_Vid->fld_flag = TRUE;
767 }
768 
perform_encode_frame(VideoParameters * p_Vid)769 void perform_encode_frame(VideoParameters *p_Vid)
770 {
771   InputParameters *p_Inp = p_Vid->p_Inp;
772   int tmpFrameQP = 0;
773   int num_ref_idx_l0 = 0;
774   int num_ref_idx_l1 = 0;
775 
776   int frame_type;
777 
778   //Rate control
779   if ( p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE )
780     p_Vid->p_rc_gen->FieldControl = 0;
781 
782   p_Vid->field_picture = 0; // we encode a frame
783 
784   //Rate control
785   if(p_Inp->RCEnable)
786     rc_init_frame(p_Vid, p_Inp);
787   // Ensure that p_Vid->p_curr_frm_struct->qp is properly set
788   p_Vid->p_curr_frm_struct->qp = p_Vid->qp;
789 
790   p_Vid->active_pps = p_Vid->PicParSet[0];
791 
792   // HME. Currently not supported for B slices
793   if(p_Inp->HMEEnable)
794   {
795     if (p_Vid->type!=I_SLICE && p_Vid->type!=SI_SLICE)
796       invoke_HME(p_Vid, 0);
797   }
798 
799 #if (MVC_EXTENSION_ENABLE)
800   if(p_Vid->view_id!=1 || !p_Vid->sec_view_force_fld)
801   {
802 #endif
803     if ((p_Vid->type == B_SLICE || p_Vid->type == P_SLICE || p_Vid->type==I_SLICE) && p_Inp->RDPictureDecision)
804     {
805       frame_picture_mp (p_Vid, p_Inp);
806     }
807     else
808     {
809       frame_picture (p_Vid, p_Vid->frame_pic[0], &p_Vid->imgData, 0);
810       p_Vid->p_frame_pic = p_Vid->frame_pic[0];
811     }
812 
813     tmpFrameQP = p_Vid->SumFrameQP; // call it here since rd_picture_coding buffers it and may modify it
814     num_ref_idx_l0 = p_Vid->num_ref_idx_l0_active;
815     num_ref_idx_l1 = p_Vid->num_ref_idx_l1_active;
816 
817     if (p_Vid->p_curr_frm_struct->type == SI_SLICE)
818     {
819       // once the picture has been encoded as a primary SP frame encode as an SI frame
820       set_slice_type( p_Vid, p_Inp, SI_SLICE );
821       frame_picture (p_Vid, p_Vid->frame_pic_si, &p_Vid->imgData, 0);
822     }
823 
824     if ((p_Vid->type == SP_SLICE) && (p_Inp->sp_output_indicator))
825     {
826       // output the transformed and quantized coefficients (useful for switching SP frames)
827       output_SP_coefficients(p_Vid, p_Inp);
828     }
829 #if (MVC_EXTENSION_ENABLE)
830   }
831 #endif
832 
833 #if (MVC_EXTENSION_ENABLE)
834   if ((p_Inp->PicInterlace == ADAPTIVE_CODING) && (!is_MVC_profile(p_Vid->active_sps->profile_idc) ||
835      (/*(p_Vid->active_sps->profile_idc == SBSPLUS_HIGH) &&*/ ((p_Vid->view_id == 0) || (p_Vid->view_id == 1 && p_Vid->sec_view_force_fld)))))
836 #else
837   if (p_Inp->PicInterlace == ADAPTIVE_CODING)
838 #endif
839   {
840     DecRefPicMarking_t *tmp_drpm;
841 
842     if ( p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE )
843       p_Vid->p_rc_gen->FieldControl=1;
844     p_Vid->write_macroblock = FALSE;
845     p_Vid->bot_MB = FALSE;
846 
847     frame_type = p_Vid->type;
848 
849     p_Vid->field_picture = 1;  // we encode fields
850 
851     //Free frame based dec_ref_pic_marking_buffer
852     while (p_Vid->dec_ref_pic_marking_buffer)
853     {
854       tmp_drpm = p_Vid->dec_ref_pic_marking_buffer;
855       p_Vid->dec_ref_pic_marking_buffer = tmp_drpm->Next;
856       free(tmp_drpm);
857     }
858 
859 #if (MVC_EXTENSION_ENABLE)
860     field_picture (p_Vid, p_Vid->field_pic_ptr[0], p_Vid->field_pic_ptr[1]);
861 
862     if(p_Inp->num_of_views==1 || p_Vid->view_id==0)  // first view will make the decision
863     {
864       if(p_Vid->rd_pass == 0)
865         p_Vid->fld_flag = picture_structure_decision (p_Vid, p_Vid->frame_pic[0], p_Vid->field_pic_ptr[0], p_Vid->field_pic_ptr[1]);
866       else if(p_Vid->rd_pass == 1)
867         p_Vid->fld_flag = picture_structure_decision (p_Vid, p_Vid->frame_pic[1], p_Vid->field_pic_ptr[0], p_Vid->field_pic_ptr[1]);
868       else
869         p_Vid->fld_flag = picture_structure_decision (p_Vid, p_Vid->frame_pic[2], p_Vid->field_pic_ptr[0], p_Vid->field_pic_ptr[1]);
870 
871       p_Vid->sec_view_force_fld = p_Vid->fld_flag;    // store 1st view decision, same coding structure to be used for second view
872     }
873     else
874     {
875       p_Vid->fld_flag = (byte) p_Vid->sec_view_force_fld;
876     }
877 #else
878     field_picture (p_Vid, p_Vid->field_pic[0], p_Vid->field_pic[1]);
879 
880     if(p_Vid->rd_pass == 0)
881       p_Vid->fld_flag = picture_structure_decision (p_Vid, p_Vid->frame_pic[0], p_Vid->field_pic[0], p_Vid->field_pic[1]);
882     else if(p_Vid->rd_pass == 1)
883       p_Vid->fld_flag = picture_structure_decision (p_Vid, p_Vid->frame_pic[1], p_Vid->field_pic[0], p_Vid->field_pic[1]);
884     else
885       p_Vid->fld_flag = picture_structure_decision (p_Vid, p_Vid->frame_pic[2], p_Vid->field_pic[0], p_Vid->field_pic[1]);
886 #endif
887 
888     if ( p_Vid->fld_flag )
889     {
890       tmpFrameQP = p_Vid->SumFrameQP;
891       num_ref_idx_l0 = p_Vid->num_ref_idx_l0_active;
892       num_ref_idx_l1 = p_Vid->num_ref_idx_l1_active;
893     }
894 
895     if ( p_Vid->fld_flag==0 )
896     {
897       p_Vid->type = (short) frame_type;
898     }
899 
900     update_field_frame_contexts (p_Vid, p_Vid->fld_flag);
901 
902     //Rate control
903     if ( p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE )
904       p_Vid->p_rc_gen->FieldFrame = !(p_Vid->fld_flag) ? 1 : 0;
905   }
906   else
907     p_Vid->fld_flag = FALSE;
908 
909   p_Vid->SumFrameQP = tmpFrameQP;
910   p_Vid->num_ref_idx_l0_active = num_ref_idx_l0;
911   p_Vid->num_ref_idx_l1_active = num_ref_idx_l1;
912 }
913 
free_slice_data(VideoParameters * p_Vid)914 void free_slice_data(VideoParameters *p_Vid)
915 {
916   int i;
917 
918   if (p_Vid->frame_pic_si)
919   {
920     free_slice_list(p_Vid->frame_pic_si);
921   }
922 
923   for (i = 0; i < p_Vid->frm_iter; i++)
924   {
925     if (p_Vid->frame_pic[i])
926     {
927       free_slice_list(p_Vid->frame_pic[i]);
928     }
929   }
930 
931 #if (MVC_EXTENSION_ENABLE)
932   if ( p_Vid->field_pic_ptr && (p_Vid->p_Inp->num_of_views==1 || p_Vid->view_id==1) )
933   {
934     for (i = 0; i < 2; i++)
935     {
936       if (p_Vid->field_pic1[i])
937         free_slice_list(p_Vid->field_pic1[i]);
938       if (p_Vid->p_Inp->num_of_views==2 && p_Vid->field_pic2)
939       {
940         if (p_Vid->field_pic2[i])
941           free_slice_list(p_Vid->field_pic2[i]);
942       }
943     }
944   }
945 #else
946   if (p_Vid->field_pic)
947   {
948     for (i = 0; i < 2; i++)
949     {
950       if (p_Vid->field_pic[i])
951         free_slice_list(p_Vid->field_pic[i]);
952     }
953   }
954 #endif
955 }
956 
store_coded_picture(DecodedPictureBuffer * p_Dpb)957 void store_coded_picture(DecodedPictureBuffer *p_Dpb)
958 {
959   VideoParameters *p_Vid = p_Dpb->p_Vid;
960   InputParameters *p_Inp = p_Vid->p_Inp;
961   unsigned int profile_idc = p_Vid->active_sps->profile_idc;
962 
963 #if (MVC_EXTENSION_ENABLE)
964   if ( (p_Inp->PicInterlace == ADAPTIVE_CODING) && ((p_Dpb->layer_id == 0 ) || !is_MVC_profile(profile_idc)))
965 #else
966   if ( p_Inp->PicInterlace == ADAPTIVE_CODING )
967 #endif
968   {
969     if (p_Vid->fld_flag)
970     {
971       update_global_stats(p_Inp, p_Vid->p_Stats, &p_Vid->enc_field_picture[0]->stats);
972       update_global_stats(p_Inp, p_Vid->p_Stats, &p_Vid->enc_field_picture[1]->stats);
973       // store bottom field
974       store_picture_in_dpb (p_Dpb, p_Vid->enc_field_picture[1], &p_Inp->output);
975 #if (MVC_EXTENSION_ENABLE)
976       if ( p_Vid->proc_picture && is_MVC_profile(profile_idc) )
977       {
978         store_proc_picture_in_dpb (p_Vid->p_Dpb_layer[1], p_Vid->proc_picture, &p_Inp->output);
979         p_Vid->proc_picture = NULL;
980       }
981 #endif
982       free_storable_picture(p_Vid, p_Vid->enc_frame_picture[0]);
983       free_storable_picture(p_Vid, p_Vid->enc_frame_picture[1]);
984       free_storable_picture(p_Vid, p_Vid->enc_frame_picture[2]);
985     }
986     else
987     {
988       update_global_stats(p_Inp, p_Vid->p_Stats, &p_Vid->enc_frame_picture[p_Vid->rd_pass]->stats);
989       // replace top with frame
990       if (p_Vid->rd_pass==2)
991       {
992         replace_top_pic_with_frame(p_Dpb, p_Vid->enc_frame_picture[2], &p_Inp->output);
993         free_storable_picture     (p_Vid, p_Vid->enc_frame_picture[0]);
994         free_storable_picture     (p_Vid, p_Vid->enc_frame_picture[1]);
995       }
996       else if (p_Vid->rd_pass==1)
997       {
998         replace_top_pic_with_frame(p_Dpb, p_Vid->enc_frame_picture[1], &p_Inp->output);
999         free_storable_picture     (p_Vid, p_Vid->enc_frame_picture[0]);
1000         free_storable_picture     (p_Vid, p_Vid->enc_frame_picture[2]);
1001       }
1002       else
1003       {
1004         if(p_Inp->redundant_pic_flag==0 || (p_Vid->key_frame==0))
1005         {
1006           replace_top_pic_with_frame(p_Dpb, p_Vid->enc_frame_picture[0], &p_Inp->output);
1007           free_storable_picture     (p_Vid, p_Vid->enc_frame_picture[1]);
1008           free_storable_picture     (p_Vid, p_Vid->enc_frame_picture[2]);
1009         }
1010       }
1011       free_storable_picture(p_Vid, p_Vid->enc_field_picture[1]);
1012     }
1013   }
1014   else
1015   {
1016     if (p_Vid->fld_flag)
1017     {
1018       update_global_stats(p_Inp, p_Vid->p_Stats, &p_Vid->enc_field_picture[0]->stats);
1019       update_global_stats(p_Inp, p_Vid->p_Stats, &p_Vid->enc_field_picture[1]->stats);
1020       store_picture_in_dpb(p_Dpb, p_Vid->enc_field_picture[1], &p_Inp->output);
1021 #if (MVC_EXTENSION_ENABLE)
1022       if ( is_MVC_profile(profile_idc) && p_Vid->proc_picture)
1023       {
1024         store_proc_picture_in_dpb (p_Vid->p_Dpb_layer[1], p_Vid->proc_picture, &p_Inp->output);
1025         p_Vid->proc_picture = NULL;
1026       }
1027 #endif
1028     }
1029     else
1030     {
1031       if ((p_Inp->redundant_pic_flag != 1) || (p_Vid->key_frame == 0))
1032       {
1033         update_global_stats(p_Inp, p_Vid->p_Stats, &p_Vid->enc_picture->stats);
1034         store_picture_in_dpb (p_Dpb, p_Vid->enc_picture, &p_Inp->output);
1035 
1036 #if (MVC_EXTENSION_ENABLE)
1037         if ( is_MVC_profile(profile_idc) && p_Vid->proc_picture)
1038         {
1039           store_proc_picture_in_dpb (p_Vid->p_Dpb_layer[1], p_Vid->proc_picture, &p_Inp->output);
1040           p_Vid->proc_picture = NULL;
1041         }
1042 #endif
1043         free_pictures(p_Vid, -1);
1044       }
1045     }
1046   }
1047 }
1048 
update_bitcounter_stats(VideoParameters * p_Vid)1049 void update_bitcounter_stats(VideoParameters *p_Vid)
1050 {
1051   p_Vid->p_Stats->bit_ctr_n = p_Vid->p_Stats->bit_ctr;
1052   p_Vid->p_Stats->bit_ctr_parametersets_n = 0;
1053   p_Vid->p_Stats->bit_ctr_filler_data_n = p_Vid->p_Stats->bit_ctr_filler_data;
1054 
1055 #if (MVC_EXTENSION_ENABLE)
1056   if ( p_Vid->p_Inp->num_of_views == 2 )
1057   {
1058     if ( p_Vid->fld_flag )
1059     {
1060       if ( p_Vid->view_id == 1 )
1061       {
1062         p_Vid->p_Stats->bit_ctr_parametersets_v[0] += p_Vid->p_Stats->bit_ctr_parametersets_n_v[0];
1063         p_Vid->p_Stats->bit_ctr_parametersets_v[1] += p_Vid->p_Stats->bit_ctr_parametersets_n_v[1];
1064         p_Vid->p_Stats->bit_ctr_n_v[0] = p_Vid->p_Stats->bit_ctr_v[0];
1065         p_Vid->p_Stats->bit_ctr_n_v[1] = p_Vid->p_Stats->bit_ctr_v[1];
1066         p_Vid->p_Stats->bit_ctr_parametersets_n_v[0] = 0;
1067         p_Vid->p_Stats->bit_ctr_parametersets_n_v[1] = 0;
1068         p_Vid->p_Stats->bit_ctr_filler_data_n_v[0] = p_Vid->p_Stats->bit_ctr_filler_data_v[0];
1069         p_Vid->p_Stats->bit_ctr_filler_data_n_v[1] = p_Vid->p_Stats->bit_ctr_filler_data_v[1];
1070       }
1071     }
1072     else // this seems to be enough and the check above seems redundant...check later...
1073     {
1074       p_Vid->p_Stats->bit_ctr_parametersets_v[p_Vid->view_id] += p_Vid->p_Stats->bit_ctr_parametersets_n_v[p_Vid->view_id];
1075       p_Vid->p_Stats->bit_ctr_n_v[p_Vid->view_id] = p_Vid->p_Stats->bit_ctr_v[p_Vid->view_id];
1076       p_Vid->p_Stats->bit_ctr_parametersets_n_v[p_Vid->view_id] = 0;
1077       p_Vid->p_Stats->bit_ctr_filler_data_n_v[p_Vid->view_id] = p_Vid->p_Stats->bit_ctr_filler_data_v[p_Vid->view_id];
1078     }
1079   }
1080 #endif
1081 }
1082 
update_idr_order_stats(VideoParameters * p_Vid)1083 void update_idr_order_stats(VideoParameters *p_Vid)
1084 {
1085   if ( p_Vid->fld_flag ) // need to check both fields here
1086   {
1087     // top field picture
1088     if ( p_Vid->p_curr_frm_struct->p_top_fld_pic->p_Slice[0].type == I_SLICE && p_Vid->p_curr_frm_struct->p_top_fld_pic->nal_ref_idc)
1089     {
1090       p_Vid->lastINTRA       = imax(p_Vid->lastINTRA, p_Vid->frame_no);
1091       p_Vid->lastIntraNumber = p_Vid->curr_frm_idx;
1092       if ( p_Vid->p_curr_frm_struct->p_top_fld_pic->idr_flag )
1093       {
1094         p_Vid->last_idr_disp_order = imax( p_Vid->frame_no, p_Vid->last_idr_disp_order );
1095         p_Vid->last_idr_code_order = p_Vid->curr_frm_idx;
1096       }
1097     }
1098     // bottom field picture
1099     if ( p_Vid->p_curr_frm_struct->p_bot_fld_pic->p_Slice[0].type == I_SLICE && p_Vid->p_curr_frm_struct->p_bot_fld_pic->nal_ref_idc)
1100     {
1101       p_Vid->lastINTRA       = imax(p_Vid->lastINTRA, p_Vid->frame_no);
1102       p_Vid->lastIntraNumber = p_Vid->curr_frm_idx;
1103       if ( p_Vid->p_curr_frm_struct->p_bot_fld_pic->idr_flag )
1104       {
1105         p_Vid->last_idr_disp_order = imax( p_Vid->frame_no, p_Vid->last_idr_disp_order );
1106         p_Vid->last_idr_code_order = p_Vid->curr_frm_idx;
1107       }
1108     }
1109   }
1110   else
1111   {
1112     // frame picture
1113     if ( p_Vid->p_curr_frm_struct->p_frame_pic->p_Slice[0].type == I_SLICE && p_Vid->p_curr_frm_struct->p_frame_pic->nal_ref_idc)
1114     {
1115       p_Vid->lastINTRA       = imax(p_Vid->lastINTRA, p_Vid->frame_no);
1116       p_Vid->lastIntraNumber = p_Vid->curr_frm_idx;
1117       if ( p_Vid->p_curr_frm_struct->p_frame_pic->idr_flag )
1118       {
1119         p_Vid->last_idr_disp_order = imax( p_Vid->frame_no, p_Vid->last_idr_disp_order );
1120         p_Vid->last_idr_code_order = p_Vid->curr_frm_idx;
1121       }
1122     }
1123   }
1124 }
1125 
update_video_stats(VideoParameters * p_Vid)1126 int update_video_stats(VideoParameters *p_Vid)
1127 {
1128   int bits = 0;
1129   InputParameters *p_Inp = p_Vid->p_Inp;
1130 
1131   //Rate control
1132   if(p_Inp->RCEnable)
1133   {
1134     if ((!p_Inp->PicInterlace) && (!p_Inp->MbInterlace))
1135     {
1136       bits = (int) (p_Vid->p_Stats->bit_ctr - p_Vid->p_Stats->bit_ctr_n)
1137         + (int)( p_Vid->p_Stats->bit_ctr_filler_data - p_Vid->p_Stats->bit_ctr_filler_data_n );
1138     }
1139     else if ( p_Inp->RCUpdateMode <= MAX_RC_MODE )
1140     {
1141       bits = (int)(p_Vid->p_Stats->bit_ctr - (p_Vid->p_rc_quad->Pprev_bits))
1142         + (int)( p_Vid->p_Stats->bit_ctr_filler_data - p_Vid->p_Stats->bit_ctr_filler_data_n ); // used for rate control update
1143       p_Vid->p_rc_quad->Pprev_bits = p_Vid->p_Stats->bit_ctr + p_Vid->p_Stats->bit_ctr_filler_data;
1144     }
1145     else
1146     {
1147       bits = (int) (p_Vid->p_Stats->bit_ctr - p_Vid->p_Stats->bit_ctr_n)
1148         + (int)( p_Vid->p_Stats->bit_ctr_filler_data - p_Vid->p_Stats->bit_ctr_filler_data_n );
1149     }
1150   }
1151 
1152   p_Vid->p_Stats->bit_counter[p_Vid->type] += p_Vid->p_Stats->bit_ctr - p_Vid->p_Stats->bit_ctr_n;
1153 
1154 #if (MVC_EXTENSION_ENABLE)
1155   if ( p_Inp->num_of_views == 2 )
1156   {
1157     if ( p_Vid->fld_flag )
1158     {
1159       if ( p_Vid->view_id == 1 )
1160       {
1161         int prev_type = (p_Vid->p_pred->p_frm_mvc + ( (p_Vid->curr_frm_idx << 1) % p_Vid->p_pred->num_frames_mvc ))->type;
1162 
1163         p_Vid->p_Stats->bit_counter_v[0][prev_type]   += p_Vid->p_Stats->bit_ctr_v[0] - p_Vid->p_Stats->bit_ctr_n_v[0];
1164         p_Vid->p_Stats->bit_counter_v[1][p_Vid->type] += p_Vid->p_Stats->bit_ctr_v[1] - p_Vid->p_Stats->bit_ctr_n_v[1];
1165       }
1166     }
1167     else
1168     {
1169       p_Vid->p_Stats->bit_counter_v[p_Vid->view_id][p_Vid->type] += p_Vid->p_Stats->bit_ctr_v[p_Vid->view_id] - p_Vid->p_Stats->bit_ctr_n_v[p_Vid->view_id];
1170     }
1171   }
1172 #endif
1173 
1174   return bits;
1175 }
1176 
1177 /*!
1178  ************************************************************************
1179  * \brief
1180  *    Encodes one frame
1181  ************************************************************************
1182  */
encode_one_frame(VideoParameters * p_Vid,InputParameters * p_Inp)1183 int encode_one_frame (VideoParameters *p_Vid, InputParameters *p_Inp)
1184 {
1185   int i;
1186   int nplane;
1187 
1188   //Rate control
1189   int bits = 0;
1190 
1191   TIME_T start_time;
1192   TIME_T end_time;
1193   int64  tmp_time;
1194 
1195   p_Vid->me_time = 0;
1196   p_Vid->rd_pass = 0;
1197 
1198   if( (p_Inp->separate_colour_plane_flag != 0) )
1199   {
1200     for( nplane=0; nplane<MAX_PLANE; nplane++ ){
1201       p_Vid->enc_frame_picture_JV[nplane] = NULL;
1202     }
1203   }
1204 
1205   for (i = 0; i < 6; i++)
1206     p_Vid->enc_frame_picture[i]  = NULL;
1207 
1208   gettime(&start_time);          // start time in ms
1209 
1210   //Rate control
1211   p_Vid->write_macroblock = FALSE;
1212   /*
1213   //Shankar Regunathan (Oct 2002)
1214   //Prepare Panscanrect SEI payload
1215   UpdatePanScanRectInfo (p_SEI);
1216   //Prepare Arbitrarydata SEI Payload
1217   UpdateUser_data_unregistered (p_SEI);
1218   //Prepare Registered data SEI Payload
1219   UpdateUser_data_registered_itu_t_t35 (p_SEI);
1220   //Prepare RandomAccess SEI Payload
1221   UpdateRandomAccess (p_Vid);
1222   */
1223 
1224   put_buffer_frame (p_Vid);    // sets the pointers to the frame structures
1225                                // (and not to one of the field structures)
1226   init_frame (p_Vid, p_Inp);
1227 
1228   if (p_Inp->enable_32_pulldown)
1229   {
1230     if ( !read_input_data_32pulldown (p_Vid) )
1231     {
1232       return 0;
1233     }
1234   }
1235   else
1236   {
1237     if ( !read_input_data (p_Vid) )
1238     {
1239       return 0;
1240     }
1241   }
1242 
1243   process_image(p_Vid, p_Inp);
1244   pad_borders (p_Inp->output, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr, p_Vid->imgData.frm_data);
1245 
1246 #if (MVC_EXTENSION_ENABLE)
1247   if(p_Inp->num_of_views==1 || p_Vid->view_id==0)
1248   {
1249     p_Vid->field_pic_ptr = p_Vid->field_pic1;
1250   }
1251   else
1252   {
1253     p_Vid->field_pic_ptr = p_Vid->field_pic2;
1254   }
1255 #endif
1256 
1257 
1258   // Following code should consider optimal coding mode. Currently also does not support
1259   // multiple slices per frame.
1260   p_Vid->p_Dist->frame_ctr++;
1261 #if (MVC_EXTENSION_ENABLE)
1262   if (p_Inp->num_of_views == 2)
1263   {
1264     p_Vid->p_Dist->frame_ctr_v[p_Vid->view_id]++;
1265   }
1266 #endif
1267 
1268   if(p_Vid->type == SP_SLICE)
1269   {
1270     if(p_Inp->sp2_frame_indicator)
1271     { // switching SP frame encoding
1272       p_Vid->sp2_frame_indicator = TRUE;
1273       read_SP_coefficients(p_Vid, p_Inp);
1274     }
1275   }
1276   else
1277   {
1278     p_Vid->sp2_frame_indicator = FALSE;
1279   }
1280 
1281   if ( p_Inp->WPMCPrecision )
1282   {
1283     wpxInitWPXPasses(p_Vid, p_Inp);
1284     p_Vid->pWPX->curr_wp_rd_pass = p_Vid->pWPX->wp_rd_passes;
1285     p_Vid->pWPX->curr_wp_rd_pass->algorithm = WP_REGULAR;
1286   }
1287 
1288   if (p_Inp->PicInterlace == FIELD_CODING)
1289     perform_encode_field(p_Vid);
1290   else
1291     perform_encode_frame(p_Vid);
1292 
1293   p_Vid->p_Stats->frame_counter++;
1294   p_Vid->p_Stats->frame_ctr[p_Vid->type]++;
1295 
1296   // Here, p_Vid->structure may be either FRAME or BOTTOM FIELD depending on whether AFF coding is used
1297   // The picture structure decision changes really only the fld_flag
1298   write_frame_picture(p_Vid);
1299 
1300 #if (MVC_EXTENSION_ENABLE)
1301   if(p_Inp->num_of_views==2)
1302   {
1303     // view_id 1 will follow view_id 0 anchor_pic_flag value
1304     if((p_Vid->anchor_pic_flag[0]==1) && (p_Vid->view_id&1)==0)
1305     {
1306       p_Vid->prev_view_is_anchor = 1;
1307     }
1308     else
1309     {
1310       p_Vid->prev_view_is_anchor = 0;
1311     }
1312   }
1313 #endif
1314 
1315   //Need slice data for deblocking in UpdateDecoders
1316   if (p_Inp->PicInterlace == FRAME_CODING)
1317   {
1318     if ((p_Inp->rdopt == 3) && (p_Inp->de == LLN) && (p_Vid->nal_reference_idc != 0))
1319     {
1320       UpdateDecoders (p_Vid, p_Inp, p_Vid->enc_picture);      // simulate packet losses and move decoded image to reference buffers
1321     }
1322 
1323     if (p_Inp->RestrictRef)
1324       UpdatePixelMap (p_Vid, p_Inp);
1325   }
1326 
1327 #if OUTPUT_REF_LIST
1328   {
1329     int i;
1330     printf( " [L0]: " );
1331     for ( i = 0; i<p_Vid->currentSlice->listXsize[0]; i++ )
1332     {
1333       printf( "%d ", p_Vid->currentSlice->listX[0][i]->poc / 2 ); // may not work with field
1334     }
1335     printf( " [L1]: " );
1336     for ( i = 0; i<p_Vid->currentSlice->listXsize[1]; i++ )
1337     {
1338       printf( "%d ", p_Vid->currentSlice->listX[1][i]->poc / 2 ); // may not work with field
1339     }
1340     printf("\n");
1341   }
1342 #endif
1343 
1344   free_slice_data(p_Vid);
1345 
1346   //Rate control
1347   if ( p_Inp->RCEnable )
1348   {
1349     // we could add here a function pointer!
1350     bits = (int)( p_Vid->p_Stats->bit_ctr - p_Vid->p_Stats->bit_ctr_n )
1351       + (int)( p_Vid->p_Stats->bit_ctr_filler_data - p_Vid->p_Stats->bit_ctr_filler_data_n );
1352 
1353     if ( p_Inp->RCUpdateMode <= MAX_RC_MODE )
1354       p_Vid->rc_update_pict_frame_ptr(p_Vid, p_Inp, p_Vid->p_rc_quad, p_Vid->p_rc_gen, bits);
1355 
1356   }
1357 
1358   compute_distortion(p_Vid, &p_Vid->imgData);
1359 
1360   // redundant pictures: save reconstruction to calculate SNR and replace reference picture
1361   if(p_Inp->redundant_pic_flag)
1362   {
1363     storeRedundantFrame(p_Vid);
1364   }
1365 
1366   //printf("view_id:%d, frame_num:%d, poc:%d\n", p_Vid->view_id, (int)p_Vid->enc_picture->frame_num, p_Vid->enc_picture->poc);
1367   store_coded_picture(p_Vid->p_Dpb_layer[p_Vid->view_id]);
1368 
1369 
1370   p_Vid->AverageFrameQP = isign(p_Vid->SumFrameQP) * ((iabs(p_Vid->SumFrameQP) + (int) (p_Vid->FrameSizeInMbs >> 1))/ (int) p_Vid->FrameSizeInMbs);
1371 
1372   if ( p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE && p_Vid->type != B_SLICE && p_Inp->basicunit < p_Vid->FrameSizeInMbs )
1373     p_Vid->p_rc_quad->CurrLastQP = p_Vid->AverageFrameQP + p_Vid->p_rc_quad->bitdepth_qp_scale;
1374 
1375 #ifdef _LEAKYBUCKET_
1376   // Store bits used for this frame and increment counter of no. of coded frames
1377   if (!p_Vid->redundant_coding)
1378   {
1379     p_Vid->Bit_Buffer[p_Vid->total_frame_buffer++] = (long) (p_Vid->p_Stats->bit_ctr - p_Vid->p_Stats->bit_ctr_n)
1380       + (long)( p_Vid->p_Stats->bit_ctr_filler_data - p_Vid->p_Stats->bit_ctr_filler_data_n );
1381   }
1382 #endif
1383 
1384   // POC200301: Verify that POC coding type 2 is not used if more than one consecutive
1385   // non-reference frame is requested or if decoding order is different from output order
1386   if (p_Vid->pic_order_cnt_type == 2)
1387   {
1388     if (!p_Vid->nal_reference_idc)
1389       p_Vid->consecutive_non_reference_pictures++;
1390     else
1391       p_Vid->consecutive_non_reference_pictures = 0;
1392 
1393     if (p_Vid->frame_no < p_Vid->prev_frame_no || p_Vid->consecutive_non_reference_pictures>1)
1394       error("POC type 2 cannot be applied for the coding pattern where the encoding /decoding order of pictures are different from the output order.\n", -1);
1395     p_Vid->prev_frame_no = p_Vid->frame_no;
1396   }
1397 
1398   gettime(&end_time);    // end time in ms
1399   tmp_time  = timediff(&start_time, &end_time);
1400   p_Vid->tot_time += tmp_time;
1401   tmp_time  = timenorm(tmp_time);
1402   p_Vid->me_time   = timenorm(p_Vid->me_time);
1403   if (p_Vid->p_Stats->bit_ctr_parametersets_n!=0 && p_Inp->Verbose != 3)
1404     ReportNALNonVLCBits(p_Vid, tmp_time);
1405 
1406 #if (MVC_EXTENSION_ENABLE)
1407   if (p_Vid->curr_frm_idx == 0 && !p_Vid->view_id)
1408 #else
1409   if (p_Vid->curr_frm_idx == 0)
1410 #endif
1411     ReportFirstframe(p_Vid, tmp_time);
1412   else
1413   {
1414     bits = update_video_stats(p_Vid);
1415 
1416     switch (p_Vid->type)
1417     {
1418     case I_SLICE:
1419     case SI_SLICE:
1420       ReportI(p_Vid, tmp_time);
1421       break;
1422     case B_SLICE:
1423       ReportB(p_Vid, tmp_time);
1424       break;
1425     case SP_SLICE:
1426       ReportP(p_Vid, tmp_time);
1427       break;
1428     case P_SLICE:
1429     default:
1430       ReportP(p_Vid, tmp_time);
1431     }
1432   }
1433 
1434   if (p_Inp->Verbose == 0)
1435   {
1436     //for (i = 0; i <= (p_Vid->number & 0x0F); i++)
1437     //printf(".");
1438     //printf("                              \r");
1439     printf("Completed Encoding Frame %05d.\r", p_Vid->frame_no);
1440   }
1441   // Flush output statistics
1442   fflush(stdout);
1443 
1444   //Rate control
1445   if(p_Inp->RCEnable)
1446     p_Vid->rc_update_picture_ptr( p_Vid, p_Inp, bits );
1447 
1448   // update bit counters
1449   update_bitcounter_stats(p_Vid);
1450 
1451   update_idr_order_stats(p_Vid);
1452 
1453   return 1;
1454 }
1455 
1456 
1457 /*!
1458  ************************************************************************
1459  * \brief
1460  *    This function write out a picture
1461  * \return
1462  *    0 if OK,                                                         \n
1463  *    1 in case of error
1464  *
1465  ************************************************************************
1466  */
1467 #if (MVC_EXTENSION_ENABLE)
writeout_picture(VideoParameters * p_Vid,Picture * pic,int is_bottom)1468 static void writeout_picture(VideoParameters *p_Vid, Picture *pic, int is_bottom)
1469 {
1470   int partition, slice, bits;
1471   Slice  *currSlice;
1472   NALU_t *nalu = NULL;
1473 
1474   p_Vid->currentPicture = pic;
1475 
1476   // loop over all slices of the picture
1477   for (slice=0; slice < pic->no_slices; slice++)
1478   {
1479     currSlice = pic->slices[slice];
1480 
1481     // loop over the partitions
1482     for (partition=0; partition < currSlice->max_part_nr; partition++)
1483     {
1484       // write only if the partition has content
1485       if ( currSlice->partArr[partition].bitstream->write_flag )
1486       {
1487         if(p_Vid->p_Inp->num_of_views==2 && p_Vid->view_id==0)
1488         {
1489           nalu = AllocNALU(MAXNALUSIZE);
1490           nalu->startcodeprefix_len = 4;
1491           nalu->nal_unit_type       = NALU_TYPE_PREFIX;
1492           nalu->nal_reference_idc   = NALU_PRIORITY_HIGHEST;
1493           nalu->svc_extension_flag  = 0;
1494           nalu->non_idr_flag        = p_Vid->non_idr_flag[is_bottom];
1495           nalu->priority_id         = p_Vid->priority_id;
1496           nalu->view_id             = p_Vid->view_id;
1497           nalu->temporal_id         = p_Vid->temporal_id;
1498           nalu->anchor_pic_flag     = p_Vid->anchor_pic_flag[is_bottom];
1499           nalu->inter_view_flag     = p_Vid->inter_view_flag[is_bottom];
1500           nalu->reserved_one_bit    = 1;
1501 
1502           bits = p_Vid->WriteNALU (p_Vid, nalu, p_Vid->f_out);
1503           p_Vid->p_Stats->bit_ctr += bits;
1504           p_Vid->p_Stats->bit_ctr_v[0] += bits;
1505           FreeNALU(nalu);
1506         }
1507 
1508         bits = p_Vid->WriteNALU (p_Vid, currSlice->partArr[partition].nal_unit, p_Vid->f_out);
1509         p_Vid->p_Stats->bit_ctr += bits;
1510         if ( p_Vid->p_Inp->num_of_views == 2 )
1511         {
1512           p_Vid->p_Stats->bit_ctr_v[p_Vid->view_id] += bits;
1513         }
1514       }
1515     }
1516   }
1517 }
1518 #else
writeout_picture(VideoParameters * p_Vid,Picture * pic)1519 static void writeout_picture(VideoParameters *p_Vid, Picture *pic)
1520 {
1521   int partition, slice, bits;
1522   Slice  *currSlice;
1523 
1524   p_Vid->currentPicture = pic;
1525 
1526   // loop over all slices of the picture
1527   for (slice=0; slice < pic->no_slices; slice++)
1528   {
1529     currSlice = pic->slices[slice];
1530 
1531     // loop over the partitions
1532     for (partition=0; partition < currSlice->max_part_nr; partition++)
1533     {
1534       // write only if the partition has content
1535       if (currSlice->partArr[partition].bitstream->write_flag )
1536       {
1537         bits = p_Vid->WriteNALU (p_Vid, currSlice->partArr[partition].nal_unit, p_Vid->f_out);
1538         p_Vid->p_Stats->bit_ctr += bits;
1539 
1540       }
1541     }
1542   }
1543 }
1544 #endif
1545 
copy_params(VideoParameters * p_Vid,StorablePicture * enc_picture,seq_parameter_set_rbsp_t * active_sps)1546 void copy_params(VideoParameters *p_Vid, StorablePicture *enc_picture, seq_parameter_set_rbsp_t *active_sps)
1547 {
1548   enc_picture->frame_mbs_only_flag = active_sps->frame_mbs_only_flag;
1549   enc_picture->frame_cropping_flag = active_sps->frame_cropping_flag;
1550   enc_picture->chroma_format_idc   = active_sps->chroma_format_idc;
1551   enc_picture->chroma_mask_mv_x    = p_Vid->chroma_mask_mv_x;
1552   enc_picture->chroma_mask_mv_y    = p_Vid->chroma_mask_mv_y;
1553   enc_picture->chroma_shift_y      = p_Vid->chroma_shift_y;
1554   enc_picture->chroma_shift_x      = p_Vid->chroma_shift_x;
1555   enc_picture->otf_flag            = p_Vid->p_Inp->OnTheFlyFractMCP;
1556 
1557   if (active_sps->frame_cropping_flag)
1558   {
1559     enc_picture->frame_crop_left_offset   = active_sps->frame_crop_left_offset;
1560     enc_picture->frame_crop_right_offset  = active_sps->frame_crop_right_offset;
1561     enc_picture->frame_crop_top_offset    = active_sps->frame_crop_top_offset;
1562     enc_picture->frame_crop_bottom_offset = active_sps->frame_crop_bottom_offset;
1563   }
1564   else
1565   {
1566     enc_picture->frame_crop_left_offset   = 0;
1567     enc_picture->frame_crop_right_offset  = 0;
1568     enc_picture->frame_crop_top_offset    = 0;
1569     enc_picture->frame_crop_bottom_offset = 0;
1570   }
1571 }
1572 
1573 /*!
1574  ************************************************************************
1575  * \brief
1576  *    Prepare and allocate an encoded frame picture structure
1577  ************************************************************************
1578  */
prepare_enc_frame_picture(VideoParameters * p_Vid,StorablePicture ** stored_pic)1579 static void prepare_enc_frame_picture (VideoParameters *p_Vid, StorablePicture **stored_pic)
1580 {
1581   InputParameters *p_Inp = p_Vid->p_Inp;
1582   (*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);
1583 
1584   p_Vid->ThisPOC             = p_Vid->framepoc;
1585   (*stored_pic)->poc         = p_Vid->framepoc;
1586   (*stored_pic)->top_poc     = p_Vid->toppoc;
1587   (*stored_pic)->bottom_poc  = p_Vid->bottompoc;
1588   (*stored_pic)->frame_poc   = p_Vid->framepoc;
1589   (*stored_pic)->pic_num     = p_Vid->frame_num;
1590   (*stored_pic)->frame_num   = p_Vid->frame_num;
1591   (*stored_pic)->coded_frame = 1;
1592   (*stored_pic)->mb_aff_frame_flag = p_Vid->mb_aff_frame_flag = (Boolean) (p_Inp->MbInterlace != FRAME_CODING);
1593 
1594   p_Vid->get_mb_block_pos    = p_Vid->mb_aff_frame_flag ? get_mb_block_pos_mbaff : get_mb_block_pos_normal;
1595   p_Vid->getNeighbour        = p_Vid->mb_aff_frame_flag ? getAffNeighbour : getNonAffNeighbour;
1596   p_Vid->enc_picture         = *stored_pic;
1597 
1598   copy_params(p_Vid, p_Vid->enc_picture, p_Vid->active_sps);
1599 }
1600 
calc_picture_bits(Picture * frame)1601 static void calc_picture_bits(Picture *frame)
1602 {
1603   int i, j;
1604   Slice *thisSlice = NULL;
1605 
1606   frame->bits_per_picture = 0;
1607 
1608   for ( i = 0; i < frame->no_slices; i++ )
1609   {
1610     thisSlice = frame->slices[i];
1611 
1612     for ( j = 0; j < thisSlice->max_part_nr; j++ )
1613       frame->bits_per_picture += 8 * ((thisSlice->partArr[j]).bitstream)->byte_pos;
1614   }
1615 }
1616 
1617 /*!
1618  ************************************************************************
1619  * \brief
1620  *    Encodes a frame picture
1621  ************************************************************************
1622  */
frame_picture(VideoParameters * p_Vid,Picture * frame,ImageData * imgData,int rd_pass)1623 void frame_picture (VideoParameters *p_Vid, Picture *frame, ImageData *imgData, int rd_pass)
1624 {
1625   int nplane;
1626   InputParameters *p_Inp = p_Vid->p_Inp;
1627   p_Vid->rd_pass = rd_pass;
1628   p_Vid->SumFrameQP = 0;
1629   p_Vid->num_ref_idx_l0_active = 0;
1630   p_Vid->num_ref_idx_l1_active = 0;
1631   p_Vid->p_curr_pic = p_Vid->p_curr_frm_struct->p_frame_pic;
1632   p_Vid->structure = FRAME;
1633   p_Vid->PicSizeInMbs = p_Vid->FrameSizeInMbs;
1634   //set mv limits to frame type
1635   update_mv_limits(p_Vid, FALSE);
1636 
1637   InitWP(p_Vid, p_Inp, 0);
1638   if(rd_pass == 0 && p_Vid->wp_parameters_set == 0)
1639     ResetWP(p_Vid, p_Inp);
1640 
1641   if( (p_Inp->separate_colour_plane_flag != 0) )
1642   {
1643     for( nplane=0; nplane<MAX_PLANE; nplane++ )
1644     {
1645       prepare_enc_frame_picture( p_Vid, &p_Vid->enc_frame_picture_JV[nplane] );
1646     }
1647   }
1648   else
1649   {
1650     prepare_enc_frame_picture( p_Vid, &p_Vid->enc_frame_picture[rd_pass] );
1651   }
1652 
1653   p_Vid->fld_flag = FALSE;
1654   code_a_picture(p_Vid, frame);
1655 
1656   if( (p_Inp->separate_colour_plane_flag != 0) )
1657   {
1658     make_frame_picture_JV(p_Vid);
1659   }
1660 
1661   calc_picture_bits(frame);
1662 
1663   if (p_Vid->structure==FRAME)
1664   {
1665     find_distortion (p_Vid, imgData);
1666     frame->distortion = p_Vid->p_Dist->metric[SSE];
1667   }
1668 }
1669 
1670 
1671 /*!
1672  ************************************************************************
1673  * \brief
1674  *    Encodes a field picture, consisting of top and bottom field
1675  ************************************************************************
1676  */
field_picture(VideoParameters * p_Vid,Picture * top,Picture * bottom)1677 static void field_picture (VideoParameters *p_Vid, Picture *top, Picture *bottom)
1678 {
1679   InputParameters *p_Inp = p_Vid->p_Inp;
1680   //Rate control
1681   int TopFieldBits;
1682 
1683   p_Vid->SumFrameQP = 0;
1684   p_Vid->num_ref_idx_l0_active = 0;
1685   p_Vid->num_ref_idx_l1_active = 0;
1686 
1687   //set mv limits to field type
1688   update_mv_limits(p_Vid, TRUE);
1689 
1690   p_Vid->height    = (p_Inp->output.height[0] + p_Vid->auto_crop_bottom) >> 1;
1691   p_Vid->height_cr = p_Vid->height_cr_frame >> 1;
1692   p_Vid->fld_flag  = TRUE;
1693   p_Vid->PicSizeInMbs = p_Vid->FrameSizeInMbs >> 1;
1694   // Top field
1695 
1696   p_Vid->enc_field_picture[0]              = alloc_storable_picture (p_Vid, (PictureStructure) p_Vid->structure, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr);
1697   p_Vid->enc_field_picture[0]->poc         = p_Vid->toppoc;
1698   p_Vid->enc_field_picture[0]->frame_poc   = p_Vid->toppoc;
1699   /*Potentially buggy to not divide by num_of_layers */
1700   p_Vid->enc_field_picture[0]->pic_num     = p_Vid->frame_num;
1701   p_Vid->enc_field_picture[0]->frame_num   = p_Vid->frame_num;
1702   p_Vid->enc_field_picture[0]->coded_frame = 0;
1703   p_Vid->enc_field_picture[0]->mb_aff_frame_flag = p_Vid->mb_aff_frame_flag = FALSE;
1704   p_Vid->get_mb_block_pos = get_mb_block_pos_normal;
1705   p_Vid->getNeighbour = getNonAffNeighbour;
1706   p_Vid->ThisPOC = p_Vid->toppoc;
1707 
1708   p_Vid->p_curr_pic = p_Vid->p_curr_frm_struct->p_top_fld_pic;
1709   p_Vid->structure = TOP_FIELD;
1710   p_Vid->enc_picture = p_Vid->enc_field_picture[0];
1711   copy_params(p_Vid, p_Vid->enc_picture, p_Vid->active_sps);
1712 
1713   put_buffer_top (p_Vid);
1714   init_field (p_Vid, p_Inp);
1715   set_slice_type(p_Vid, p_Inp, p_Vid->p_curr_pic->p_Slice[0].type);
1716 
1717   p_Vid->fld_flag = TRUE;
1718 
1719   //Rate control
1720   if(p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE)
1721     rc_init_top_field(p_Vid, p_Inp);
1722 
1723   code_a_picture(p_Vid, top);
1724   p_Vid->enc_picture->structure = TOP_FIELD;
1725   store_picture_in_dpb(p_Vid->p_Dpb_layer[p_Vid->view_id], p_Vid->enc_field_picture[0], &p_Inp->output);
1726 
1727 #if (MVC_EXTENSION_ENABLE)
1728   if ( is_MVC_profile(p_Vid->active_sps->profile_idc) && p_Vid->proc_picture)
1729   {
1730     store_proc_picture_in_dpb (p_Vid->p_Dpb_layer[1], p_Vid->proc_picture, &p_Inp->output);
1731     p_Vid->proc_picture = NULL;
1732   }
1733 #endif
1734   calc_picture_bits(top);
1735 
1736   //Rate control
1737   TopFieldBits=top->bits_per_picture;
1738 
1739   //  Bottom field
1740   p_Vid->enc_field_picture[1]  = alloc_storable_picture (p_Vid, (PictureStructure) p_Vid->structure, p_Vid->width, p_Vid->height, p_Vid->width_cr, p_Vid->height_cr);
1741   p_Vid->enc_field_picture[1]->poc=p_Vid->bottompoc;
1742   p_Vid->enc_field_picture[1]->frame_poc = p_Vid->bottompoc;
1743   p_Vid->enc_field_picture[1]->pic_num = p_Vid->frame_num;
1744   p_Vid->enc_field_picture[1]->frame_num = p_Vid->frame_num;
1745   p_Vid->enc_field_picture[1]->coded_frame = 0;
1746   p_Vid->enc_field_picture[1]->mb_aff_frame_flag = p_Vid->mb_aff_frame_flag = FALSE;
1747   p_Vid->get_mb_block_pos = get_mb_block_pos_normal;
1748   p_Vid->getNeighbour = getNonAffNeighbour;
1749 
1750   p_Vid->ThisPOC = p_Vid->bottompoc;
1751   p_Vid->p_curr_pic = p_Vid->p_curr_frm_struct->p_bot_fld_pic;
1752   p_Vid->structure = BOTTOM_FIELD;
1753   p_Vid->enc_picture = p_Vid->enc_field_picture[1];
1754   copy_params(p_Vid, p_Vid->enc_picture, p_Vid->active_sps);
1755   put_buffer_bot (p_Vid);
1756 
1757   init_field (p_Vid, p_Inp);
1758   set_slice_type(p_Vid, p_Inp, p_Vid->p_curr_pic->p_Slice[0].type);
1759 
1760   p_Vid->fld_flag = TRUE;
1761 
1762   //Rate control
1763   if(p_Inp->RCEnable && p_Inp->RCUpdateMode <= MAX_RC_MODE)
1764     rc_init_bottom_field( p_Vid, p_Inp, TopFieldBits );
1765 
1766   p_Vid->enc_picture->structure = BOTTOM_FIELD;
1767   code_a_picture(p_Vid, bottom);
1768 
1769   calc_picture_bits(bottom);
1770 
1771   // the distortion for a field coded frame (consisting of top and bottom field)
1772   // lives in the top->distortion variables, the bottom-> are dummies
1773   distortion_fld (p_Vid, p_Inp, top, &p_Vid->imgData);
1774 }
1775 
1776 /*!
1777  ************************************************************************
1778  * \brief
1779  *    form frame picture from two field pictures
1780  ************************************************************************
1781  */
combine_field(VideoParameters * p_Vid)1782 static void combine_field(VideoParameters *p_Vid)
1783 {
1784   int i, k;
1785 
1786   for (i = 0; i < (p_Vid->height >> 1); i++)
1787   {
1788     memcpy(p_Vid->imgY_com[i*2],     p_Vid->enc_field_picture[0]->imgY[i], p_Vid->width*sizeof(imgpel));     // top field
1789     memcpy(p_Vid->imgY_com[i*2 + 1], p_Vid->enc_field_picture[1]->imgY[i], p_Vid->width*sizeof(imgpel)); // bottom field
1790   }
1791 
1792   if (p_Vid->yuv_format != YUV400)
1793   {
1794     for (k = 0; k < 2; k++)
1795     {
1796       for (i = 0; i < (p_Vid->height_cr >> 1); i++)
1797       {
1798         memcpy(p_Vid->imgUV_com[k][i * 2],     p_Vid->enc_field_picture[0]->imgUV[k][i], p_Vid->width_cr*sizeof(imgpel));
1799         memcpy(p_Vid->imgUV_com[k][i * 2 + 1], p_Vid->enc_field_picture[1]->imgUV[k][i], p_Vid->width_cr*sizeof(imgpel));
1800       }
1801     }
1802   }
1803 }
1804 
1805 /*!
1806  ************************************************************************
1807  * \brief
1808  *    Distortion Field
1809  ************************************************************************
1810  */
distortion_fld(VideoParameters * p_Vid,InputParameters * p_Inp,Picture * field_pic,ImageData * imgData)1811 static void distortion_fld (VideoParameters *p_Vid, InputParameters *p_Inp, Picture *field_pic, ImageData *imgData)
1812 {
1813   p_Vid->height    = (p_Inp->output.height[0] + p_Vid->auto_crop_bottom);
1814   p_Vid->height_cr = p_Vid->height_cr_frame;
1815 
1816   combine_field (p_Vid);
1817 
1818   p_Vid->pCurImg   = imgData->frm_data[0];
1819   p_Vid->pImgOrg[0] = imgData->frm_data[0];
1820 
1821   if (p_Inp->output.yuv_format != YUV400)
1822   {
1823     p_Vid->pImgOrg[1] = imgData->frm_data[1];
1824     p_Vid->pImgOrg[2] = imgData->frm_data[2];
1825   }
1826 
1827   find_distortion (p_Vid, imgData);   // find snr from original frame picture
1828   field_pic->distortion = p_Vid->p_Dist->metric[SSE];
1829 }
1830 
1831 
1832 /*!
1833  ************************************************************************
1834  * \brief
1835  *    RD decision of frame and field coding
1836  ************************************************************************
1837  */
decide_fld_frame(float snr_frame_Y,float snr_field_Y,int bit_field,int bit_frame,double lambda_picture)1838 static byte decide_fld_frame(float snr_frame_Y, float snr_field_Y, int bit_field, int bit_frame, double lambda_picture)
1839 {
1840   double cost_frame, cost_field;
1841 
1842   cost_frame = bit_frame * lambda_picture + snr_frame_Y;
1843   cost_field = bit_field * lambda_picture + snr_field_Y;
1844 
1845   if (cost_field > cost_frame)
1846     return FALSE;
1847   else
1848     return TRUE;
1849 }
1850 
1851 /*!
1852  ************************************************************************
1853  * \brief
1854  *    Picture Structure Decision
1855  ************************************************************************
1856  */
picture_structure_decision(VideoParameters * p_Vid,Picture * frame,Picture * top,Picture * bot)1857 static byte picture_structure_decision (VideoParameters *p_Vid, Picture *frame, Picture *top, Picture *bot)
1858 {
1859   double lambda_picture;
1860   float sse_frame, sse_field;
1861   int bit_frame, bit_field;
1862 
1863   lambda_picture = 0.68 * pow (2, p_Vid->bitdepth_lambda_scale + ((p_Vid->qp - SHIFT_QP) / 3.0)) * ((p_Vid->type == B_SLICE) ? 1 : 1);
1864 
1865   sse_frame = frame->distortion.value[0] + frame->distortion.value[1] + frame->distortion.value[2];
1866   //! all distrortions of a field picture are accumulated in the top field
1867   sse_field = top->distortion.value[0] + top->distortion.value[1] + top->distortion.value[2];
1868 
1869   bit_field = top->bits_per_picture + bot->bits_per_picture;
1870   bit_frame = frame->bits_per_picture;
1871   return decide_fld_frame (sse_frame, sse_field, bit_field, bit_frame, lambda_picture);
1872 }
1873 
1874 
1875 /*!
1876  ************************************************************************
1877  * \brief
1878  *    Field Mode Buffer
1879  ************************************************************************
1880  */
field_mode_buffer(VideoParameters * p_Vid)1881 static void field_mode_buffer (VideoParameters *p_Vid)
1882 {
1883   put_buffer_frame (p_Vid);
1884 }
1885 
1886 
1887 /*!
1888  ************************************************************************
1889  * \brief
1890  *    Frame Mode Buffer
1891  ************************************************************************
1892  */
frame_mode_buffer(VideoParameters * p_Vid)1893 static void frame_mode_buffer (VideoParameters *p_Vid)
1894 {
1895   InputParameters *p_Inp = p_Vid->p_Inp;
1896   put_buffer_frame (p_Vid);
1897 
1898   if ((p_Inp->PicInterlace != FRAME_CODING)||(p_Inp->MbInterlace != FRAME_CODING))
1899   {
1900     p_Vid->height = p_Vid->height / 2;
1901     p_Vid->height_cr = p_Vid->height_cr / 2;
1902 
1903     put_buffer_top (p_Vid);
1904 
1905     put_buffer_bot (p_Vid);
1906 
1907     p_Vid->height = (p_Inp->output.height[0] + p_Vid->auto_crop_bottom);
1908     p_Vid->height_cr = p_Vid->height_cr_frame;
1909 
1910     put_buffer_frame (p_Vid);
1911   }
1912 }
1913 
1914 
1915 /*!
1916  ************************************************************************
1917  * \brief
1918  *    mmco initializations should go here
1919  ************************************************************************
1920  */
init_dec_ref_pic_marking_buffer(VideoParameters * p_Vid)1921 static void init_dec_ref_pic_marking_buffer(VideoParameters *p_Vid)
1922 {
1923   p_Vid->dec_ref_pic_marking_buffer=NULL;
1924 }
1925 
1926 /*!
1927 ************************************************************************
1928 * \brief
1929 *    Init fixed QP I_SLICE
1930 ************************************************************************
1931 */
1932 
init_fixed_qp_i_slice(VideoParameters * p_Vid,InputParameters * p_Inp,FrameUnitStruct * p_cur_frm)1933 static inline void init_fixed_qp_i_slice(VideoParameters *p_Vid, InputParameters *p_Inp, FrameUnitStruct *p_cur_frm )
1934 {
1935   //QP oscillation for secondary SP frames
1936   p_Vid->qp = p_cur_frm->qp;   // set quant. parameter for I-frame
1937 
1938   if (p_Vid->redundant_coding)
1939   {
1940     //!KS: hard code qp increment
1941     p_Vid->qp = imin(p_Vid->qp + 5, 51);
1942   }
1943 }
1944 
1945 /*!
1946 ************************************************************************
1947 * \brief
1948 *    Init fixed QP P_SLICE or B_SLICE
1949 ************************************************************************
1950 */
1951 
init_fixed_qp_pb_slice(VideoParameters * p_Vid,InputParameters * p_Inp,FrameUnitStruct * p_cur_frm,int type)1952 static inline void init_fixed_qp_pb_slice(VideoParameters *p_Vid, InputParameters *p_Inp, FrameUnitStruct *p_cur_frm, int type )
1953 {
1954   //QP oscillation for secondary SP frames
1955   p_Vid->qp = p_cur_frm->qp;
1956 
1957 }
1958 
1959 /*!
1960 ************************************************************************
1961 * \brief
1962 *    Init fixed QP SP_SLICE
1963 ************************************************************************
1964 */
1965 
init_fixed_qp_sp_slice(VideoParameters * p_Vid,InputParameters * p_Inp,FrameUnitStruct * p_cur_frm)1966 static inline void init_fixed_qp_sp_slice(VideoParameters *p_Vid, InputParameters *p_Inp, FrameUnitStruct *p_cur_frm )
1967 {
1968   p_Vid->qp   = p_cur_frm->qp;
1969   p_Vid->qpsp = p_Inp->qpsp;
1970 }
1971 
1972 /*!
1973 ************************************************************************
1974 * \brief
1975 *    Initializes the quantization parameters for a new frame (no RC)
1976 *    ***Much of what happens in this function have to be moved elsewhere...***
1977 ************************************************************************
1978 */
1979 
init_fixed_qp(VideoParameters * p_Vid,InputParameters * p_Inp)1980 static void init_fixed_qp(VideoParameters *p_Vid, InputParameters *p_Inp)
1981 {
1982   FrameUnitStruct *p_cur_frm = p_Vid->p_curr_frm_struct;
1983 
1984   if (p_cur_frm->layer == 0)
1985   {
1986     switch( p_cur_frm->type )
1987     {
1988     default:
1989     case I_SLICE:
1990       init_fixed_qp_i_slice( p_Vid, p_Inp, p_cur_frm );
1991       break;
1992     case P_SLICE:
1993     case B_SLICE:
1994       init_fixed_qp_pb_slice( p_Vid, p_Inp, p_cur_frm, p_cur_frm->type );
1995       break;
1996     case SP_SLICE:
1997     case SI_SLICE:
1998       init_fixed_qp_sp_slice( p_Vid, p_Inp, p_cur_frm );
1999       break;
2000     }
2001 
2002     p_Vid->mb_y_intra = p_Vid->mb_y_upd;  //  p_Vid->mb_y_intra indicates which GOB to intra code for this frame
2003 
2004     if (p_Inp->intra_upd > 0) // if error robustness, find next GOB to update
2005     {
2006       p_Vid->mb_y_upd = (p_Vid->curr_frm_idx / p_Inp->intra_upd) % (p_Vid->height / MB_BLOCK_SIZE);
2007     }
2008   }
2009   else
2010   {
2011     p_Vid->qp = p_cur_frm->qp;
2012   }
2013 
2014   // Change QP
2015   if ( p_Inp->qp2frame && p_Inp->qp2frame < p_Vid->frame_no )
2016   {
2017     p_Vid->p_curr_frm_struct->qp = p_Vid->qp = iClip3( -p_Vid->bitdepth_luma_qp_scale, MAX_QP, p_Vid->qp + p_Inp->qp2off[p_cur_frm->type] );
2018   }
2019 }
2020 
2021 /*!
2022  ************************************************************************
2023  * \brief
2024  *    Initializes the quantization parameters for a new frame (no RC)
2025  ************************************************************************
2026  */
2027 
init_mb_line_intra_up(VideoParameters * p_Vid,InputParameters * p_Inp)2028 static void init_mb_line_intra_up(VideoParameters *p_Vid, InputParameters *p_Inp)
2029 {
2030   FrameUnitStruct *p_cur_frm = p_Vid->p_curr_frm_struct;
2031 
2032   if (p_cur_frm->layer == 0)
2033   {
2034     p_Vid->mb_y_intra = p_Vid->mb_y_upd;  //  p_Vid->mb_y_intra indicates which GOB to intra code for this frame
2035 
2036     if (p_Inp->intra_upd > 0) // if error robustness, find next GOB to update
2037     {
2038       p_Vid->mb_y_upd = (p_Vid->curr_frm_idx / p_Inp->intra_upd) % (p_Vid->width / MB_BLOCK_SIZE);
2039     }
2040   }
2041 }
2042 
2043 /*!
2044  ************************************************************************
2045  * \brief
2046  *    Initializes the parameters for a new frame
2047  ************************************************************************
2048  */
init_frame(VideoParameters * p_Vid,InputParameters * p_Inp)2049 static void init_frame (VideoParameters *p_Vid, InputParameters *p_Inp)
2050 {
2051   int i, j;
2052 
2053   p_Vid->current_mb_nr = 0;
2054   p_Vid->current_slice_nr = 0;
2055   p_Vid->p_Stats->bit_slice = 0;
2056 
2057   // The 'slice_nr' of each macroblock is set to -1 here, to guarantee the correct encoding
2058   // with FMO (if no FMO, encoding is correct without following assignment),
2059   // for which MBs may not be encoded with scan order
2060   if( (p_Inp->separate_colour_plane_flag != 0) )
2061   {
2062     for( j = 0; j < MAX_PLANE; j++ ){
2063       for(i = 0; i < ((int) (p_Vid->FrameSizeInMbs));i++)
2064         p_Vid->mb_data_JV[j][i].slice_nr = -1;
2065     }
2066   }
2067   else
2068   {
2069     for(i = 0; i < ((int) (p_Vid->FrameSizeInMbs)); i++)
2070       p_Vid->mb_data[i].slice_nr = -1;
2071   }
2072 
2073   if ( !(p_Inp->RCEnable) )
2074   {
2075     init_fixed_qp(p_Vid, p_Inp);
2076   }
2077   init_mb_line_intra_up(p_Vid, p_Inp);
2078 
2079   p_Vid->no_output_of_prior_pics_flag = 0;
2080   p_Vid->long_term_reference_flag = FALSE;
2081 
2082   init_dec_ref_pic_marking_buffer(p_Vid);
2083 
2084   if(p_Inp->WPIterMC)
2085     p_Vid->frameOffsetAvail = 0;
2086 
2087   // set parameters for direct mode and deblocking filter
2088   // currently selection is done at the frame level instead of slice level. This needs to be changed.
2089   p_Vid->direct_spatial_mv_pred_flag = (char) p_Inp->direct_spatial_mv_pred_flag;
2090 #if (MVC_EXTENSION_ENABLE)
2091   if ( (p_Inp->num_of_views == 1) || (p_Vid->view_id == 0) )
2092 #endif
2093   {
2094     p_Vid->DFDisableIdc                = (char) p_Inp->DFDisableIdc[p_Vid->nal_reference_idc > 0][p_Vid->type];
2095     p_Vid->DFAlphaC0Offset             = (char) p_Inp->DFAlpha     [p_Vid->nal_reference_idc > 0][p_Vid->type];
2096     p_Vid->DFBetaOffset                = (char) p_Inp->DFBeta      [p_Vid->nal_reference_idc > 0][p_Vid->type];
2097   }
2098 #if (MVC_EXTENSION_ENABLE)
2099   else
2100   {
2101     p_Vid->DFDisableIdc                = (char) p_Inp->EnhLayerDFDisableIdc[p_Vid->nal_reference_idc > 0][p_Vid->type];
2102     p_Vid->DFAlphaC0Offset             = (char) p_Inp->EnhLayerDFAlpha     [p_Vid->nal_reference_idc > 0][p_Vid->type];
2103     p_Vid->DFBetaOffset                = (char) p_Inp->EnhLayerDFBeta      [p_Vid->nal_reference_idc > 0][p_Vid->type];
2104   }
2105 #endif
2106   p_Vid->AdaptiveRounding            = p_Inp->AdaptiveRounding;
2107 }
2108 
2109 /*!
2110  ************************************************************************
2111  * \brief
2112  *    Initializes the parameters for a new field
2113  ************************************************************************
2114  */
init_field(VideoParameters * p_Vid,InputParameters * p_Inp)2115 static void init_field (VideoParameters *p_Vid, InputParameters *p_Inp)
2116 {
2117   FrameUnitStruct *p_cur_frm = p_Vid->p_curr_frm_struct;
2118 
2119   p_Vid->current_mb_nr = 0;
2120   p_Vid->current_slice_nr = 0;
2121   p_Vid->p_Stats->bit_slice = 0;
2122 
2123   if (!p_cur_frm->layer)
2124   {
2125     if(!p_Inp->RCEnable)                  // without using rate control
2126     {
2127       p_Vid->qp = p_cur_frm->qp;
2128       if (p_Vid->type == SP_SLICE || p_Vid->type == SI_SLICE)
2129       {
2130         p_Vid->qpsp = p_Inp->qpsp;
2131       }
2132     }
2133     init_mb_line_intra_up( p_Vid, p_Inp );
2134   }
2135   else
2136   {
2137     if(!p_Inp->RCEnable)
2138     {
2139       p_Vid->qp = p_cur_frm->qp;
2140     }
2141   }
2142 }
2143 
GenerateImagePyramid(VideoParameters * p_Vid,int size_x,int size_y,imgpel *** p_hme_img,int offset_x,int offset_y)2144 void GenerateImagePyramid(VideoParameters *p_Vid, int size_x, int size_y, imgpel ***p_hme_img, int offset_x, int offset_y)
2145 {
2146   int i, iPrevWidth, iPrevHeight,iCurrWidth, iCurrHeight;
2147   HMEInfo_t *pHMEInfo = p_Vid->pHMEInfo;
2148 
2149   iPrevWidth = size_x;
2150   iPrevHeight = size_y;
2151 
2152   for(i=1; i < pHMEInfo->iPyramidLevels; i++)
2153   {
2154     iCurrWidth  = iPrevWidth  >>1;
2155     iCurrHeight = iPrevHeight >>1;
2156 
2157     PyrDownG5x5_U8CnR((const imgpel *) *(p_hme_img[i-1]), (int) ((p_hme_img[i-1][1]-p_hme_img[i-1][0])*sizeof(imgpel)), iPrevWidth, iPrevHeight, *(p_hme_img[i]), (int) ((p_hme_img[i][1]-p_hme_img[i][0])*sizeof(imgpel)), 1);
2158 
2159     // update image dimensions
2160     iPrevWidth = iCurrWidth;
2161     iPrevHeight = iCurrHeight;
2162 
2163   }
2164 }
2165 
GenerateHMELayers(VideoParameters * p_Vid,StorablePicture * s)2166 void GenerateHMELayers(VideoParameters *p_Vid, StorablePicture *s)
2167 {
2168     //Generate Image pyramid;
2169     s->pHmeImage[0] = s->p_curr_img;
2170     GenerateImagePyramid(p_Vid, s->size_x, s->size_y, s->pHmeImage, IMG_PAD_SIZE_X, IMG_PAD_SIZE_Y);
2171     //Add Padding (we could remove this as well);
2172     GetHMEIntImagesLuma(p_Vid, s->size_x, s->size_y, s->pHmeImage);
2173  }
2174 
2175 /*!
2176  ************************************************************************
2177  * \brief
2178  *    Upsample 4 times, store them in out4x.  Color is simply copied
2179  *
2180  * \par Input:
2181  *    srcy, srcu, srcv, out4y, out4u, out4v
2182  *
2183  * \par Side Effects_
2184  *    Uses (writes) img4Y_tmp.  This should be moved to a static variable
2185  *    in this module
2186  ************************************************************************/
UnifiedOneForthPix(VideoParameters * p_Vid,StorablePicture * s)2187 void UnifiedOneForthPix ( VideoParameters *p_Vid, StorablePicture *s)
2188 {
2189   InputParameters *p_Inp = p_Vid->p_Inp;
2190   if(s->bInterpolated)
2191     return;
2192   s->bInterpolated = 1;
2193   // Y component
2194   s->p_img_sub[0] = s->imgY_sub;
2195   s->p_curr_img_sub = s->imgY_sub;
2196   s->p_curr_img = s->imgY;
2197 
2198   // derive the subpixel images for first component
2199   // No need to interpolate if intra only encoding
2200   //if (p_Inp->intra_period != 1)
2201   if ( (!p_Inp->OnTheFlyFractMCP) || (p_Inp->OnTheFlyFractMCP==OTF_L1) ) // JLT : on-the-fly
2202   {
2203     getSubImagesLuma ( p_Vid, s );
2204     // and the sub-images for U and V
2205     if ( ((p_Vid->yuv_format != YUV400) && (p_Inp->ChromaMCBuffer)) || p_Vid->P444_joined )
2206     {
2207       if (p_Vid->P444_joined)
2208       {
2209         imgpel **** p_curr_img_sub = s->p_curr_img_sub;
2210         imgpel **   p_curr_img = s->p_curr_img;
2211         //U
2212         select_plane(p_Vid, PLANE_U);
2213         s->p_curr_img_sub = s->imgUV_sub[0];
2214         s->p_curr_img = s->imgUV[0];
2215         getSubImagesLuma (p_Vid, s);
2216         //V
2217         select_plane(p_Vid, PLANE_V);
2218         s->p_curr_img_sub = s->imgUV_sub[1];
2219         s->p_curr_img = s->imgUV[1];
2220         getSubImagesLuma (p_Vid, s);
2221         //Y
2222         select_plane(p_Vid, PLANE_Y);
2223         s->p_curr_img_sub = p_curr_img_sub;
2224         s->p_curr_img = p_curr_img;
2225       }
2226       else
2227       {
2228         getSubImagesChroma( p_Vid, s );
2229       }
2230     }
2231   }
2232   else
2233   {
2234     // perform  padding ( copying borders) that is implicitly done above if p_Inp->OnTheFlyFractMCP=0
2235     OtfCompatibility_copyWithPadding( s->imgY, s->imgY, s->size_x, s->size_y, IMG_PAD_SIZE_X, IMG_PAD_SIZE_Y ) ;
2236     OtfCompatibility_copyWithPadding( s->imgUV[0], s->imgUV[0], s->size_x_cr, s->size_y_cr, p_Vid->pad_size_uv_x,p_Vid->pad_size_uv_y ) ;
2237     OtfCompatibility_copyWithPadding( s->imgUV[1], s->imgUV[1], s->size_x_cr, s->size_y_cr, p_Vid->pad_size_uv_x, p_Vid->pad_size_uv_y ) ;
2238   }
2239 }
2240 
2241 /*!
2242  ************************************************************************
2243  * \brief
2244  *    Upsample 4 times, store them in out4x.  Color is simply copied
2245  *    for 4:4:4 Independent mode
2246  *
2247  * \par Input:
2248  *    nplane
2249  *
2250  ************************************************************************/
UnifiedOneForthPix_JV(VideoParameters * p_Vid,int nplane,StorablePicture * s)2251 void UnifiedOneForthPix_JV (VideoParameters *p_Vid, int nplane, StorablePicture *s)
2252 {
2253   InputParameters *p_Inp = p_Vid->p_Inp;
2254 
2255   if( nplane == 0 )
2256   {
2257     if(s->bInterpolated)
2258       return;
2259     s->bInterpolated = 1;
2260     s->p_img[0] = s->imgY;
2261     s->p_img[1] = s->imgUV[0];
2262     s->p_img[2] = s->imgUV[1];
2263 
2264     if ( (!p_Inp->OnTheFlyFractMCP) || (p_Inp->OnTheFlyFractMCP==OTF_L1) ) // JLT : on-the-fly
2265     {
2266       s->p_img_sub[0] = s->imgY_sub;
2267       s->p_img_sub[1] = s->imgUV_sub[0];
2268       s->p_img_sub[2] = s->imgUV_sub[1];
2269     }
2270   }
2271 
2272   // derive the subpixel images for first component
2273   s->colour_plane_id = nplane;
2274   s->p_curr_img = s->p_img[nplane];
2275   s->p_curr_img_sub = s->p_img_sub[nplane];
2276 
2277   if( (!p_Inp->OnTheFlyFractMCP) || (p_Inp->OnTheFlyFractMCP==OTF_L1) )
2278   {
2279     getSubImagesLuma ( p_Vid, s );
2280   }
2281   else
2282   {
2283     // perform  padding ( copying borders) that is implicitly done above if p_Inp->OnTheFlyFractMCP=0
2284      OtfCompatibility_copyWithPadding( s->p_img[nplane], s->p_img[nplane], s->size_x, s->size_y, IMG_PAD_SIZE_X, IMG_PAD_SIZE_Y ) ;
2285   }
2286 }
2287 
2288   /*!
2289  ************************************************************************
2290  * \brief
2291  *    Just a placebo
2292  ************************************************************************
2293  */
dummy_slice_too_big(int bits_slice)2294 Boolean dummy_slice_too_big (int bits_slice)
2295 {
2296   return FALSE;
2297 }
2298 
ReportSimple(VideoParameters * p_Vid,char * pic_type,int cur_bits,DistMetric * metric,int tmp_time)2299 static void ReportSimple(VideoParameters *p_Vid, char *pic_type, int cur_bits, DistMetric *metric, int tmp_time)
2300 {
2301 #if (MVC_EXTENSION_ENABLE)
2302   if ( p_Vid->p_Inp->num_of_views == 2 )
2303   {
2304     if ( p_Vid->fld_flag && p_Vid->view_id == 0 )
2305     {
2306       p_Vid->prev_cs.frm_no_in_file = p_Vid->frm_no_in_file;
2307       p_Vid->prev_cs.view_id = p_Vid->view_id;
2308       p_Vid->prev_cs.cur_bits = cur_bits;
2309       strcpy( p_Vid->prev_cs.pic_type, pic_type );
2310       p_Vid->prev_cs.AverageFrameQP = p_Vid->AverageFrameQP;
2311       p_Vid->prev_cs.lambda = 0;
2312       p_Vid->prev_cs.psnr_value[0] = metric->value[0];
2313       p_Vid->prev_cs.psnr_value[1] = metric->value[1];
2314       p_Vid->prev_cs.psnr_value[2] = metric->value[2];
2315       p_Vid->prev_cs.ssim_value[0] = 0;
2316       p_Vid->prev_cs.ssim_value[1] = 0;
2317       p_Vid->prev_cs.ssim_value[2] = 0;
2318       p_Vid->prev_cs.tmp_time = tmp_time;
2319       p_Vid->prev_cs.me_time = (int)p_Vid->me_time;
2320       p_Vid->prev_cs.fld_flag = p_Vid->fld_flag;
2321       p_Vid->prev_cs.intras = p_Vid->intras;
2322       p_Vid->prev_cs.direct_mode = 0;
2323       p_Vid->prev_cs.num_ref_idx_l0_active = p_Vid->num_ref_idx_l0_active;
2324       p_Vid->prev_cs.num_ref_idx_l1_active = p_Vid->num_ref_idx_l1_active;
2325       p_Vid->prev_cs.rd_pass = p_Vid->rd_pass;
2326       p_Vid->prev_cs.nal_reference_idc = p_Vid->nal_reference_idc;
2327     }
2328     else if ( p_Vid->fld_flag && p_Vid->view_id == 1 )
2329     {
2330       printf ("%05d(%3s)  %1d  %8d   %2d %7.3f %7.3f %7.3f %9d %7d    %3s    %d\n",
2331         p_Vid->prev_cs.frm_no_in_file, p_Vid->prev_cs.pic_type, p_Vid->prev_cs.view_id,
2332         (int)(p_Vid->p_Stats->bit_ctr_v[0] - p_Vid->p_Stats->bit_ctr_n_v[0]) + (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[0] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[0]),
2333         p_Vid->prev_cs.AverageFrameQP,
2334         p_Vid->prev_cs.psnr_value[0], p_Vid->prev_cs.psnr_value[1], p_Vid->prev_cs.psnr_value[2],
2335         p_Vid->prev_cs.tmp_time, (int) p_Vid->prev_cs.me_time,
2336         p_Vid->prev_cs.fld_flag ? "FLD" : "FRM",
2337         p_Vid->prev_cs.nal_reference_idc);
2338       printf ("%05d(%3s)  %1d  %8d   %2d %7.3f %7.3f %7.3f %9d %7d    %3s    %d\n",
2339         p_Vid->frm_no_in_file, pic_type, p_Vid->view_id,
2340         (int)(p_Vid->p_Stats->bit_ctr_v[1] - p_Vid->p_Stats->bit_ctr_n_v[1]) + (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[1] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[1]),
2341         p_Vid->AverageFrameQP,
2342         metric->value[0], metric->value[1], metric->value[2],
2343         tmp_time, (int) p_Vid->me_time,
2344         p_Vid->fld_flag ? "FLD" : "FRM",
2345         p_Vid->nal_reference_idc);
2346     }
2347     else
2348     {
2349       printf ("%05d(%3s)  %1d  %8d   %2d %7.3f %7.3f %7.3f %9d %7d    %3s    %d\n",
2350         p_Vid->frm_no_in_file, pic_type, p_Vid->view_id, cur_bits,
2351         p_Vid->AverageFrameQP,
2352         metric->value[0], metric->value[1], metric->value[2],
2353         tmp_time, (int) p_Vid->me_time,
2354         p_Vid->fld_flag ? "FLD" : "FRM",
2355         p_Vid->nal_reference_idc);
2356     }
2357   }
2358   else
2359 #endif
2360   printf ("%05d(%3s)%8d   %2d %7.3f %7.3f %7.3f %9d %7d    %3s    %d\n",
2361     p_Vid->frm_no_in_file, pic_type, cur_bits,
2362     p_Vid->AverageFrameQP,
2363     metric->value[0], metric->value[1], metric->value[2],
2364     tmp_time, (int) p_Vid->me_time,
2365     p_Vid->fld_flag ? "FLD" : "FRM",
2366     p_Vid->nal_reference_idc);
2367 }
2368 
ReportVerbose(VideoParameters * p_Vid,char * pic_type,int cur_bits,int wp_method,int lambda,DistMetric * mPSNR,int tmp_time,int direct_mode)2369 static void ReportVerbose(VideoParameters *p_Vid, char *pic_type, int cur_bits, int wp_method, int lambda, DistMetric *mPSNR, int tmp_time, int direct_mode)
2370 {
2371 #if (MVC_EXTENSION_ENABLE)
2372   if ( p_Vid->p_Inp->num_of_views == 2 )
2373   {
2374     if ( p_Vid->fld_flag && p_Vid->view_id == 0 )
2375     {
2376       p_Vid->prev_cs.frm_no_in_file = p_Vid->frm_no_in_file;
2377       p_Vid->prev_cs.view_id = p_Vid->view_id;
2378       p_Vid->prev_cs.cur_bits = cur_bits;
2379       strcpy( p_Vid->prev_cs.pic_type, pic_type );
2380       p_Vid->prev_cs.AverageFrameQP = p_Vid->AverageFrameQP;
2381       p_Vid->prev_cs.lambda = 0;
2382       p_Vid->prev_cs.psnr_value[0] = mPSNR->value[0];
2383       p_Vid->prev_cs.psnr_value[1] = mPSNR->value[1];
2384       p_Vid->prev_cs.psnr_value[2] = mPSNR->value[2];
2385       p_Vid->prev_cs.ssim_value[0] = 0;
2386       p_Vid->prev_cs.ssim_value[1] = 0;
2387       p_Vid->prev_cs.ssim_value[2] = 0;
2388       p_Vid->prev_cs.tmp_time = tmp_time;
2389       p_Vid->prev_cs.me_time = (int)p_Vid->me_time;
2390       p_Vid->prev_cs.fld_flag = p_Vid->fld_flag;
2391       p_Vid->prev_cs.intras = p_Vid->intras;
2392       p_Vid->prev_cs.direct_mode = direct_mode;
2393       p_Vid->prev_cs.num_ref_idx_l0_active = p_Vid->num_ref_idx_l0_active;
2394       p_Vid->prev_cs.num_ref_idx_l1_active = p_Vid->num_ref_idx_l1_active;
2395       p_Vid->prev_cs.rd_pass = p_Vid->rd_pass;
2396       p_Vid->prev_cs.nal_reference_idc = p_Vid->nal_reference_idc;
2397     }
2398     else if ( p_Vid->fld_flag && p_Vid->view_id == 1 )
2399     {
2400       printf ("%05d(%3s)  %1d  %8d %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2401         p_Vid->prev_cs.frm_no_in_file, p_Vid->prev_cs.pic_type, p_Vid->prev_cs.view_id,
2402         (int)(p_Vid->p_Stats->bit_ctr_v[0] - p_Vid->p_Stats->bit_ctr_n_v[0]) + (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[0] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[0]),
2403         p_Vid->prev_cs.wp_method, p_Vid->prev_cs.AverageFrameQP, p_Vid->prev_cs.lambda,
2404         p_Vid->prev_cs.psnr_value[0], p_Vid->prev_cs.psnr_value[1], p_Vid->prev_cs.psnr_value[2],
2405         p_Vid->prev_cs.tmp_time, (int) p_Vid->prev_cs.me_time,
2406         p_Vid->prev_cs.fld_flag ? "FLD" : "FRM", p_Vid->prev_cs.intras, p_Vid->prev_cs.direct_mode,
2407         p_Vid->prev_cs.num_ref_idx_l0_active, p_Vid->prev_cs.num_ref_idx_l1_active, p_Vid->prev_cs.rd_pass, p_Vid->prev_cs.nal_reference_idc);
2408       printf ("%05d(%3s)  %1d  %8d %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2409         p_Vid->frm_no_in_file, pic_type, p_Vid->view_id,
2410         (int)(p_Vid->p_Stats->bit_ctr_v[1] - p_Vid->p_Stats->bit_ctr_n_v[1]) + (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[1] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[1]), wp_method,
2411         p_Vid->AverageFrameQP, lambda,
2412         mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2413         tmp_time, (int) p_Vid->me_time,
2414         p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2415         p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2416     }
2417     else
2418     {
2419       printf ("%05d(%3s)  %1d  %8d %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2420         p_Vid->frm_no_in_file, pic_type, p_Vid->view_id, cur_bits, wp_method,
2421         p_Vid->AverageFrameQP, lambda,
2422         mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2423         tmp_time, (int) p_Vid->me_time,
2424         p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2425         p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2426     }
2427   }
2428   else
2429 #endif
2430   {
2431   printf ("%05d(%3s)%8d %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2432     p_Vid->frm_no_in_file, pic_type, cur_bits, wp_method,
2433     p_Vid->AverageFrameQP, lambda,
2434     mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2435     tmp_time, (int) p_Vid->me_time,
2436     p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2437     p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2438   }
2439 }
2440 
ReportVerboseNVB(VideoParameters * p_Vid,char * pic_type,int cur_bits,int nvb_bits,int wp_method,int lambda,DistMetric * mPSNR,int tmp_time,int direct_mode)2441 static void ReportVerboseNVB(VideoParameters *p_Vid, char *pic_type, int cur_bits, int nvb_bits, int wp_method, int lambda, DistMetric *mPSNR, int tmp_time, int direct_mode)
2442 {
2443 #if (MVC_EXTENSION_ENABLE)
2444   if ( p_Vid->num_of_layers == 2 )
2445   {
2446     if ( p_Vid->fld_flag && p_Vid->view_id == 0 )
2447     {
2448       p_Vid->prev_cs.frm_no_in_file = p_Vid->frm_no_in_file;
2449       p_Vid->prev_cs.view_id = p_Vid->view_id;
2450       p_Vid->prev_cs.cur_bits = cur_bits;
2451       strcpy( p_Vid->prev_cs.pic_type, pic_type );
2452       p_Vid->prev_cs.AverageFrameQP = p_Vid->AverageFrameQP;
2453       p_Vid->prev_cs.lambda = 0;
2454       p_Vid->prev_cs.psnr_value[0] = mPSNR->value[0];
2455       p_Vid->prev_cs.psnr_value[1] = mPSNR->value[1];
2456       p_Vid->prev_cs.psnr_value[2] = mPSNR->value[2];
2457       p_Vid->prev_cs.ssim_value[0] = 0;
2458       p_Vid->prev_cs.ssim_value[1] = 0;
2459       p_Vid->prev_cs.ssim_value[2] = 0;
2460       p_Vid->prev_cs.tmp_time = tmp_time;
2461       p_Vid->prev_cs.me_time = (int)p_Vid->me_time;
2462       p_Vid->prev_cs.fld_flag = p_Vid->fld_flag;
2463       p_Vid->prev_cs.intras = p_Vid->intras;
2464       p_Vid->prev_cs.direct_mode = direct_mode;
2465       p_Vid->prev_cs.num_ref_idx_l0_active = p_Vid->num_ref_idx_l0_active;
2466       p_Vid->prev_cs.num_ref_idx_l1_active = p_Vid->num_ref_idx_l1_active;
2467       p_Vid->prev_cs.rd_pass = p_Vid->rd_pass;
2468       p_Vid->prev_cs.nal_reference_idc = p_Vid->nal_reference_idc;
2469     }
2470     else if ( p_Vid->fld_flag && p_Vid->view_id == 1 )
2471     {
2472       printf ("%05d(%3s)  %1d  %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2473         p_Vid->prev_cs.frm_no_in_file, p_Vid->prev_cs.pic_type, p_Vid->prev_cs.view_id,
2474         (int)(p_Vid->p_Stats->bit_ctr_v[0] - p_Vid->p_Stats->bit_ctr_n_v[0])
2475         + (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[0] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[0]) + p_Vid->p_Stats->bit_ctr_parametersets_n_v[0],
2476         p_Vid->p_Stats->bit_ctr_parametersets_n_v[0], p_Vid->prev_cs.wp_method, p_Vid->prev_cs.AverageFrameQP, p_Vid->prev_cs.lambda,
2477         p_Vid->prev_cs.psnr_value[0], p_Vid->prev_cs.psnr_value[1], p_Vid->prev_cs.psnr_value[2],
2478         p_Vid->prev_cs.tmp_time, (int) p_Vid->prev_cs.me_time,
2479         p_Vid->prev_cs.fld_flag ? "FLD" : "FRM", p_Vid->prev_cs.intras, p_Vid->prev_cs.direct_mode,
2480         p_Vid->prev_cs.num_ref_idx_l0_active, p_Vid->prev_cs.num_ref_idx_l1_active, p_Vid->prev_cs.rd_pass, p_Vid->prev_cs.nal_reference_idc);
2481       printf ("%05d(%3s)  %1d  %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2482         p_Vid->frm_no_in_file, pic_type, p_Vid->view_id,
2483         (int)(p_Vid->p_Stats->bit_ctr_v[1] - p_Vid->p_Stats->bit_ctr_n_v[1])
2484         + (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[1] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[1]) + p_Vid->p_Stats->bit_ctr_parametersets_n_v[1],
2485         p_Vid->p_Stats->bit_ctr_parametersets_n_v[1], wp_method,
2486         p_Vid->AverageFrameQP, lambda,
2487         mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2488         tmp_time, (int) p_Vid->me_time,
2489         p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2490         p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2491     }
2492     else
2493     {
2494       printf ("%05d(%3s)  %1d  %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d  %d\n",
2495         p_Vid->frm_no_in_file, pic_type, p_Vid->view_id, cur_bits, nvb_bits, wp_method,
2496         p_Vid->AverageFrameQP, lambda,
2497         mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2498         tmp_time, (int) p_Vid->me_time,
2499         p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2500         p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc, p_Vid->iInterViewMBs);
2501     }
2502   }
2503   else
2504 #endif
2505   {
2506     printf ("%05d(%3s)%8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2507       p_Vid->frm_no_in_file, pic_type, cur_bits, nvb_bits, wp_method,
2508       p_Vid->AverageFrameQP, lambda,
2509       mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2510       tmp_time, (int) p_Vid->me_time,
2511       p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2512       p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2513 
2514   }
2515 }
2516 
ReportVerboseFDN(VideoParameters * p_Vid,char * pic_type,int cur_bits,int fdn_bits,int nvb_bits,int wp_method,int lambda,DistMetric * mPSNR,int tmp_time,int direct_mode)2517 static void ReportVerboseFDN(VideoParameters *p_Vid, char *pic_type, int cur_bits, int fdn_bits, int nvb_bits, int wp_method, int lambda, DistMetric *mPSNR, int tmp_time, int direct_mode)
2518 {
2519 #if (MVC_EXTENSION_ENABLE)
2520   if ( p_Vid->p_Inp->num_of_views == 2 )
2521   {
2522     if ( p_Vid->fld_flag && p_Vid->view_id == 0 )
2523     {
2524       p_Vid->prev_cs.frm_no_in_file = p_Vid->frm_no_in_file;
2525       p_Vid->prev_cs.view_id = p_Vid->view_id;
2526       p_Vid->prev_cs.cur_bits = cur_bits;
2527       strcpy( p_Vid->prev_cs.pic_type, pic_type );
2528       p_Vid->prev_cs.AverageFrameQP = p_Vid->AverageFrameQP;
2529       p_Vid->prev_cs.lambda = 0;
2530       p_Vid->prev_cs.psnr_value[0] = mPSNR->value[0];
2531       p_Vid->prev_cs.psnr_value[1] = mPSNR->value[1];
2532       p_Vid->prev_cs.psnr_value[2] = mPSNR->value[2];
2533       p_Vid->prev_cs.ssim_value[0] = 0;
2534       p_Vid->prev_cs.ssim_value[1] = 0;
2535       p_Vid->prev_cs.ssim_value[2] = 0;
2536       p_Vid->prev_cs.tmp_time = tmp_time;
2537       p_Vid->prev_cs.me_time = (int)p_Vid->me_time;
2538       p_Vid->prev_cs.fld_flag = p_Vid->fld_flag;
2539       p_Vid->prev_cs.intras = p_Vid->intras;
2540       p_Vid->prev_cs.direct_mode = direct_mode;
2541       p_Vid->prev_cs.num_ref_idx_l0_active = p_Vid->num_ref_idx_l0_active;
2542       p_Vid->prev_cs.num_ref_idx_l1_active = p_Vid->num_ref_idx_l1_active;
2543       p_Vid->prev_cs.rd_pass = p_Vid->rd_pass;
2544       p_Vid->prev_cs.nal_reference_idc = p_Vid->nal_reference_idc;
2545     }
2546     else if ( p_Vid->fld_flag && p_Vid->view_id == 1 )
2547     {
2548       printf ("%05d(%3s)  %1d  %8d %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2549         p_Vid->prev_cs.frm_no_in_file, p_Vid->prev_cs.pic_type, p_Vid->prev_cs.view_id,
2550         (int)(p_Vid->p_Stats->bit_ctr_v[0] - p_Vid->p_Stats->bit_ctr_n_v[0])
2551         + (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[0] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[0]) + p_Vid->p_Stats->bit_ctr_parametersets_n_v[0],
2552         (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[0] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[0]),
2553         p_Vid->p_Stats->bit_ctr_parametersets_n_v[0],
2554         p_Vid->prev_cs.wp_method, p_Vid->prev_cs.AverageFrameQP, p_Vid->prev_cs.lambda,
2555         p_Vid->prev_cs.psnr_value[0], p_Vid->prev_cs.psnr_value[1], p_Vid->prev_cs.psnr_value[2],
2556         p_Vid->prev_cs.tmp_time, (int) p_Vid->prev_cs.me_time,
2557         p_Vid->prev_cs.fld_flag ? "FLD" : "FRM", p_Vid->prev_cs.intras, p_Vid->prev_cs.direct_mode,
2558         p_Vid->prev_cs.num_ref_idx_l0_active, p_Vid->prev_cs.num_ref_idx_l1_active, p_Vid->prev_cs.rd_pass, p_Vid->prev_cs.nal_reference_idc);
2559       printf ("%05d(%3s)  %1d  %8d %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2560         p_Vid->frm_no_in_file, pic_type, p_Vid->view_id,
2561         (int)(p_Vid->p_Stats->bit_ctr_v[1] - p_Vid->p_Stats->bit_ctr_n_v[1])
2562         + (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[1] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[1]) + p_Vid->p_Stats->bit_ctr_parametersets_n_v[1],
2563         (int)(p_Vid->p_Stats->bit_ctr_filler_data_v[1] - p_Vid->p_Stats->bit_ctr_filler_data_n_v[1]),
2564         p_Vid->p_Stats->bit_ctr_parametersets_n_v[1],
2565         wp_method,
2566         p_Vid->AverageFrameQP, lambda,
2567         mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2568         tmp_time, (int) p_Vid->me_time,
2569         p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2570         p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2571     }
2572     else
2573     {
2574       printf ("%05d(%3s)  %1d  %8d %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2575         p_Vid->frm_no_in_file, pic_type, p_Vid->view_id, cur_bits, fdn_bits, nvb_bits, wp_method,
2576         p_Vid->AverageFrameQP, lambda,
2577         mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2578         tmp_time, (int) p_Vid->me_time,
2579         p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2580         p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2581     }
2582   }
2583   else
2584 #endif
2585   {
2586   printf ("%05d(%3s)%8d %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2587     p_Vid->frm_no_in_file, pic_type, cur_bits, fdn_bits, nvb_bits, wp_method,
2588     p_Vid->AverageFrameQP, lambda,
2589     mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2590     tmp_time, (int) p_Vid->me_time,
2591     p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2592     p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2593   }
2594 }
2595 
ReportVerboseSSIM(VideoParameters * p_Vid,char * pic_type,int cur_bits,int wp_method,int lambda,DistMetric * mPSNR,DistMetric * mSSIM,int tmp_time,int direct_mode)2596 static void ReportVerboseSSIM(VideoParameters *p_Vid, char *pic_type, int cur_bits, int wp_method, int lambda, DistMetric *mPSNR, DistMetric *mSSIM,int tmp_time, int direct_mode)
2597 {
2598 #if (MVC_EXTENSION_ENABLE)
2599   if ( p_Vid->p_Inp->num_of_views == 2 )
2600   {
2601     printf ("%05d(%3s)  %1d  %8d %1d %2d %4d %7.3f %7.3f %7.3f %7.4f %7.4f %7.4f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2602       p_Vid->frm_no_in_file, pic_type, p_Vid->view_id, cur_bits, wp_method,
2603       p_Vid->AverageFrameQP, lambda,
2604       mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2605       mSSIM->value[0], mSSIM->value[1], mSSIM->value[2],
2606       tmp_time, (int) p_Vid->me_time,
2607       p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2608       p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active,p_Vid->rd_pass, p_Vid->nal_reference_idc);
2609   }
2610   else
2611 #endif
2612   {
2613   printf ("%05d(%3s)%8d %1d %2d %4d %7.3f %7.3f %7.3f %7.4f %7.4f %7.4f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2614     p_Vid->frm_no_in_file, pic_type, cur_bits, wp_method,
2615     p_Vid->AverageFrameQP, lambda,
2616     mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2617     mSSIM->value[0], mSSIM->value[1], mSSIM->value[2],
2618     tmp_time, (int) p_Vid->me_time,
2619     p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2620     p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active,p_Vid->rd_pass, p_Vid->nal_reference_idc);
2621   }
2622 }
2623 
ReportVerboseNVBSSIM(VideoParameters * p_Vid,char * pic_type,int cur_bits,int nvb_bits,int wp_method,int lambda,DistMetric * mPSNR,DistMetric * mSSIM,int tmp_time,int direct_mode)2624 static void ReportVerboseNVBSSIM(VideoParameters *p_Vid, char *pic_type, int cur_bits, int nvb_bits, int wp_method, int lambda, DistMetric *mPSNR, DistMetric *mSSIM,int tmp_time, int direct_mode)
2625 {
2626 #if (MVC_EXTENSION_ENABLE)
2627   if ( p_Vid->p_Inp->num_of_views == 2 )
2628   {
2629     printf ("%05d(%3s)  %1d  %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %7.4f %7.4f %7.4f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2630       p_Vid->frm_no_in_file, pic_type, p_Vid->view_id, cur_bits, nvb_bits, wp_method,
2631       p_Vid->AverageFrameQP, lambda,
2632       mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2633       mSSIM->value[0], mSSIM->value[1], mSSIM->value[2],
2634       tmp_time, (int) p_Vid->me_time,
2635       p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2636       p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2637   }
2638   else
2639 #endif
2640   {
2641   printf ("%05d(%3s)%8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %7.4f %7.4f %7.4f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2642     p_Vid->frm_no_in_file, pic_type, cur_bits, nvb_bits, wp_method,
2643     p_Vid->AverageFrameQP, lambda,
2644     mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2645     mSSIM->value[0], mSSIM->value[1], mSSIM->value[2],
2646     tmp_time, (int) p_Vid->me_time,
2647     p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2648     p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2649   }
2650 }
2651 
ReportVerboseFDNSSIM(VideoParameters * p_Vid,char * pic_type,int cur_bits,int fdn_bits,int nvb_bits,int wp_method,int lambda,DistMetric * mPSNR,DistMetric * mSSIM,int tmp_time,int direct_mode)2652 static void ReportVerboseFDNSSIM(VideoParameters *p_Vid, char *pic_type, int cur_bits, int fdn_bits, int nvb_bits, int wp_method, int lambda, DistMetric *mPSNR, DistMetric *mSSIM,int tmp_time, int direct_mode)
2653 {
2654 #if (MVC_EXTENSION_ENABLE)
2655   if ( p_Vid->p_Inp->num_of_views == 2 )
2656   {
2657     printf ("%05d(%3s)  %1d  %8d %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %7.4f %7.4f %7.4f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2658       p_Vid->frm_no_in_file, pic_type, p_Vid->view_id, cur_bits, fdn_bits, nvb_bits, wp_method,
2659       p_Vid->AverageFrameQP, lambda,
2660       mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2661       mSSIM->value[0], mSSIM->value[1], mSSIM->value[2],
2662       tmp_time, (int) p_Vid->me_time,
2663       p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2664       p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2665   }
2666   else
2667 #endif
2668   {
2669   printf ("%05d(%3s)%8d %8d %3d  %1d %2d %4d %7.3f %7.3f %7.3f %7.4f %7.4f %7.4f %9d %7d    %3s %5d %1d %2d %2d  %d   %d\n",
2670     p_Vid->frm_no_in_file, pic_type, cur_bits, fdn_bits, nvb_bits, wp_method,
2671     p_Vid->AverageFrameQP, lambda,
2672     mPSNR->value[0], mPSNR->value[1], mPSNR->value[2],
2673     mSSIM->value[0], mSSIM->value[1], mSSIM->value[2],
2674     tmp_time, (int) p_Vid->me_time,
2675     p_Vid->fld_flag ? "FLD" : "FRM", p_Vid->intras, direct_mode,
2676     p_Vid->num_ref_idx_l0_active, p_Vid->num_ref_idx_l1_active, p_Vid->rd_pass, p_Vid->nal_reference_idc);
2677   }
2678 }
2679 
2680 
ReportNALNonVLCBits(VideoParameters * p_Vid,int64 tmp_time)2681 static void ReportNALNonVLCBits(VideoParameters *p_Vid, int64 tmp_time)
2682 {
2683   InputParameters *p_Inp = p_Vid->p_Inp;
2684   StatParameters *p_Stats = p_Vid->p_Stats;
2685 
2686   //! Need to add type (i.e. SPS, PPS, SEI etc).
2687 #if (MVC_EXTENSION_ENABLE)
2688   if (p_Inp->num_of_views == 2)
2689   {
2690     if (p_Inp->Verbose != 0)
2691       printf ("%05d(NVB)     %8d \n", p_Vid->frame_no, p_Stats->bit_ctr_parametersets_n);
2692   }
2693   else
2694 #endif
2695   {
2696     if (p_Inp->Verbose != 0)
2697       printf ("%05d(NVB)%8d \n", p_Vid->frame_no, p_Stats->bit_ctr_parametersets_n);
2698   }
2699 }
2700 
ReportFirstframe(VideoParameters * p_Vid,int64 tmp_time)2701 static void ReportFirstframe(VideoParameters *p_Vid, int64 tmp_time)
2702 {
2703   InputParameters *p_Inp = p_Vid->p_Inp;
2704   StatParameters *stats = p_Vid->p_Stats;
2705 
2706   int cur_bits = (int)(stats->bit_ctr - stats->bit_ctr_n)
2707     + (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n);
2708 
2709   if (p_Inp->Verbose == 1)
2710   {
2711     ReportSimple(p_Vid, "IDR", cur_bits, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time);
2712   }
2713   else if (p_Inp->Verbose == 2)
2714   {
2715     int lambda = (int) p_Vid->lambda_me[I_SLICE][p_Vid->masterQP][F_PEL];
2716     if (p_Inp->Distortion[SSIM] == 1)
2717       ReportVerboseSSIM(p_Vid, "IDR", cur_bits, 0, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, 0);
2718     else
2719       ReportVerbose(p_Vid, "IDR", cur_bits, 0, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, 0);
2720   }
2721   else if (p_Inp->Verbose == 3)
2722   {
2723     int lambda = (int) p_Vid->lambda_me[I_SLICE][p_Vid->masterQP][F_PEL];
2724     if (p_Inp->Distortion[SSIM] == 1)
2725       ReportVerboseNVBSSIM(p_Vid, "IDR", cur_bits + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, 0, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, 0);
2726     else
2727       ReportVerboseNVB(p_Vid, "IDR", cur_bits + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, 0, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, 0);
2728   }
2729   else if (p_Inp->Verbose == 4)
2730   {
2731     int lambda = (int) p_Vid->lambda_me[I_SLICE][p_Vid->masterQP][F_PEL];
2732     if (p_Inp->Distortion[SSIM] == 1)
2733       ReportVerboseFDNSSIM(p_Vid, "IDR", cur_bits + stats->bit_ctr_parametersets_n, (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n), stats->bit_ctr_parametersets_n, 0, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, 0);
2734     else
2735       ReportVerboseFDN(p_Vid, "IDR", cur_bits + stats->bit_ctr_parametersets_n, (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n), stats->bit_ctr_parametersets_n, 0, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, 0);
2736   }
2737 
2738   stats->bit_counter[I_SLICE] = stats->bit_ctr;
2739 #if (MVC_EXTENSION_ENABLE)
2740   if ( p_Inp->num_of_views == 2 )
2741   {
2742     if ( !p_Vid->fld_flag )
2743     {
2744       stats->bit_counter_v[p_Vid->view_id][I_SLICE] = stats->bit_ctr;
2745     }
2746     stats->bit_ctr_v[0] = 0;
2747     stats->bit_ctr_v[1] = 0;
2748   }
2749 #endif
2750   stats->bit_ctr = 0;
2751 }
2752 
ReportI(VideoParameters * p_Vid,int64 tmp_time)2753 static void ReportI(VideoParameters *p_Vid, int64 tmp_time)
2754 {
2755   InputParameters *p_Inp = p_Vid->p_Inp;
2756   StatParameters *stats = p_Vid->p_Stats;
2757   char pic_type[4];
2758   int  cur_bits = (int)(stats->bit_ctr - stats->bit_ctr_n)
2759     + (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n);
2760 
2761   if ((p_Inp->redundant_pic_flag == 0) || !p_Vid->redundant_coding )
2762   {
2763     if (p_Vid->currentPicture->idr_flag == TRUE)
2764       strcpy(pic_type,"IDR");
2765     else if ( p_Vid->type == SI_SLICE )
2766       strcpy(pic_type,"SI ");
2767     else
2768       strcpy(pic_type," I ");
2769   }
2770   else
2771     strcpy(pic_type,"R");
2772 
2773   if (p_Inp->Verbose == 1)
2774   {
2775     ReportSimple(p_Vid, pic_type, cur_bits, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time);
2776   }
2777   else if (p_Inp->Verbose == 2)
2778   {
2779     int lambda = (int) p_Vid->lambda_me[I_SLICE][p_Vid->masterQP][F_PEL];
2780     if (p_Inp->Distortion[SSIM] == 1)
2781     {
2782       ReportVerboseSSIM(p_Vid, pic_type, cur_bits, 0, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, 0);
2783     }
2784     else
2785     {
2786       ReportVerbose(p_Vid, pic_type, cur_bits, 0, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, 0);
2787     }
2788   }
2789   else if (p_Inp->Verbose == 3)
2790   {
2791     int lambda = (int) p_Vid->lambda_me[I_SLICE][p_Vid->masterQP][F_PEL];
2792     if (p_Inp->Distortion[SSIM] == 1)
2793     {
2794       ReportVerboseNVBSSIM(p_Vid, pic_type, cur_bits + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, 0, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, 0);
2795     }
2796     else
2797     {
2798       ReportVerboseNVB(p_Vid, pic_type, cur_bits + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, 0, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, 0);
2799     }
2800   }
2801   else if (p_Inp->Verbose == 4)
2802   {
2803     int lambda = (int) p_Vid->lambda_me[I_SLICE][p_Vid->masterQP][F_PEL];
2804     if (p_Inp->Distortion[SSIM] == 1)
2805     {
2806       ReportVerboseFDNSSIM(p_Vid, pic_type, cur_bits + stats->bit_ctr_parametersets_n, (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n), stats->bit_ctr_parametersets_n, 0, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, 0);
2807     }
2808     else
2809     {
2810       ReportVerboseFDN(p_Vid, pic_type, cur_bits + stats->bit_ctr_parametersets_n, (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n), stats->bit_ctr_parametersets_n, 0, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, 0);
2811     }
2812   }
2813 }
2814 
ReportB(VideoParameters * p_Vid,int64 tmp_time)2815 static void ReportB(VideoParameters *p_Vid, int64 tmp_time)
2816 {
2817   InputParameters *p_Inp = p_Vid->p_Inp;
2818   StatParameters *stats = p_Vid->p_Stats;
2819 
2820   int cur_bits = (int)(stats->bit_ctr - stats->bit_ctr_n)
2821     + (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n);
2822 
2823   if (p_Inp->Verbose == 1)
2824   {
2825     ReportSimple(p_Vid, " B ", cur_bits, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time);
2826   }
2827   else if (p_Inp->Verbose == 2)
2828   {
2829     int lambda = (int) p_Vid->lambda_me[B_SLICE][p_Vid->masterQP][F_PEL];
2830     if (p_Inp->Distortion[SSIM] == 1)
2831       ReportVerboseSSIM(p_Vid, " B ", cur_bits, p_Vid->active_pps->weighted_bipred_idc, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, p_Vid->direct_spatial_mv_pred_flag);
2832     else
2833       ReportVerbose(p_Vid, " B ", cur_bits, p_Vid->active_pps->weighted_bipred_idc, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, p_Vid->direct_spatial_mv_pred_flag);
2834   }
2835   else if (p_Inp->Verbose == 3)
2836   {
2837     int lambda = (int) p_Vid->lambda_me[B_SLICE][p_Vid->masterQP][F_PEL];
2838     if (p_Inp->Distortion[SSIM] == 1)
2839       ReportVerboseNVBSSIM(p_Vid, " B ", cur_bits + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, p_Vid->active_pps->weighted_bipred_idc, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, p_Vid->direct_spatial_mv_pred_flag);
2840     else
2841       ReportVerboseNVB(p_Vid, " B ", cur_bits + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, p_Vid->active_pps->weighted_bipred_idc, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, p_Vid->direct_spatial_mv_pred_flag);
2842   }
2843   else if (p_Inp->Verbose == 4)
2844   {
2845     int lambda = (int) p_Vid->lambda_me[B_SLICE][p_Vid->masterQP][F_PEL];
2846     if (p_Inp->Distortion[SSIM] == 1)
2847       ReportVerboseFDNSSIM(p_Vid, " B ", cur_bits + stats->bit_ctr_parametersets_n, (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n), stats->bit_ctr_parametersets_n, p_Vid->active_pps->weighted_bipred_idc, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, p_Vid->direct_spatial_mv_pred_flag);
2848     else
2849       ReportVerboseFDN(p_Vid, " B ", cur_bits + stats->bit_ctr_parametersets_n, (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n), stats->bit_ctr_parametersets_n, p_Vid->active_pps->weighted_bipred_idc, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, p_Vid->direct_spatial_mv_pred_flag);
2850   }
2851 }
2852 
ReportP(VideoParameters * p_Vid,int64 tmp_time)2853 static void ReportP(VideoParameters *p_Vid, int64 tmp_time)
2854 {
2855   InputParameters *p_Inp = p_Vid->p_Inp;
2856   StatParameters *stats = p_Vid->p_Stats;
2857 
2858   char pic_type[4];
2859   int  cur_bits = (int)(stats->bit_ctr - stats->bit_ctr_n)
2860     + (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n);
2861 
2862   if (p_Vid->type == SP_SLICE)
2863     strcpy(pic_type,"SP ");
2864   else if ((p_Inp->redundant_pic_flag == 0) || !p_Vid->redundant_coding )
2865     strcpy(pic_type," P ");
2866   else
2867     strcpy(pic_type," R ");
2868 
2869   if (p_Inp->Verbose == 1)
2870   {
2871     ReportSimple(p_Vid, pic_type, cur_bits, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time);
2872   }
2873   else if (p_Inp->Verbose == 2)
2874   {
2875     int lambda = (int) p_Vid->lambda_me[P_SLICE][p_Vid->masterQP][F_PEL];
2876     if (p_Inp->Distortion[SSIM] == 1)
2877       ReportVerboseSSIM(p_Vid, pic_type, cur_bits, p_Vid->active_pps->weighted_pred_flag, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, 0);
2878     else
2879       ReportVerbose(p_Vid, pic_type, cur_bits, p_Vid->active_pps->weighted_pred_flag, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, 0);
2880   }
2881   else if (p_Inp->Verbose == 3)
2882   {
2883     int lambda = (int) p_Vid->lambda_me[P_SLICE][p_Vid->masterQP][F_PEL];
2884     if (p_Inp->Distortion[SSIM] == 1)
2885       ReportVerboseNVBSSIM(p_Vid, pic_type, cur_bits + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, p_Vid->active_pps->weighted_pred_flag, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, 0);
2886     else
2887       ReportVerboseNVB(p_Vid, pic_type, cur_bits + stats->bit_ctr_parametersets_n, stats->bit_ctr_parametersets_n, p_Vid->active_pps->weighted_pred_flag, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, 0);
2888   }
2889   else if (p_Inp->Verbose == 4)
2890   {
2891     int lambda = (int) p_Vid->lambda_me[P_SLICE][p_Vid->masterQP][F_PEL];
2892     if (p_Inp->Distortion[SSIM] == 1)
2893       ReportVerboseFDNSSIM(p_Vid, pic_type, cur_bits + stats->bit_ctr_parametersets_n, (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n), stats->bit_ctr_parametersets_n, p_Vid->active_pps->weighted_pred_flag, lambda, &p_Vid->p_Dist->metric[PSNR], &p_Vid->p_Dist->metric[SSIM], (int) tmp_time, 0);
2894     else
2895       ReportVerboseFDN(p_Vid, pic_type, cur_bits + stats->bit_ctr_parametersets_n, (int)(stats->bit_ctr_filler_data - stats->bit_ctr_filler_data_n), stats->bit_ctr_parametersets_n, p_Vid->active_pps->weighted_pred_flag, lambda, &p_Vid->p_Dist->metric[PSNR], (int) tmp_time, 0);
2896   }
2897 }
2898 
2899 /*!
2900  ************************************************************************
2901  * \brief
2902  *    point to frame coding variables
2903  ************************************************************************
2904  */
put_buffer_frame(VideoParameters * p_Vid)2905 static void put_buffer_frame(VideoParameters *p_Vid)
2906 {
2907   p_Vid->pCurImg    = p_Vid->imgData.frm_data[0];
2908   p_Vid->pImgOrg[0] = p_Vid->imgData.frm_data[0];
2909 
2910   if (p_Vid->yuv_format != YUV400)
2911   {
2912     p_Vid->pImgOrg[1] = p_Vid->imgData.frm_data[1];
2913     p_Vid->pImgOrg[2] = p_Vid->imgData.frm_data[2];
2914   }
2915 
2916   if (p_Vid->p_Inp->MDReference[0] || p_Vid->p_Inp->MDReference[1])
2917   {
2918     p_Vid->pCurImgRef    = p_Vid->imgRefData.frm_data[0];
2919     p_Vid->pImgOrgRef[0] = p_Vid->imgRefData.frm_data[0];
2920 
2921     if (p_Vid->yuv_format != YUV400)
2922     {
2923       p_Vid->pImgOrgRef[1] = p_Vid->imgRefData.frm_data[1];
2924       p_Vid->pImgOrgRef[2] = p_Vid->imgRefData.frm_data[2];
2925     }
2926   }
2927 }
2928 
2929 /*!
2930  ************************************************************************
2931  * \brief
2932  *    point to top field coding variables
2933  ************************************************************************
2934  */
put_buffer_top(VideoParameters * p_Vid)2935 static void put_buffer_top(VideoParameters *p_Vid)
2936 {
2937   p_Vid->fld_type = 0;
2938 
2939   p_Vid->pCurImg    = p_Vid->imgData.top_data[0];
2940   p_Vid->pImgOrg[0] = p_Vid->imgData.top_data[0];
2941 
2942   if (p_Vid->yuv_format != YUV400)
2943   {
2944     p_Vid->pImgOrg[1] = p_Vid->imgData.top_data[1];
2945     p_Vid->pImgOrg[2] = p_Vid->imgData.top_data[2];
2946   }
2947 
2948   if ( p_Vid->p_Inp->MDReference[0] || p_Vid->p_Inp->MDReference[1] )
2949   {
2950     p_Vid->pCurImgRef    = p_Vid->imgRefData.top_data[0];
2951     p_Vid->pImgOrgRef[0] = p_Vid->imgRefData.top_data[0];
2952 
2953     if (p_Vid->yuv_format != YUV400)
2954     {
2955       p_Vid->pImgOrgRef[1] = p_Vid->imgRefData.top_data[1];
2956       p_Vid->pImgOrgRef[2] = p_Vid->imgRefData.top_data[2];
2957     }
2958   }
2959 }
2960 
2961 /*!
2962  ************************************************************************
2963  * \brief
2964  *    point to bottom field coding variables
2965  ************************************************************************
2966  */
put_buffer_bot(VideoParameters * p_Vid)2967 static void put_buffer_bot(VideoParameters *p_Vid)
2968 {
2969   p_Vid->fld_type = 1;
2970 
2971   p_Vid->pCurImg    = p_Vid->imgData.bot_data[0];
2972   p_Vid->pImgOrg[0] = p_Vid->imgData.bot_data[0];
2973 
2974   if (p_Vid->yuv_format != YUV400)
2975   {
2976     p_Vid->pImgOrg[1] = p_Vid->imgData.bot_data[1];
2977     p_Vid->pImgOrg[2] = p_Vid->imgData.bot_data[2];
2978   }
2979 
2980   if ( p_Vid->p_Inp->MDReference[0] || p_Vid->p_Inp->MDReference[1] )
2981   {
2982     p_Vid->pCurImgRef    = p_Vid->imgRefData.bot_data[0];
2983     p_Vid->pImgOrgRef[0] = p_Vid->imgRefData.bot_data[0];
2984 
2985     if (p_Vid->yuv_format != YUV400)
2986     {
2987       p_Vid->pImgOrgRef[1] = p_Vid->imgRefData.bot_data[1];
2988       p_Vid->pImgOrgRef[2] = p_Vid->imgRefData.bot_data[2];
2989     }
2990   }
2991 }
2992 
2993 /*!
2994 *************************************************************************************
2995 * Brief
2996 *     Output SP frames coefficients
2997 *************************************************************************************
2998 */
output_SP_coefficients(VideoParameters * p_Vid,InputParameters * p_Inp)2999 void output_SP_coefficients(VideoParameters *p_Vid, InputParameters *p_Inp)
3000 {
3001   int i,k;
3002   FILE *SP_coeff_file;
3003   int ret;
3004   if(p_Vid->number_sp2_frames==0)
3005   {
3006     if ((SP_coeff_file = fopen(p_Inp->sp_output_filename,"wb")) == NULL)
3007     {
3008       printf ("Fatal: cannot open SP output file '%s', exit (-1)\n", p_Inp->sp_output_filename);
3009       exit (-1);
3010     }
3011     p_Vid->number_sp2_frames++;
3012   }
3013   else
3014   {
3015     if ((SP_coeff_file = fopen(p_Inp->sp_output_filename,"ab")) == NULL)
3016     {
3017       printf ("Fatal: cannot open SP output file '%s', exit (-1)\n", p_Inp->sp_output_filename);
3018       exit (-1);
3019     }
3020   }
3021 
3022   for(i=0;i<p_Vid->height;i++)
3023   {
3024     ret = (int) fwrite(p_Vid->lrec[i], sizeof(int), p_Vid->width, SP_coeff_file);
3025     if (ret != p_Vid->width)
3026     {
3027       error ("cannot write to SP output file", -1);
3028     }
3029   }
3030 
3031   for(k=0;k<2;k++)
3032   {
3033     for(i=0;i<p_Vid->height_cr;i++)
3034     {
3035       ret = (int) fwrite(p_Vid->lrec_uv[k][i], sizeof(int), p_Vid->width_cr, SP_coeff_file);
3036       if (ret != p_Vid->width_cr)
3037       {
3038         error ("cannot write to SP output file", -1);
3039       }
3040     }
3041   }
3042   fclose(SP_coeff_file);
3043 }
3044 
3045 /*!
3046 *************************************************************************************
3047 * Brief
3048 *     Read SP frames coefficients
3049 *************************************************************************************
3050 */
read_SP_coefficients(VideoParameters * p_Vid,InputParameters * p_Inp)3051 void read_SP_coefficients(VideoParameters *p_Vid, InputParameters *p_Inp)
3052 {
3053   int i,k;
3054   FILE *SP_coeff_file;
3055 
3056   if ( p_Inp->sp_switch_period > 0 && ( p_Vid->number_sp2_frames % (p_Inp->sp_switch_period << 1) ) >= p_Inp->sp_switch_period )
3057   {
3058     if ((SP_coeff_file = fopen(p_Inp->sp2_input_filename1,"rb")) == NULL)
3059     {
3060       printf ("Fatal: cannot open SP input file '%s', exit (-1)\n", p_Inp->sp2_input_filename2);
3061       exit (-1);
3062     }
3063   }
3064   else
3065   {
3066     if ((SP_coeff_file = fopen(p_Inp->sp2_input_filename2,"rb")) == NULL)
3067     {
3068       printf ("Fatal: cannot open SP input file '%s', exit (-1)\n", p_Inp->sp2_input_filename1);
3069       exit (-1);
3070     }
3071   }
3072 
3073   if (0 != fseek (SP_coeff_file, p_Vid->size * 3/2*p_Vid->number_sp2_frames*sizeof(int), SEEK_SET))
3074   {
3075     printf ("Fatal: cannot seek in SP input file, exit (-1)\n");
3076     exit (-1);
3077   }
3078   p_Vid->number_sp2_frames++;
3079 
3080   for(i=0;i<p_Vid->height;i++)
3081   {
3082     if(p_Vid->width!=(int)fread(p_Vid->lrec[i],sizeof(int),p_Vid->width,SP_coeff_file))
3083     {
3084       printf ("Fatal: cannot read in SP input file, exit (-1)\n");
3085       exit (-1);
3086     }
3087   }
3088 
3089   for(k=0;k<2;k++)
3090   {
3091     for(i=0;i<p_Vid->height_cr;i++)
3092     {
3093       if(p_Vid->width_cr!=(int)fread(p_Vid->lrec_uv[k][i],sizeof(int),p_Vid->width_cr,SP_coeff_file))
3094       {
3095         printf ("Fatal: cannot read in SP input file, exit (-1)\n");
3096         exit (-1);
3097       }
3098     }
3099   }
3100   fclose(SP_coeff_file);
3101 }
3102 
3103 
3104 /*!
3105 *************************************************************************************
3106 * Brief
3107 *     Select appropriate image plane (for 444 coding)
3108 *************************************************************************************
3109 */
select_plane(VideoParameters * p_Vid,ColorPlane color_plane)3110 void select_plane(VideoParameters *p_Vid, ColorPlane color_plane)
3111 {
3112   p_Vid->pCurImg              = p_Vid->pImgOrg[color_plane];
3113   p_Vid->enc_picture->p_curr_img     = p_Vid->enc_picture->p_img[color_plane];
3114   p_Vid->enc_picture->p_curr_img_sub = p_Vid->enc_picture->p_img_sub[color_plane];
3115   p_Vid->max_imgpel_value     = (short) p_Vid->max_pel_value_comp[color_plane];
3116   p_Vid->dc_pred_value        = p_Vid->dc_pred_value_comp[color_plane];
3117 }
3118 
3119 /*!
3120 *************************************************************************************
3121 * Brief
3122 *     Is this picture the first access unit in this GOP?
3123 *************************************************************************************
3124 */
is_gop_first_unit(VideoParameters * p_Vid,InputParameters * p_Inp)3125 static int is_gop_first_unit(VideoParameters *p_Vid, InputParameters *p_Inp)
3126 {
3127   return ( get_idr_flag(p_Vid) || ( p_Vid->type == I_SLICE && p_Inp->EnableOpenGOP && get_random_access_flag(p_Vid) ) );
3128 }
3129 
3130 /*!
3131 *************************************************************************************
3132 * Brief
3133 *     AUD, SPS, PPS, and SEI messages
3134 *************************************************************************************
3135 */
write_non_vcl_nalu(VideoParameters * p_Vid)3136 void write_non_vcl_nalu( VideoParameters *p_Vid )
3137 {
3138   InputParameters *p_Inp = p_Vid->p_Inp;
3139 
3140   // SPS + PPS + AUD
3141   if (p_Inp->ResendSPS == 3 && is_gop_first_unit(p_Vid, p_Inp) && p_Vid->number)
3142   {
3143     p_Vid->p_Stats->bit_slice = rewrite_paramsets(p_Vid);
3144   }
3145   else if (p_Inp->ResendSPS == 2 && get_idr_flag(p_Vid) && p_Vid->number)
3146   {
3147     p_Vid->p_Stats->bit_slice = rewrite_paramsets(p_Vid);
3148   }
3149   else if (p_Inp->ResendSPS == 1 && p_Vid->type == I_SLICE && p_Vid->curr_frm_idx != 0)
3150   {
3151     p_Vid->p_Stats->bit_slice = rewrite_paramsets(p_Vid);
3152   }
3153   // PPS + AUD
3154   else if ( p_Inp->ResendPPS && p_Vid->curr_frm_idx != 0 )
3155   {
3156     int i, total_pps = p_Inp->GenerateMultiplePPS ? 3 : 1;
3157 
3158     p_Vid->p_Stats->bit_ctr_parametersets_n = 0;
3159     // Access Unit Delimiter NALU
3160     if ( p_Inp->SendAUD )
3161     {
3162       p_Vid->p_Stats->bit_ctr_parametersets_n = Write_AUD_NALU(p_Vid);
3163     }
3164     for ( i = 0; i < total_pps; i++ )
3165     {
3166       p_Vid->p_Stats->bit_ctr_parametersets_n += write_PPS(p_Vid, 0, i);
3167     }
3168   }
3169   // Access Unit Delimiter NALU
3170   else if ( p_Inp->SendAUD && p_Vid->curr_frm_idx != 0 )
3171   {
3172     p_Vid->p_Stats->bit_ctr_parametersets_n += Write_AUD_NALU(p_Vid);
3173   }
3174 
3175   UpdateSubseqInfo (p_Vid, p_Inp, p_Vid->layer);        // Tian Dong (Sept 2002)
3176   UpdateSceneInformation (p_Vid->p_SEI, FALSE, 0, 0, -1); // JVT-D099, scene information SEI, nothing included by default
3177   UpdateFramePackingArrangement (p_Vid, p_Inp);
3178 
3179   //! Commented out by StW, needs fixing in SEI.h to keep the trace file clean
3180   //  PrepareAggregationSEIMessage (p_Vid);
3181 
3182   // write tone mapping SEI message
3183   if (p_Inp->ToneMappingSEIPresentFlag)
3184   {
3185     UpdateToneMapping(p_Vid->p_SEI);
3186   }
3187 
3188   // pic timing
3189   ClearPicTiming(p_Vid->p_SEI);
3190   if ( p_Inp->SEIVUI32Pulldown )
3191   {
3192     UpdatePicTiming(p_Vid, p_Inp);
3193     PrepareAggregationSEIMessage(p_Vid);
3194     p_Vid->p_Stats->bit_ctr_parametersets_n += Write_SEI_NALU(p_Vid, 0);
3195     ClearPicTiming(p_Vid->p_SEI);
3196     // clear message
3197     clear_sei_message(p_Vid->p_SEI, AGGREGATION_SEI);
3198   }
3199   else
3200   {
3201     PrepareAggregationSEIMessage(p_Vid);
3202   }
3203 
3204   p_Vid->p_Stats->bit_ctr_parametersets_n += Write_SEI_NALU(p_Vid, 0);
3205 
3206   // update seq NVB counter
3207   p_Vid->p_Stats->bit_ctr_parametersets   += p_Vid->p_Stats->bit_ctr_parametersets_n;
3208 }
3209 
3210 /*!
3211 *************************************************************************************
3212 * Brief
3213 *     AUD, SPS, PPS, and SEI messages for the bottom field picture
3214 *************************************************************************************
3215 */
write_non_vcl_nalu_bot_fld(VideoParameters * p_Vid)3216 void write_non_vcl_nalu_bot_fld( VideoParameters *p_Vid )
3217 {
3218   InputParameters *p_Inp = p_Vid->p_Inp;
3219   int init_param_set_bits = p_Vid->p_Stats->bit_ctr_parametersets_n;
3220 
3221   // SPS + PPS + AUD
3222   if (p_Inp->ResendSPS == 1 && p_Vid->type == I_SLICE)
3223   {
3224     p_Vid->p_Stats->bit_slice = rewrite_paramsets(p_Vid);
3225   }
3226   // PPS + AUD
3227   else if ( p_Inp->ResendPPS )
3228   {
3229     int i, total_pps = p_Inp->GenerateMultiplePPS ? 3 : 1;
3230 
3231     p_Vid->p_Stats->bit_ctr_parametersets_n = 0;
3232     // Access Unit Delimiter NALU
3233     if ( p_Inp->SendAUD )
3234     {
3235       p_Vid->p_Stats->bit_ctr_parametersets_n = Write_AUD_NALU(p_Vid);
3236     }
3237     for ( i = 0; i < total_pps; i++ )
3238     {
3239       p_Vid->p_Stats->bit_ctr_parametersets_n += write_PPS(p_Vid, 0, i);
3240     }
3241   }
3242   // Access Unit Delimiter NALU
3243   else if ( p_Inp->SendAUD )
3244   {
3245     p_Vid->p_Stats->bit_ctr_parametersets_n += Write_AUD_NALU(p_Vid);
3246   }
3247 
3248   UpdateSubseqInfo (p_Vid, p_Inp, p_Vid->layer);        // Tian Dong (Sept 2002)
3249   UpdateSceneInformation (p_Vid->p_SEI, FALSE, 0, 0, -1); // JVT-D099, scene information SEI, nothing included by default
3250   UpdateFramePackingArrangement (p_Vid, p_Inp);
3251 
3252   //! Commented out by StW, needs fixing in SEI.h to keep the trace file clean
3253   //  PrepareAggregationSEIMessage (p_Vid);
3254 
3255   // write tone mapping SEI message
3256   if (p_Inp->ToneMappingSEIPresentFlag)
3257   {
3258     UpdateToneMapping(p_Vid->p_SEI);
3259   }
3260 
3261   PrepareAggregationSEIMessage(p_Vid);
3262 
3263   p_Vid->p_Stats->bit_ctr_parametersets_n += Write_SEI_NALU(p_Vid, 0);
3264 
3265   // update seq NVB counter
3266   p_Vid->p_Stats->bit_ctr_parametersets   += (p_Vid->p_Stats->bit_ctr_parametersets_n - init_param_set_bits);
3267 }
3268 
OtfCompatibility_copyWithPadding(imgpel ** dstImg,imgpel ** srcImg,int size_x,int size_y,int padding_x,int padding_y)3269 void OtfCompatibility_copyWithPadding ( imgpel **dstImg, imgpel **srcImg, int size_x, int size_y, int padding_x, int padding_y )
3270 {
3271   int i, j;
3272   int size_x_minus1 = size_x - 1;
3273 
3274   imgpel *wBufSrc, *wBufDst;
3275 
3276   // Copy top line
3277   wBufDst = &( dstImg[-padding_y][-padding_x] );
3278   wBufSrc = srcImg[0];
3279   // left IMG_PAD_SIZE
3280   for (i = 0; i < padding_x; ++i)
3281     *(wBufDst++) = wBufSrc[0];
3282   // center 0-(size_x)
3283   memcpy(wBufDst, wBufSrc, size_x * sizeof(imgpel));
3284   wBufDst += size_x;
3285   // right IMG_PAD_SIZE
3286   for (i = 0; i < padding_x; ++i)
3287     *(wBufDst++) = wBufSrc[size_x_minus1];
3288 
3289   // Now copy remaining pad lines
3290   for (j = -padding_y+1; j < 0; ++j)
3291   {
3292     memcpy(dstImg[j]-padding_x, dstImg[j - 1]-padding_x, (size_x+2*padding_x) * sizeof(imgpel));
3293   }
3294 
3295   for (j = 0; j < size_y; ++j)
3296   {
3297     wBufDst = &( dstImg[j][-padding_x] ); // 4:4:4 independent mode
3298     wBufSrc = srcImg[j];
3299     // left IMG_PAD_SIZE
3300     for (i = 0; i < padding_x; ++i)
3301       *(wBufDst++) = wBufSrc[0];
3302     // center 0-(size_x)
3303     //memcpy(wBufDst, wBufSrc, size_x * sizeof(imgpel));
3304     wBufDst += size_x;
3305     // right IMG_PAD_SIZE
3306     for (i = 0; i < padding_x; ++i)
3307       *(wBufDst++) = wBufSrc[size_x_minus1];
3308   }
3309 
3310   // Replicate bottom pad lines
3311   for (j = size_y; j < size_y+padding_y; ++j)
3312   {
3313     memcpy(dstImg[j]-padding_x, dstImg[j - 1]-padding_x, (size_x+2*padding_x) * sizeof(imgpel));
3314   }
3315 
3316 }
3317 
3318 #if (MVC_EXTENSION_ENABLE)
3319 /*!
3320 *************************************************************************************
3321 * Brief
3322 *     MVC AUD, SPS, PPS, and SEI messages
3323 *************************************************************************************
3324 */
write_non_vcl_nalu_mvc(VideoParameters * p_Vid)3325 void write_non_vcl_nalu_mvc( VideoParameters *p_Vid )
3326 {
3327   InputParameters *p_Inp = p_Vid->p_Inp;
3328   int bits;
3329 
3330   if ( p_Vid->view_id )
3331   {
3332     if ( p_Inp->SendAUD == 2 )
3333     {
3334       p_Vid->p_Stats->bit_ctr_parametersets_n += bits = Write_AUD_NALU(p_Vid);
3335       p_Vid->p_Stats->bit_ctr_parametersets_n_v[p_Vid->view_id] += bits;
3336     }
3337   }
3338   else // view_id == 0
3339   {
3340     // SPS + PPS + AUD
3341     if (p_Inp->ResendSPS == 3 && is_gop_first_unit(p_Vid, p_Inp) && p_Vid->number && p_Vid->structure != BOTTOM_FIELD)
3342     {
3343       p_Vid->p_Stats->bit_slice = rewrite_paramsets(p_Vid);
3344     }
3345     else if (p_Inp->ResendSPS == 2 && get_idr_flag(p_Vid) && p_Vid->number && p_Vid->structure != BOTTOM_FIELD)
3346     {
3347       p_Vid->p_Stats->bit_slice = rewrite_paramsets(p_Vid);
3348     }
3349     else if (p_Inp->ResendSPS == 1 && p_Vid->type == I_SLICE && p_Vid->curr_frm_idx != 0 && p_Vid->structure != BOTTOM_FIELD)
3350     {
3351       p_Vid->p_Stats->bit_slice = rewrite_paramsets(p_Vid);
3352     }
3353     // PPS + AUD
3354     else if ( p_Inp->ResendPPS && p_Vid->curr_frm_idx != 0 )
3355     {
3356       int i, total_pps = p_Inp->GenerateMultiplePPS ? 3 : 1;
3357 
3358       p_Vid->p_Stats->bit_ctr_parametersets_n = 0;
3359       // Access Unit Delimiter NALU
3360       if ( p_Inp->SendAUD )
3361       {
3362         p_Vid->p_Stats->bit_ctr_parametersets_n = bits = Write_AUD_NALU(p_Vid);
3363         p_Vid->p_Stats->bit_ctr_parametersets_n_v[p_Vid->view_id] += bits;
3364         for ( i = 0; i < total_pps; i++ )
3365         {
3366           p_Vid->p_Stats->bit_ctr_parametersets_n += bits = write_PPS(p_Vid, 0, i);
3367           p_Vid->p_Stats->bit_ctr_parametersets_n_v[p_Vid->view_id] += bits;
3368         }
3369       }
3370       else
3371       {
3372         for ( i = 0; i < total_pps; i++ )
3373         {
3374           p_Vid->p_Stats->bit_ctr_parametersets_n += bits = write_PPS(p_Vid, 0, i);
3375           p_Vid->p_Stats->bit_ctr_parametersets_n_v[p_Vid->view_id] += bits;
3376         }
3377       }
3378     }
3379     // Access Unit Delimiter NALU
3380     else if ( p_Inp->SendAUD && (p_Vid->curr_frm_idx != 0 || p_Vid->structure == BOTTOM_FIELD) )
3381     {
3382       p_Vid->p_Stats->bit_ctr_parametersets_n += bits = Write_AUD_NALU(p_Vid);
3383       p_Vid->p_Stats->bit_ctr_parametersets_n_v[p_Vid->view_id] += bits;
3384     }
3385   }
3386 
3387   UpdateSubseqInfo (p_Vid, p_Inp, p_Vid->layer);        // Tian Dong (Sept 2002)
3388   UpdateSceneInformation (p_Vid->p_SEI, FALSE, 0, 0, -1); // JVT-D099, scene information SEI, nothing included by default
3389 
3390   //! Commented out by StW, needs fixing in SEI.h to keep the trace file clean
3391   //  PrepareAggregationSEIMessage (p_Vid);
3392 
3393   // write tone mapping SEI message
3394   if (p_Inp->ToneMappingSEIPresentFlag)
3395   {
3396     UpdateToneMapping(p_Vid->p_SEI);
3397   }
3398 
3399   PrepareAggregationSEIMessage(p_Vid);
3400 
3401   p_Vid->p_Stats->bit_ctr_parametersets_n += bits = Write_SEI_NALU(p_Vid, 0);
3402   p_Vid->p_Stats->bit_ctr_parametersets_n_v[p_Vid->view_id] += bits;
3403 
3404   // update seq NVB counter
3405   p_Vid->p_Stats->bit_ctr_parametersets += p_Vid->p_Stats->bit_ctr_parametersets_n;
3406 }
3407 #endif
3408 
3409 
3410