1
2 /*!
3 ***********************************************************************
4 * \mainpage
5 * This is the H.264/AVC decoder reference software. For detailed documentation
6 * see the comments in each file.
7 *
8 * The JM software web site is located at:
9 * http://iphome.hhi.de/suehring/tml
10 *
11 * For bug reporting and known issues see:
12 * https://ipbt.hhi.fraunhofer.de
13 *
14 * \author
15 * The main contributors are listed in contributors.h
16 *
17 * \version
18 * JM 18.4 (FRExt)
19 *
20 * \note
21 * tags are used for document system "doxygen"
22 * available at http://www.doxygen.org
23 */
24 /*!
25 * \file
26 * ldecod.c
27 * \brief
28 * H.264/AVC reference decoder project main()
29 * \author
30 * Main contributors (see contributors.h for copyright, address and affiliation details)
31 * - Inge Lille-Lang�y <inge.lille-langoy@telenor.com>
32 * - Rickard Sjoberg <rickard.sjoberg@era.ericsson.se>
33 * - Stephan Wenger <stewe@cs.tu-berlin.de>
34 * - Jani Lainema <jani.lainema@nokia.com>
35 * - Sebastian Purreiter <sebastian.purreiter@mch.siemens.de>
36 * - Byeong-Moon Jeon <jeonbm@lge.com>
37 * - Gabi Blaettermann
38 * - Ye-Kui Wang <wyk@ieee.org>
39 * - Valeri George
40 * - Karsten Suehring
41 *
42 ***********************************************************************
43 */
44
45 #include "contributors.h"
46
47 //#include <sys/stat.h>
48
49 #include "global.h"
50 #include "annexb.h"
51 #include "image.h"
52 #include "memalloc.h"
53 #include "mc_prediction.h"
54 #include "mbuffer.h"
55 #include "leaky_bucket.h"
56 #include "fmo.h"
57 #include "output.h"
58 #include "cabac.h"
59 #include "parset.h"
60 #include "sei.h"
61 #include "erc_api.h"
62 #include "quant.h"
63 #include "block.h"
64 #include "nalu.h"
65 #include "img_io.h"
66 #include "loopfilter.h"
67 #include "rtp.h"
68 #include "input.h"
69 #include "output.h"
70 #include "h264decoder.h"
71 #include "dec_statistics.h"
72
73 #define LOGFILE "log.dec"
74 #define DATADECFILE "dataDec.txt"
75 #define TRACEFILE "trace_dec.txt"
76
77 // Decoder definition. This should be the only global variable in the entire
78 // software. Global variables should be avoided.
79 DecoderParams *p_Dec;
80 extern char errortext[ET_SIZE];
81
82 // Prototypes of static functions
83 static void Report (VideoParameters *p_Vid);
84 static void init (VideoParameters *p_Vid);
85 static void free_slice (Slice *currSlice);
86
87 void init_frext(VideoParameters *p_Vid);
88
89 /*!
90 ************************************************************************
91 * \brief
92 * Error handling procedure. Print error message to stderr and exit
93 * with supplied code.
94 * \param text
95 * Error message
96 * \param code
97 * Exit code
98 ************************************************************************
99 */
error(char * text,int code)100 void error(char *text, int code)
101 {
102 fprintf(stderr, "%s\n", text);
103 if (p_Dec)
104 {
105 flush_dpb(p_Dec->p_Vid->p_Dpb_layer[0]);
106 #if (MVC_EXTENSION_ENABLE)
107 flush_dpb(p_Dec->p_Vid->p_Dpb_layer[1]);
108 #endif
109 }
110
111 exit(code);
112 }
113
reset_dpb(VideoParameters * p_Vid,DecodedPictureBuffer * p_Dpb)114 static void reset_dpb( VideoParameters *p_Vid, DecodedPictureBuffer *p_Dpb )
115 {
116 p_Dpb->p_Vid = p_Vid;
117 p_Dpb->init_done = 0;
118 }
119 /*!
120 ***********************************************************************
121 * \brief
122 * Allocate the Video Parameters structure
123 * \par Output:
124 * Video Parameters VideoParameters *p_Vid
125 ***********************************************************************
126 */
alloc_video_params(VideoParameters ** p_Vid)127 static void alloc_video_params( VideoParameters **p_Vid)
128 {
129 int i;
130 if ((*p_Vid = (VideoParameters *) calloc(1, sizeof(VideoParameters)))==NULL)
131 no_mem_exit("alloc_video_params: p_Vid");
132
133 if (((*p_Vid)->old_slice = (OldSliceParams *) calloc(1, sizeof(OldSliceParams)))==NULL)
134 no_mem_exit("alloc_video_params: p_Vid->old_slice");
135
136 if (((*p_Vid)->snr = (SNRParameters *)calloc(1, sizeof(SNRParameters)))==NULL)
137 no_mem_exit("alloc_video_params: p_Vid->snr");
138
139 // Allocate new dpb buffer
140 for (i = 0; i < MAX_NUM_DPB_LAYERS; i++)
141 {
142 if (((*p_Vid)->p_Dpb_layer[i] = (DecodedPictureBuffer*)calloc(1, sizeof(DecodedPictureBuffer)))==NULL)
143 no_mem_exit("alloc_video_params: p_Vid->p_Dpb_layer[i]");
144 (*p_Vid)->p_Dpb_layer[i]->layer_id = i;
145 reset_dpb(*p_Vid, (*p_Vid)->p_Dpb_layer[i]);
146 if(((*p_Vid)->p_EncodePar[i] = (CodingParameters *)calloc(1, sizeof(CodingParameters))) == NULL)
147 no_mem_exit("alloc_video_params:p_Vid->p_EncodePar[i]");
148 ((*p_Vid)->p_EncodePar[i])->layer_id = i;
149 if(((*p_Vid)->p_LayerPar[i] = (LayerParameters *)calloc(1, sizeof(LayerParameters))) == NULL)
150 no_mem_exit("alloc_video_params:p_Vid->p_LayerPar[i]");
151 ((*p_Vid)->p_LayerPar[i])->layer_id = i;
152 }
153 (*p_Vid)->global_init_done[0] = (*p_Vid)->global_init_done[1] = 0;
154
155 #if (ENABLE_OUTPUT_TONEMAPPING)
156 if (((*p_Vid)->seiToneMapping = (ToneMappingSEI*)calloc(1, sizeof(ToneMappingSEI)))==NULL)
157 no_mem_exit("alloc_video_params: (*p_Vid)->seiToneMapping");
158 #endif
159
160 if(((*p_Vid)->ppSliceList = (Slice **) calloc(MAX_NUM_DECSLICES, sizeof(Slice *))) == NULL)
161 {
162 no_mem_exit("alloc_video_params: p_Vid->ppSliceList");
163 }
164 (*p_Vid)->iNumOfSlicesAllocated = MAX_NUM_DECSLICES;
165 //(*p_Vid)->currentSlice = NULL;
166 (*p_Vid)->pNextSlice = NULL;
167 (*p_Vid)->nalu = AllocNALU(MAX_CODED_FRAME_SIZE);
168 (*p_Vid)->pDecOuputPic = (DecodedPicList *)calloc(1, sizeof(DecodedPicList));
169 (*p_Vid)->pNextPPS = AllocPPS();
170 (*p_Vid)->first_sps = TRUE;
171 }
172
173
174 /*!
175 ***********************************************************************
176 * \brief
177 * Allocate the Input structure
178 * \par Output:
179 * Input Parameters InputParameters *p_Vid
180 ***********************************************************************
181 */
alloc_params(InputParameters ** p_Inp)182 static void alloc_params( InputParameters **p_Inp )
183 {
184 if ((*p_Inp = (InputParameters *) calloc(1, sizeof(InputParameters)))==NULL)
185 no_mem_exit("alloc_params: p_Inp");
186 }
187
188 /*!
189 ***********************************************************************
190 * \brief
191 * Allocate the Decoder Structure
192 * \par Output:
193 * Decoder Parameters
194 ***********************************************************************
195 */
alloc_decoder(DecoderParams ** p_Dec)196 static int alloc_decoder( DecoderParams **p_Dec)
197 {
198 if ((*p_Dec = (DecoderParams *) calloc(1, sizeof(DecoderParams)))==NULL)
199 {
200 fprintf(stderr, "alloc_decoder: p_Dec\n");
201 return -1;
202 }
203
204 alloc_video_params(&((*p_Dec)->p_Vid));
205 alloc_params(&((*p_Dec)->p_Inp));
206 (*p_Dec)->p_Vid->p_Inp = (*p_Dec)->p_Inp;
207 (*p_Dec)->p_trace = NULL;
208 (*p_Dec)->bufferSize = 0;
209 (*p_Dec)->bitcounter = 0;
210 return 0;
211 }
212
213 /*!
214 ***********************************************************************
215 * \brief
216 * Free the Image structure
217 * \par Input:
218 * Image Parameters VideoParameters *p_Vid
219 ***********************************************************************
220 */
free_img(VideoParameters * p_Vid)221 static void free_img( VideoParameters *p_Vid)
222 {
223 int i;
224 if (p_Vid != NULL)
225 {
226 if ( p_Vid->p_Inp->FileFormat == PAR_OF_ANNEXB )
227 {
228 free_annex_b (&p_Vid->annex_b);
229 }
230 #if (ENABLE_OUTPUT_TONEMAPPING)
231 if (p_Vid->seiToneMapping != NULL)
232 {
233 free (p_Vid->seiToneMapping);
234 p_Vid->seiToneMapping = NULL;
235 }
236 #endif
237
238 // Free new dpb layers
239 for (i = 0; i < MAX_NUM_DPB_LAYERS; i++)
240 {
241 if (p_Vid->p_Dpb_layer[i] != NULL)
242 {
243 free (p_Vid->p_Dpb_layer[i]);
244 p_Vid->p_Dpb_layer[i] = NULL;
245 }
246 if(p_Vid->p_EncodePar[i])
247 {
248 free(p_Vid->p_EncodePar[i]);
249 p_Vid->p_EncodePar[i] = NULL;
250 }
251 if(p_Vid->p_LayerPar[i])
252 {
253 free(p_Vid->p_LayerPar[i]);
254 p_Vid->p_LayerPar[i] = NULL;
255 }
256 }
257 if (p_Vid->snr != NULL)
258 {
259 free (p_Vid->snr);
260 p_Vid->snr = NULL;
261 }
262 if (p_Vid->old_slice != NULL)
263 {
264 free (p_Vid->old_slice);
265 p_Vid->old_slice = NULL;
266 }
267
268 if(p_Vid->pNextSlice)
269 {
270 free_slice(p_Vid->pNextSlice);
271 p_Vid->pNextSlice=NULL;
272 }
273 if(p_Vid->ppSliceList)
274 {
275 int i;
276 for(i=0; i<p_Vid->iNumOfSlicesAllocated; i++)
277 if(p_Vid->ppSliceList[i])
278 free_slice(p_Vid->ppSliceList[i]);
279 free(p_Vid->ppSliceList);
280 }
281 if(p_Vid->nalu)
282 {
283 FreeNALU(p_Vid->nalu);
284 p_Vid->nalu=NULL;
285 }
286 //free memory;
287 FreeDecPicList(p_Vid->pDecOuputPic);
288 if(p_Vid->pNextPPS)
289 {
290 FreePPS(p_Vid->pNextPPS);
291 p_Vid->pNextPPS = NULL;
292 }
293
294 // clear decoder statistics
295 #if ENABLE_DEC_STATS
296 delete_dec_stats(p_Vid->dec_stats);
297 free (p_Vid->dec_stats);
298 #endif
299
300 free (p_Vid);
301 p_Vid = NULL;
302 }
303 }
304
FreeDecPicList(DecodedPicList * pDecPicList)305 void FreeDecPicList(DecodedPicList *pDecPicList)
306 {
307 while(pDecPicList)
308 {
309 DecodedPicList *pPicNext = pDecPicList->pNext;
310 if(pDecPicList->pY)
311 {
312 free(pDecPicList->pY);
313 pDecPicList->pY = NULL;
314 pDecPicList->pU = NULL;
315 pDecPicList->pV = NULL;
316 }
317 free(pDecPicList);
318 pDecPicList = pPicNext;
319 }
320 }
321
322 /*!
323 ***********************************************************************
324 * \brief
325 * Initilize some arrays
326 ***********************************************************************
327 */
init(VideoParameters * p_Vid)328 static void init(VideoParameters *p_Vid) //!< video parameters
329 {
330 //int i;
331 InputParameters *p_Inp = p_Vid->p_Inp;
332 p_Vid->oldFrameSizeInMbs = (unsigned int) -1;
333
334 p_Vid->imgY_ref = NULL;
335 p_Vid->imgUV_ref = NULL;
336
337 p_Vid->recovery_point = 0;
338 p_Vid->recovery_point_found = 0;
339 p_Vid->recovery_poc = 0x7fffffff; /* set to a max value */
340
341 p_Vid->idr_psnr_number = p_Inp->ref_offset;
342 p_Vid->psnr_number=0;
343
344 p_Vid->number = 0;
345 p_Vid->type = I_SLICE;
346
347 //p_Vid->dec_ref_pic_marking_buffer = NULL;
348
349 p_Vid->g_nFrame = 0;
350 // B pictures
351 p_Vid->Bframe_ctr = p_Vid->snr->frame_ctr = 0;
352
353 // time for total decoding session
354 p_Vid->tot_time = 0;
355
356 p_Vid->dec_picture = NULL;
357 /*// reference flag initialization
358 for(i=0;i<17;++i)
359 {
360 p_Vid->ref_flag[i] = 1;
361 }*/
362
363 p_Vid->MbToSliceGroupMap = NULL;
364 p_Vid->MapUnitToSliceGroupMap = NULL;
365
366 p_Vid->LastAccessUnitExists = 0;
367 p_Vid->NALUCount = 0;
368
369
370 p_Vid->out_buffer = NULL;
371 p_Vid->pending_output = NULL;
372 p_Vid->pending_output_state = FRAME;
373 p_Vid->recovery_flag = 0;
374
375
376 #if (ENABLE_OUTPUT_TONEMAPPING)
377 init_tone_mapping_sei(p_Vid->seiToneMapping);
378 #endif
379
380 #if (MVC_EXTENSION_ENABLE)
381 p_Vid->last_pic_width_in_mbs_minus1 = 0;
382 p_Vid->last_pic_height_in_map_units_minus1 = 0;
383 p_Vid->last_max_dec_frame_buffering = 0;
384 #endif
385
386 p_Vid->newframe = 0;
387 p_Vid->previous_frame_num = 0;
388
389 p_Vid->iLumaPadX = MCBUF_LUMA_PAD_X;
390 p_Vid->iLumaPadY = MCBUF_LUMA_PAD_Y;
391 p_Vid->iChromaPadX = MCBUF_CHROMA_PAD_X;
392 p_Vid->iChromaPadY = MCBUF_CHROMA_PAD_Y;
393
394 p_Vid->iPostProcess = 0;
395 p_Vid->bDeblockEnable = 0x3;
396 p_Vid->last_dec_view_id = -1;
397 p_Vid->last_dec_layer_id = -1;
398
399 #if ENABLE_DEC_STATS
400 if ((p_Vid->dec_stats = (DecStatParameters *) malloc (sizeof (DecStatParameters)))== NULL)
401 no_mem_exit ("init: p_Vid->dec_stats");
402 init_dec_stats(p_Vid->dec_stats);
403 #endif
404 }
405
406 /*!
407 ***********************************************************************
408 * \brief
409 * Initialize FREXT variables
410 ***********************************************************************
411 */
init_frext(VideoParameters * p_Vid)412 void init_frext(VideoParameters *p_Vid) //!< video parameters
413 {
414 //pel bitdepth init
415 p_Vid->bitdepth_luma_qp_scale = 6 * (p_Vid->bitdepth_luma - 8);
416
417 if(p_Vid->bitdepth_luma > p_Vid->bitdepth_chroma || p_Vid->active_sps->chroma_format_idc == YUV400)
418 p_Vid->pic_unit_bitsize_on_disk = (p_Vid->bitdepth_luma > 8)? 16:8;
419 else
420 p_Vid->pic_unit_bitsize_on_disk = (p_Vid->bitdepth_chroma > 8)? 16:8;
421 p_Vid->dc_pred_value_comp[0] = 1<<(p_Vid->bitdepth_luma - 1);
422 p_Vid->max_pel_value_comp[0] = (1<<p_Vid->bitdepth_luma) - 1;
423 p_Vid->mb_size[0][0] = p_Vid->mb_size[0][1] = MB_BLOCK_SIZE;
424
425 if (p_Vid->active_sps->chroma_format_idc != YUV400)
426 {
427 //for chrominance part
428 p_Vid->bitdepth_chroma_qp_scale = 6 * (p_Vid->bitdepth_chroma - 8);
429 p_Vid->dc_pred_value_comp[1] = (1 << (p_Vid->bitdepth_chroma - 1));
430 p_Vid->dc_pred_value_comp[2] = p_Vid->dc_pred_value_comp[1];
431 p_Vid->max_pel_value_comp[1] = (1 << p_Vid->bitdepth_chroma) - 1;
432 p_Vid->max_pel_value_comp[2] = (1 << p_Vid->bitdepth_chroma) - 1;
433 p_Vid->num_blk8x8_uv = (1 << p_Vid->active_sps->chroma_format_idc) & (~(0x1));
434 p_Vid->num_uv_blocks = (p_Vid->num_blk8x8_uv >> 1);
435 p_Vid->num_cdc_coeff = (p_Vid->num_blk8x8_uv << 1);
436 p_Vid->mb_size[1][0] = p_Vid->mb_size[2][0] = p_Vid->mb_cr_size_x = (p_Vid->active_sps->chroma_format_idc==YUV420 || p_Vid->active_sps->chroma_format_idc==YUV422)? 8 : 16;
437 p_Vid->mb_size[1][1] = p_Vid->mb_size[2][1] = p_Vid->mb_cr_size_y = (p_Vid->active_sps->chroma_format_idc==YUV444 || p_Vid->active_sps->chroma_format_idc==YUV422)? 16 : 8;
438
439 p_Vid->subpel_x = p_Vid->mb_cr_size_x == 8 ? 7 : 3;
440 p_Vid->subpel_y = p_Vid->mb_cr_size_y == 8 ? 7 : 3;
441 p_Vid->shiftpel_x = p_Vid->mb_cr_size_x == 8 ? 3 : 2;
442 p_Vid->shiftpel_y = p_Vid->mb_cr_size_y == 8 ? 3 : 2;
443 p_Vid->total_scale = p_Vid->shiftpel_x + p_Vid->shiftpel_y;
444 }
445 else
446 {
447 p_Vid->bitdepth_chroma_qp_scale = 0;
448 p_Vid->max_pel_value_comp[1] = 0;
449 p_Vid->max_pel_value_comp[2] = 0;
450 p_Vid->num_blk8x8_uv = 0;
451 p_Vid->num_uv_blocks = 0;
452 p_Vid->num_cdc_coeff = 0;
453 p_Vid->mb_size[1][0] = p_Vid->mb_size[2][0] = p_Vid->mb_cr_size_x = 0;
454 p_Vid->mb_size[1][1] = p_Vid->mb_size[2][1] = p_Vid->mb_cr_size_y = 0;
455 p_Vid->subpel_x = 0;
456 p_Vid->subpel_y = 0;
457 p_Vid->shiftpel_x = 0;
458 p_Vid->shiftpel_y = 0;
459 p_Vid->total_scale = 0;
460 }
461
462 p_Vid->mb_cr_size = p_Vid->mb_cr_size_x * p_Vid->mb_cr_size_y;
463 p_Vid->mb_size_blk[0][0] = p_Vid->mb_size_blk[0][1] = p_Vid->mb_size[0][0] >> 2;
464 p_Vid->mb_size_blk[1][0] = p_Vid->mb_size_blk[2][0] = p_Vid->mb_size[1][0] >> 2;
465 p_Vid->mb_size_blk[1][1] = p_Vid->mb_size_blk[2][1] = p_Vid->mb_size[1][1] >> 2;
466
467 p_Vid->mb_size_shift[0][0] = p_Vid->mb_size_shift[0][1] = CeilLog2_sf (p_Vid->mb_size[0][0]);
468 p_Vid->mb_size_shift[1][0] = p_Vid->mb_size_shift[2][0] = CeilLog2_sf (p_Vid->mb_size[1][0]);
469 p_Vid->mb_size_shift[1][1] = p_Vid->mb_size_shift[2][1] = CeilLog2_sf (p_Vid->mb_size[1][1]);
470 }
471
472 /*!
473 ************************************************************************
474 * \brief
475 * Reports the gathered information to appropriate outputs
476 *
477 * \par Input:
478 * InputParameters *p_Inp,
479 * VideoParameters *p_Vid,
480 * struct snr_par *stat
481 *
482 * \par Output:
483 * None
484 ************************************************************************
485 */
Report(VideoParameters * p_Vid)486 static void Report(VideoParameters *p_Vid)
487 {
488 static const char yuv_formats[4][4]= { {"400"}, {"420"}, {"422"}, {"444"} };
489 pic_parameter_set_rbsp_t *active_pps = p_Vid->active_pps;
490 InputParameters *p_Inp = p_Vid->p_Inp;
491 SNRParameters *snr = p_Vid->snr;
492 #define OUTSTRING_SIZE 255
493 char string[OUTSTRING_SIZE];
494 FILE *p_log;
495
496 #ifndef WIN32
497 time_t now;
498 struct tm *l_time;
499 #else
500 char timebuf[128];
501 #endif
502
503 // normalize time
504 p_Vid->tot_time = timenorm(p_Vid->tot_time);
505
506 if (p_Inp->silent == FALSE)
507 {
508 fprintf(stdout,"-------------------- Average SNR all frames ------------------------------\n");
509 fprintf(stdout," SNR Y(dB) : %5.2f\n",snr->snra[0]);
510 fprintf(stdout," SNR U(dB) : %5.2f\n",snr->snra[1]);
511 fprintf(stdout," SNR V(dB) : %5.2f\n",snr->snra[2]);
512 fprintf(stdout," Total decoding time : %.3f sec (%.3f fps)[%d frm/%" FORMAT_OFF_T " ms]\n",p_Vid->tot_time*0.001,(snr->frame_ctr ) * 1000.0 / p_Vid->tot_time, snr->frame_ctr, p_Vid->tot_time);
513 fprintf(stdout,"--------------------------------------------------------------------------\n");
514 fprintf(stdout," Exit JM %s decoder, ver %s ",JM, VERSION);
515 fprintf(stdout,"\n");
516 }
517 else
518 {
519 fprintf(stdout,"\n----------------------- Decoding Completed -------------------------------\n");
520 fprintf(stdout," Total decoding time : %.3f sec (%.3f fps)[%d frm/%" FORMAT_OFF_T " ms]\n",p_Vid->tot_time*0.001, (snr->frame_ctr) * 1000.0 / p_Vid->tot_time, snr->frame_ctr, p_Vid->tot_time);
521 fprintf(stdout,"--------------------------------------------------------------------------\n");
522 fprintf(stdout," Exit JM %s decoder, ver %s ",JM, VERSION);
523 fprintf(stdout,"\n");
524 }
525
526 // write to log file
527 fprintf(stdout," Output status file : %s \n",LOGFILE);
528 snprintf(string, OUTSTRING_SIZE, "%s", LOGFILE);
529
530 if ((p_log=fopen(string,"r"))==0) // check if file exist
531 {
532 if ((p_log=fopen(string,"a"))==0)
533 {
534 snprintf(errortext, ET_SIZE, "Error open file %s for appending",string);
535 error(errortext, 500);
536 }
537 else // Create header to new file
538 {
539 fprintf(p_log," -------------------------------------------------------------------------------------------------------------------\n");
540 fprintf(p_log,"| Decoder statistics. This file is made first time, later runs are appended |\n");
541 fprintf(p_log," ------------------------------------------------------------------------------------------------------------------- \n");
542 fprintf(p_log,"| ver | Date | Time | Sequence |#Img| Format | YUV |Coding|SNRY 1|SNRU 1|SNRV 1|SNRY N|SNRU N|SNRV N|\n");
543 fprintf(p_log," -------------------------------------------------------------------------------------------------------------------\n");
544 }
545 }
546 else
547 {
548 fclose(p_log);
549 p_log=fopen(string,"a"); // File exist,just open for appending
550 }
551
552 fprintf(p_log,"|%s/%-4s", VERSION, EXT_VERSION);
553
554 #ifdef WIN32
555 _strdate( timebuf );
556 fprintf(p_log,"| %1.5s |",timebuf );
557
558 _strtime( timebuf);
559 fprintf(p_log," % 1.5s |",timebuf);
560 #else
561 now = time ((time_t *) NULL); // Get the system time and put it into 'now' as 'calender time'
562 time (&now);
563 l_time = localtime (&now);
564 strftime (string, sizeof string, "%d-%b-%Y", l_time);
565 fprintf(p_log,"| %1.5s |",string );
566
567 strftime (string, sizeof string, "%H:%M:%S", l_time);
568 fprintf(p_log,"| %1.5s |",string );
569 #endif
570
571 fprintf(p_log,"%20.20s|",p_Inp->infile);
572
573 fprintf(p_log,"%3d |",p_Vid->number);
574 fprintf(p_log,"%4dx%-4d|", p_Vid->width, p_Vid->height);
575 fprintf(p_log," %s |", &(yuv_formats[p_Vid->yuv_format][0]));
576
577 if (active_pps)
578 {
579 if (active_pps->entropy_coding_mode_flag == (Boolean) CAVLC)
580 fprintf(p_log," CAVLC|");
581 else
582 fprintf(p_log," CABAC|");
583 }
584
585 fprintf(p_log,"%6.3f|",snr->snr1[0]);
586 fprintf(p_log,"%6.3f|",snr->snr1[1]);
587 fprintf(p_log,"%6.3f|",snr->snr1[2]);
588 fprintf(p_log,"%6.3f|",snr->snra[0]);
589 fprintf(p_log,"%6.3f|",snr->snra[1]);
590 fprintf(p_log,"%6.3f|",snr->snra[2]);
591 fprintf(p_log,"\n");
592 fclose(p_log);
593
594 snprintf(string, OUTSTRING_SIZE,"%s", DATADECFILE);
595 p_log=fopen(string,"a");
596
597 if(p_Vid->Bframe_ctr != 0) // B picture used
598 {
599 fprintf(p_log, "%3d %2d %2d %2.2f %2.2f %2.2f %5d "
600 "%2.2f %2.2f %2.2f %5d "
601 "%2.2f %2.2f %2.2f %5d %.3f\n",
602 p_Vid->number, 0, p_Vid->ppSliceList[0]->qp,
603 snr->snr1[0],
604 snr->snr1[1],
605 snr->snr1[2],
606 0,
607 0.0,
608 0.0,
609 0.0,
610 0,
611 snr->snra[0],
612 snr->snra[1],
613 snr->snra[2],
614 0,
615 (double)0.001*p_Vid->tot_time/(p_Vid->number + p_Vid->Bframe_ctr - 1));
616 }
617 else
618 {
619 fprintf(p_log, "%3d %2d %2d %2.2f %2.2f %2.2f %5d "
620 "%2.2f %2.2f %2.2f %5d "
621 "%2.2f %2.2f %2.2f %5d %.3f\n",
622 p_Vid->number, 0, p_Vid->ppSliceList[0]? p_Vid->ppSliceList[0]->qp: 0,
623 snr->snr1[0],
624 snr->snr1[1],
625 snr->snr1[2],
626 0,
627 0.0,
628 0.0,
629 0.0,
630 0,
631 snr->snra[0],
632 snr->snra[1],
633 snr->snra[2],
634 0,
635 p_Vid->number ? ((double)0.001*p_Vid->tot_time/p_Vid->number) : 0.0);
636 }
637 fclose(p_log);
638 }
639
640 /*!
641 ************************************************************************
642 * \brief
643 * Allocates a stand-alone partition structure. Structure should
644 * be freed by FreePartition();
645 * data structures
646 *
647 * \par Input:
648 * n: number of partitions in the array
649 * \par return
650 * pointer to DataPartition Structure, zero-initialized
651 ************************************************************************
652 */
653
AllocPartition(int n)654 DataPartition *AllocPartition(int n)
655 {
656 DataPartition *partArr, *dataPart;
657 int i;
658
659 partArr = (DataPartition *) calloc(n, sizeof(DataPartition));
660 if (partArr == NULL)
661 {
662 snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for Data Partition failed");
663 error(errortext, 100);
664 }
665
666 for (i = 0; i < n; ++i) // loop over all data partitions
667 {
668 dataPart = &(partArr[i]);
669 dataPart->bitstream = (Bitstream *) calloc(1, sizeof(Bitstream));
670 if (dataPart->bitstream == NULL)
671 {
672 snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for Bitstream failed");
673 error(errortext, 100);
674 }
675 dataPart->bitstream->streamBuffer = (byte *) calloc(MAX_CODED_FRAME_SIZE, sizeof(byte));
676 if (dataPart->bitstream->streamBuffer == NULL)
677 {
678 snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for streamBuffer failed");
679 error(errortext, 100);
680 }
681 }
682 return partArr;
683 }
684
685
686
687
688 /*!
689 ************************************************************************
690 * \brief
691 * Frees a partition structure (array).
692 *
693 * \par Input:
694 * Partition to be freed, size of partition Array (Number of Partitions)
695 *
696 * \par return
697 * None
698 *
699 * \note
700 * n must be the same as for the corresponding call of AllocPartition
701 ************************************************************************
702 */
FreePartition(DataPartition * dp,int n)703 void FreePartition (DataPartition *dp, int n)
704 {
705 int i;
706
707 assert (dp != NULL);
708 assert (dp->bitstream != NULL);
709 assert (dp->bitstream->streamBuffer != NULL);
710 for (i=0; i<n; ++i)
711 {
712 free (dp[i].bitstream->streamBuffer);
713 free (dp[i].bitstream);
714 }
715 free (dp);
716 }
717
718
719 /*!
720 ************************************************************************
721 * \brief
722 * Allocates the slice structure along with its dependent
723 * data structures
724 *
725 * \par Input:
726 * Input Parameters InputParameters *p_Inp, VideoParameters *p_Vid
727 ************************************************************************
728 */
malloc_slice(InputParameters * p_Inp,VideoParameters * p_Vid)729 Slice *malloc_slice(InputParameters *p_Inp, VideoParameters *p_Vid)
730 {
731 int i, j, memory_size = 0;
732 Slice *currSlice;
733
734 currSlice = (Slice *) calloc(1, sizeof(Slice));
735 if ( currSlice == NULL)
736 {
737 snprintf(errortext, ET_SIZE, "Memory allocation for Slice datastruct in NAL-mode %d failed", p_Inp->FileFormat);
738 error(errortext,100);
739 }
740
741 // create all context models
742 currSlice->mot_ctx = create_contexts_MotionInfo();
743 currSlice->tex_ctx = create_contexts_TextureInfo();
744
745 currSlice->max_part_nr = 3; //! assume data partitioning (worst case) for the following mallocs()
746 currSlice->partArr = AllocPartition(currSlice->max_part_nr);
747
748 memory_size += get_mem2Dwp (&(currSlice->wp_params), 2, MAX_REFERENCE_PICTURES);
749
750 memory_size += get_mem3Dint(&(currSlice->wp_weight), 2, MAX_REFERENCE_PICTURES, 3);
751 memory_size += get_mem3Dint(&(currSlice->wp_offset), 6, MAX_REFERENCE_PICTURES, 3);
752 memory_size += get_mem4Dint(&(currSlice->wbp_weight), 6, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3);
753
754 memory_size += get_mem3Dpel(&(currSlice->mb_pred), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
755 memory_size += get_mem3Dpel(&(currSlice->mb_rec ), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
756 memory_size += get_mem3Dint(&(currSlice->mb_rres), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
757 memory_size += get_mem3Dint(&(currSlice->cof ), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
758 // memory_size += get_mem3Dint(&(currSlice->fcf ), MAX_PLANE, MB_BLOCK_SIZE, MB_BLOCK_SIZE);
759 allocate_pred_mem(currSlice);
760 #if (MVC_EXTENSION_ENABLE)
761 currSlice->view_id = MVC_INIT_VIEW_ID;
762 currSlice->inter_view_flag = 0;
763 currSlice->anchor_pic_flag = 0;
764 #endif
765 // reference flag initialization
766 for(i=0;i<17;++i)
767 {
768 currSlice->ref_flag[i] = 1;
769 }
770 for (i = 0; i < 6; i++)
771 {
772 currSlice->listX[i] = calloc(MAX_LIST_SIZE, sizeof (StorablePicture*)); // +1 for reordering
773 if (NULL==currSlice->listX[i])
774 no_mem_exit("malloc_slice: currSlice->listX[i]");
775 }
776 for (j = 0; j < 6; j++)
777 {
778 for (i = 0; i < MAX_LIST_SIZE; i++)
779 {
780 currSlice->listX[j][i] = NULL;
781 }
782 currSlice->listXsize[j]=0;
783 }
784
785 return currSlice;
786 }
787
788
789 /*!
790 ************************************************************************
791 * \brief
792 * Memory frees of the Slice structure and of its dependent
793 * data structures
794 *
795 * \par Input:
796 * Input Parameters Slice *currSlice
797 ************************************************************************
798 */
free_slice(Slice * currSlice)799 static void free_slice(Slice *currSlice)
800 {
801 int i;
802
803 if (currSlice->slice_type != I_SLICE && currSlice->slice_type != SI_SLICE)
804 free_ref_pic_list_reordering_buffer(currSlice);
805 free_pred_mem(currSlice);
806 free_mem3Dint(currSlice->cof );
807 free_mem3Dint(currSlice->mb_rres);
808 free_mem3Dpel(currSlice->mb_rec );
809 free_mem3Dpel(currSlice->mb_pred);
810
811 free_mem2Dwp (currSlice->wp_params );
812 free_mem3Dint(currSlice->wp_weight );
813 free_mem3Dint(currSlice->wp_offset );
814 free_mem4Dint(currSlice->wbp_weight);
815
816 FreePartition (currSlice->partArr, 3);
817
818 //if (1)
819 {
820 // delete all context models
821 delete_contexts_MotionInfo (currSlice->mot_ctx);
822 delete_contexts_TextureInfo(currSlice->tex_ctx);
823 }
824
825 for (i=0; i<6; i++)
826 {
827 if (currSlice->listX[i])
828 {
829 free (currSlice->listX[i]);
830 currSlice->listX[i] = NULL;
831 }
832 }
833 while (currSlice->dec_ref_pic_marking_buffer)
834 {
835 DecRefPicMarking_t *tmp_drpm=currSlice->dec_ref_pic_marking_buffer;
836 currSlice->dec_ref_pic_marking_buffer=tmp_drpm->Next;
837 free (tmp_drpm);
838 }
839
840 free(currSlice);
841 currSlice = NULL;
842 }
843
844 /*!
845 ************************************************************************
846 * \brief
847 * Dynamic memory allocation of frame size related global buffers
848 * buffers are defined in global.h, allocated memory must be freed in
849 * void free_global_buffers()
850 *
851 * \par Input:
852 * Input Parameters VideoParameters *p_Vid
853 *
854 * \par Output:
855 * Number of allocated bytes
856 ***********************************************************************
857 */
init_global_buffers(VideoParameters * p_Vid,int layer_id)858 int init_global_buffers(VideoParameters *p_Vid, int layer_id)
859 {
860 int memory_size=0;
861 int i;
862 CodingParameters *cps = p_Vid->p_EncodePar[layer_id];
863 BlockPos* PicPos;
864
865 if (p_Vid->global_init_done[layer_id])
866 {
867 free_layer_buffers(p_Vid, layer_id);
868 }
869
870 // allocate memory for reference frame in find_snr
871 memory_size += get_mem2Dpel(&cps->imgY_ref, cps->height, cps->width);
872 if (cps->yuv_format != YUV400)
873 {
874 memory_size += get_mem3Dpel(&cps->imgUV_ref, 2, cps->height_cr, cps->width_cr);
875 }
876 else
877 cps->imgUV_ref = NULL;
878
879 // allocate memory in structure p_Vid
880 if( (cps->separate_colour_plane_flag != 0) )
881 {
882 for( i=0; i<MAX_PLANE; ++i )
883 {
884 if(((cps->mb_data_JV[i]) = (Macroblock *) calloc(cps->FrameSizeInMbs, sizeof(Macroblock))) == NULL)
885 no_mem_exit("init_global_buffers: cps->mb_data_JV");
886 }
887 cps->mb_data = NULL;
888 }
889 else
890 {
891 if(((cps->mb_data) = (Macroblock *) calloc(cps->FrameSizeInMbs, sizeof(Macroblock))) == NULL)
892 no_mem_exit("init_global_buffers: cps->mb_data");
893 }
894 if( (cps->separate_colour_plane_flag != 0) )
895 {
896 for( i=0; i<MAX_PLANE; ++i )
897 {
898 if(((cps->intra_block_JV[i]) = (char*) calloc(cps->FrameSizeInMbs, sizeof(char))) == NULL)
899 no_mem_exit("init_global_buffers: cps->intra_block_JV");
900 }
901 cps->intra_block = NULL;
902 }
903 else
904 {
905 if(((cps->intra_block) = (char*) calloc(cps->FrameSizeInMbs, sizeof(char))) == NULL)
906 no_mem_exit("init_global_buffers: cps->intra_block");
907 }
908
909
910 //memory_size += get_mem2Dint(&PicPos,p_Vid->FrameSizeInMbs + 1,2); //! Helper array to access macroblock positions. We add 1 to also consider last MB.
911 if(((cps->PicPos) = (BlockPos*) calloc(cps->FrameSizeInMbs + 1, sizeof(BlockPos))) == NULL)
912 no_mem_exit("init_global_buffers: PicPos");
913
914 PicPos = cps->PicPos;
915 for (i = 0; i < (int) cps->FrameSizeInMbs + 1;++i)
916 {
917 PicPos[i].x = (short) (i % cps->PicWidthInMbs);
918 PicPos[i].y = (short) (i / cps->PicWidthInMbs);
919 }
920
921 if( (cps->separate_colour_plane_flag != 0) )
922 {
923 for( i=0; i<MAX_PLANE; ++i )
924 {
925 get_mem2D(&(cps->ipredmode_JV[i]), 4*cps->FrameHeightInMbs, 4*cps->PicWidthInMbs);
926 }
927 cps->ipredmode = NULL;
928 }
929 else
930 memory_size += get_mem2D(&(cps->ipredmode), 4*cps->FrameHeightInMbs, 4*cps->PicWidthInMbs);
931
932 // CAVLC mem
933 memory_size += get_mem4D(&(cps->nz_coeff), cps->FrameSizeInMbs, 3, BLOCK_SIZE, BLOCK_SIZE);
934 if( (cps->separate_colour_plane_flag != 0) )
935 {
936 for( i=0; i<MAX_PLANE; ++i )
937 {
938 get_mem2Dint(&(cps->siblock_JV[i]), cps->FrameHeightInMbs, cps->PicWidthInMbs);
939 if(cps->siblock_JV[i]== NULL)
940 no_mem_exit("init_global_buffers: p_Vid->siblock_JV");
941 }
942 cps->siblock = NULL;
943 }
944 else
945 {
946 memory_size += get_mem2Dint(&(cps->siblock), cps->FrameHeightInMbs, cps->PicWidthInMbs);
947 }
948 init_qp_process(cps);
949 cps->oldFrameSizeInMbs = cps->FrameSizeInMbs;
950
951 if(layer_id == 0 )
952 init_output(cps, ((cps->pic_unit_bitsize_on_disk+7) >> 3));
953 else
954 cps->img2buf = p_Vid->p_EncodePar[0]->img2buf;
955 p_Vid->global_init_done[layer_id] = 1;
956
957 return (memory_size);
958 }
959
960 /*!
961 ************************************************************************
962 * \brief
963 * Free allocated memory of frame size related global buffers
964 * buffers are defined in global.h, allocated memory is allocated in
965 * int init_global_buffers()
966 *
967 * \par Input:
968 * Input Parameters VideoParameters *p_Vid
969 *
970 * \par Output:
971 * none
972 *
973 ************************************************************************
974 */
free_layer_buffers(VideoParameters * p_Vid,int layer_id)975 void free_layer_buffers(VideoParameters *p_Vid, int layer_id)
976 {
977 CodingParameters *cps = p_Vid->p_EncodePar[layer_id];
978
979 if(!p_Vid->global_init_done[layer_id])
980 return;
981
982 if (cps->imgY_ref)
983 {
984 free_mem2Dpel (cps->imgY_ref);
985 cps->imgY_ref = NULL;
986 }
987 if (cps->imgUV_ref)
988 {
989 free_mem3Dpel (cps->imgUV_ref);
990 cps->imgUV_ref = NULL;
991 }
992 // CAVLC free mem
993 if (cps->nz_coeff)
994 {
995 free_mem4D(cps->nz_coeff);
996 cps->nz_coeff = NULL;
997 }
998
999 // free mem, allocated for structure p_Vid
1000 if( (cps->separate_colour_plane_flag != 0) )
1001 {
1002 int i;
1003 for(i=0; i<MAX_PLANE; i++)
1004 {
1005 free(cps->mb_data_JV[i]);
1006 cps->mb_data_JV[i] = NULL;
1007 free_mem2Dint(cps->siblock_JV[i]);
1008 cps->siblock_JV[i] = NULL;
1009 free_mem2D(cps->ipredmode_JV[i]);
1010 cps->ipredmode_JV[i] = NULL;
1011 free (cps->intra_block_JV[i]);
1012 cps->intra_block_JV[i] = NULL;
1013 }
1014 }
1015 else
1016 {
1017 if (cps->mb_data != NULL)
1018 {
1019 free(cps->mb_data);
1020 cps->mb_data = NULL;
1021 }
1022 if(cps->siblock)
1023 {
1024 free_mem2Dint(cps->siblock);
1025 cps->siblock = NULL;
1026 }
1027 if(cps->ipredmode)
1028 {
1029 free_mem2D(cps->ipredmode);
1030 cps->ipredmode = NULL;
1031 }
1032 if(cps->intra_block)
1033 {
1034 free (cps->intra_block);
1035 cps->intra_block = NULL;
1036 }
1037 }
1038 if(cps->PicPos)
1039 {
1040 free(cps->PicPos);
1041 cps->PicPos = NULL;
1042 }
1043
1044 free_qp_matrices(cps);
1045
1046
1047 p_Vid->global_init_done[layer_id] = 0;
1048 }
1049
free_global_buffers(VideoParameters * p_Vid)1050 void free_global_buffers(VideoParameters *p_Vid)
1051 {
1052 if(p_Vid->dec_picture)
1053 {
1054 free_storable_picture(p_Vid->dec_picture);
1055 p_Vid->dec_picture = NULL;
1056 }
1057 #if MVC_EXTENSION_ENABLE
1058 if(p_Vid->active_subset_sps && p_Vid->active_subset_sps->sps.Valid && (p_Vid->active_subset_sps->sps.profile_idc==MVC_HIGH||p_Vid->active_subset_sps->sps.profile_idc == STEREO_HIGH))
1059 free_img_data( p_Vid, &(p_Vid->tempData3) );
1060 #endif
1061 }
1062
report_stats_on_error(void)1063 void report_stats_on_error(void)
1064 {
1065 //free_encoder_memory(p_Vid);
1066 exit (-1);
1067 }
1068
ClearDecPicList(VideoParameters * p_Vid)1069 void ClearDecPicList(VideoParameters *p_Vid)
1070 {
1071 DecodedPicList *pPic = p_Vid->pDecOuputPic, *pPrior = NULL;
1072 //find the head first;
1073 while(pPic && !pPic->bValid)
1074 {
1075 pPrior = pPic;
1076 pPic = pPic->pNext;
1077 }
1078
1079 if(pPic && (pPic != p_Vid->pDecOuputPic))
1080 {
1081 //move all nodes before pPic to the end;
1082 DecodedPicList *pPicTail = pPic;
1083 while(pPicTail->pNext)
1084 pPicTail = pPicTail->pNext;
1085
1086 pPicTail->pNext = p_Vid->pDecOuputPic;
1087 p_Vid->pDecOuputPic = pPic;
1088 pPrior->pNext = NULL;
1089 }
1090 }
1091
get_one_avail_dec_pic_from_list(DecodedPicList * pDecPicList,int b3D,int view_id)1092 DecodedPicList *get_one_avail_dec_pic_from_list(DecodedPicList *pDecPicList, int b3D, int view_id)
1093 {
1094 DecodedPicList *pPic = pDecPicList, *pPrior = NULL;
1095 if(b3D)
1096 {
1097 while(pPic && (pPic->bValid &(1<<view_id)))
1098 {
1099 pPrior = pPic;
1100 pPic = pPic->pNext;
1101 }
1102 }
1103 else
1104 {
1105 while(pPic && (pPic->bValid))
1106 {
1107 pPrior = pPic;
1108 pPic = pPic->pNext;
1109 }
1110 }
1111
1112 if(!pPic)
1113 {
1114 pPic = (DecodedPicList *)calloc(1, sizeof(*pPic));
1115 pPrior->pNext = pPic;
1116 }
1117
1118 return pPic;
1119 }
1120 /************************************
1121 Interface: OpenDecoder
1122 Return:
1123 0: NOERROR;
1124 <0: ERROR;
1125 ************************************/
OpenDecoder(InputParameters * p_Inp)1126 int OpenDecoder(InputParameters *p_Inp)
1127 {
1128 int iRet;
1129 DecoderParams *pDecoder;
1130
1131 iRet = alloc_decoder(&p_Dec);
1132 if(iRet)
1133 {
1134 return (iRet|DEC_ERRMASK);
1135 }
1136 init_time();
1137
1138 pDecoder = p_Dec;
1139 //Configure (pDecoder->p_Vid, pDecoder->p_Inp, argc, argv);
1140 memcpy(pDecoder->p_Inp, p_Inp, sizeof(InputParameters));
1141 pDecoder->p_Vid->conceal_mode = p_Inp->conceal_mode;
1142 pDecoder->p_Vid->ref_poc_gap = p_Inp->ref_poc_gap;
1143 pDecoder->p_Vid->poc_gap = p_Inp->poc_gap;
1144 #if TRACE
1145 if ((pDecoder->p_trace = fopen(TRACEFILE,"w"))==0) // append new statistic at the end
1146 {
1147 snprintf(errortext, ET_SIZE, "Error open file %s!",TRACEFILE);
1148 //error(errortext,500);
1149 return -1;
1150 }
1151 #endif
1152
1153 #if (!MVC_EXTENSION_ENABLE)
1154 if((strcasecmp(p_Inp->outfile, "\"\"")!=0) && (strlen(p_Inp->outfile)>0))
1155 {
1156 if ((pDecoder->p_Vid->p_out = open(p_Inp->outfile, OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
1157 {
1158 snprintf(errortext, ET_SIZE, "Error open file %s ",p_Inp->outfile);
1159 error(errortext,500);
1160 }
1161 }
1162 else
1163 pDecoder->p_Vid->p_out = -1;
1164 #else
1165 {
1166 int i;
1167 VideoParameters *p_Vid = pDecoder->p_Vid;
1168 // Set defaults
1169 p_Vid->p_out = -1;
1170 for(i = 0; i < MAX_VIEW_NUM; i++)
1171 {
1172 p_Vid->p_out_mvc[i] = -1;
1173 }
1174
1175 if (p_Inp->DecodeAllLayers == 1)
1176 {
1177 OpenOutputFiles(p_Vid, 0, 1);
1178 }
1179 else
1180 { //Normal AVC
1181 if((strcasecmp(p_Inp->outfile, "\"\"")!=0) && (strlen(p_Inp->outfile)>0))
1182 {
1183 if( (strcasecmp(p_Inp->outfile, "\"\"")!=0) && ((p_Vid->p_out_mvc[0]=open(p_Inp->outfile, OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1) )
1184 {
1185 snprintf(errortext, ET_SIZE, "Error open file %s ",p_Inp->outfile);
1186 error(errortext,500);
1187 }
1188 }
1189 p_Vid->p_out = p_Vid->p_out_mvc[0];
1190 }
1191 }
1192 #endif
1193
1194
1195 if(strlen(pDecoder->p_Inp->reffile)>0 && strcmp(pDecoder->p_Inp->reffile, "\"\""))
1196 {
1197 if ((pDecoder->p_Vid->p_ref = open(pDecoder->p_Inp->reffile, OPENFLAGS_READ))==-1)
1198 {
1199 fprintf(stdout," Input reference file : %s does not exist \n",pDecoder->p_Inp->reffile);
1200 fprintf(stdout," SNR values are not available\n");
1201 }
1202 }
1203 else
1204 pDecoder->p_Vid->p_ref = -1;
1205
1206 switch( pDecoder->p_Inp->FileFormat )
1207 {
1208 default:
1209 case PAR_OF_ANNEXB:
1210 malloc_annex_b(pDecoder->p_Vid, &pDecoder->p_Vid->annex_b);
1211 open_annex_b(pDecoder->p_Inp->infile, pDecoder->p_Vid->annex_b);
1212 break;
1213 case PAR_OF_RTP:
1214 OpenRTPFile(pDecoder->p_Inp->infile, &pDecoder->p_Vid->BitStreamFile);
1215 break;
1216 }
1217
1218 // Allocate Slice data struct
1219 //pDecoder->p_Vid->currentSlice = NULL; //malloc_slice(pDecoder->p_Inp, pDecoder->p_Vid);
1220
1221 init_old_slice(pDecoder->p_Vid->old_slice);
1222
1223 init(pDecoder->p_Vid);
1224
1225 init_out_buffer(pDecoder->p_Vid);
1226
1227 #if (MVC_EXTENSION_ENABLE)
1228 pDecoder->p_Vid->active_sps = NULL;
1229 pDecoder->p_Vid->active_subset_sps = NULL;
1230 init_subset_sps_list(pDecoder->p_Vid->SubsetSeqParSet, MAXSPS);
1231 #endif
1232
1233
1234 #if _FLTDBG_
1235 pDecoder->p_Vid->fpDbg = fopen("c:/fltdbg.txt", "a");
1236 fprintf(pDecoder->p_Vid->fpDbg, "\ndecoder is opened.\n");
1237 #endif
1238
1239 return DEC_OPEN_NOERR;
1240 }
1241
1242 /************************************
1243 Interface: DecodeOneFrame
1244 Return:
1245 0: NOERROR;
1246 1: Finished decoding;
1247 others: Error Code;
1248 ************************************/
DecodeOneFrame(DecodedPicList ** ppDecPicList)1249 int DecodeOneFrame(DecodedPicList **ppDecPicList)
1250 {
1251 int iRet;
1252 DecoderParams *pDecoder = p_Dec;
1253 ClearDecPicList(pDecoder->p_Vid);
1254 iRet = decode_one_frame(pDecoder);
1255 if(iRet == SOP)
1256 {
1257 iRet = DEC_SUCCEED;
1258 }
1259 else if(iRet == EOS)
1260 {
1261 iRet = DEC_EOS;
1262 }
1263 else
1264 {
1265 iRet |= DEC_ERRMASK;
1266 }
1267
1268 *ppDecPicList = pDecoder->p_Vid->pDecOuputPic;
1269 return iRet;
1270 }
1271
FinitDecoder(DecodedPicList ** ppDecPicList)1272 int FinitDecoder(DecodedPicList **ppDecPicList)
1273 {
1274 DecoderParams *pDecoder = p_Dec;
1275 if(!pDecoder)
1276 return DEC_GEN_NOERR;
1277 ClearDecPicList(pDecoder->p_Vid);
1278 #if (MVC_EXTENSION_ENABLE)
1279 flush_dpb(pDecoder->p_Vid->p_Dpb_layer[0]);
1280 flush_dpb(pDecoder->p_Vid->p_Dpb_layer[1]);
1281 #else
1282 flush_dpb(pDecoder->p_Vid->p_Dpb_layer[0]);
1283 #endif
1284 #if (PAIR_FIELDS_IN_OUTPUT)
1285 flush_pending_output(pDecoder->p_Vid, pDecoder->p_Vid->p_out);
1286 #endif
1287 if (pDecoder->p_Inp->FileFormat == PAR_OF_ANNEXB)
1288 {
1289 reset_annex_b(pDecoder->p_Vid->annex_b);
1290 }
1291 pDecoder->p_Vid->newframe = 0;
1292 pDecoder->p_Vid->previous_frame_num = 0;
1293 *ppDecPicList = pDecoder->p_Vid->pDecOuputPic;
1294 return DEC_GEN_NOERR;
1295 }
1296
CloseDecoder()1297 int CloseDecoder()
1298 {
1299 int i;
1300
1301 DecoderParams *pDecoder = p_Dec;
1302 if(!pDecoder)
1303 return DEC_CLOSE_NOERR;
1304
1305 Report (pDecoder->p_Vid);
1306 FmoFinit(pDecoder->p_Vid);
1307 free_layer_buffers(pDecoder->p_Vid, 0);
1308 free_layer_buffers(pDecoder->p_Vid, 1);
1309 free_global_buffers(pDecoder->p_Vid);
1310 switch( pDecoder->p_Inp->FileFormat )
1311 {
1312 default:
1313 case PAR_OF_ANNEXB:
1314 close_annex_b(pDecoder->p_Vid->annex_b);
1315 break;
1316 case PAR_OF_RTP:
1317 CloseRTPFile(&pDecoder->p_Vid->BitStreamFile);
1318 break;
1319 }
1320
1321 #if (MVC_EXTENSION_ENABLE)
1322 for(i=0;i<MAX_VIEW_NUM;i++)
1323 {
1324 if (pDecoder->p_Vid->p_out_mvc[i] != -1)
1325 {
1326 close(pDecoder->p_Vid->p_out_mvc[i]);
1327 }
1328 }
1329 #else
1330 if(pDecoder->p_Vid->p_out >=0)
1331 close(pDecoder->p_Vid->p_out);
1332 #endif
1333
1334 if (pDecoder->p_Vid->p_ref != -1)
1335 close(pDecoder->p_Vid->p_ref);
1336
1337 #if TRACE
1338 fclose(pDecoder->p_trace);
1339 #endif
1340
1341 ercClose(pDecoder->p_Vid, pDecoder->p_Vid->erc_errorVar);
1342
1343 CleanUpPPS(pDecoder->p_Vid);
1344 #if (MVC_EXTENSION_ENABLE)
1345 for(i=0; i<MAXSPS; i++)
1346 {
1347 reset_subset_sps(pDecoder->p_Vid->SubsetSeqParSet+i);
1348 }
1349 #endif
1350
1351 for(i=0; i<MAX_NUM_DPB_LAYERS; i++)
1352 free_dpb(pDecoder->p_Vid->p_Dpb_layer[i]);
1353
1354
1355 uninit_out_buffer(pDecoder->p_Vid);
1356 #if _FLTDBG_
1357 if(pDecoder->p_Vid->fpDbg)
1358 {
1359 fprintf(pDecoder->p_Vid->fpDbg, "decoder is closed.\n");
1360 fclose(pDecoder->p_Vid->fpDbg);
1361 pDecoder->p_Vid->fpDbg = NULL;
1362 }
1363 #endif
1364
1365 free_img (pDecoder->p_Vid);
1366 free (pDecoder->p_Inp);
1367 free(pDecoder);
1368
1369 p_Dec = NULL;
1370 return DEC_CLOSE_NOERR;
1371 }
1372
1373 #if (MVC_EXTENSION_ENABLE)
OpenOutputFiles(VideoParameters * p_Vid,int view0_id,int view1_id)1374 void OpenOutputFiles(VideoParameters *p_Vid, int view0_id, int view1_id)
1375 {
1376 InputParameters *p_Inp = p_Vid->p_Inp;
1377 char out_ViewFileName[2][FILE_NAME_SIZE], chBuf[FILE_NAME_SIZE], *pch;
1378 if ((strcasecmp(p_Inp->outfile, "\"\"")!=0) && (strlen(p_Inp->outfile)>0))
1379 {
1380 strcpy(chBuf, p_Inp->outfile);
1381 pch = strrchr(chBuf, '.');
1382 if(pch)
1383 *pch = '\0';
1384 if (strcmp("nul", chBuf))
1385 {
1386 sprintf(out_ViewFileName[0], "%s_ViewId%04d.yuv", chBuf, view0_id);
1387 sprintf(out_ViewFileName[1], "%s_ViewId%04d.yuv", chBuf, view1_id);
1388 if(p_Vid->p_out_mvc[0] >= 0)
1389 {
1390 close(p_Vid->p_out_mvc[0]);
1391 p_Vid->p_out_mvc[0] = -1;
1392 }
1393 if ((p_Vid->p_out_mvc[0]=open(out_ViewFileName[0], OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
1394 {
1395 snprintf(errortext, ET_SIZE, "Error open file %s ", out_ViewFileName[0]);
1396 fprintf(stderr, "%s\n", errortext);
1397 exit(500);
1398 }
1399
1400 if(p_Vid->p_out_mvc[1] >= 0)
1401 {
1402 close(p_Vid->p_out_mvc[1]);
1403 p_Vid->p_out_mvc[1] = -1;
1404 }
1405 if ((p_Vid->p_out_mvc[1]=open(out_ViewFileName[1], OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
1406 {
1407 snprintf(errortext, ET_SIZE, "Error open file %s ", out_ViewFileName[1]);
1408 fprintf(stderr, "%s\n", errortext);
1409 exit(500);
1410 }
1411 }
1412 }
1413 }
1414 #endif
1415
set_global_coding_par(VideoParameters * p_Vid,CodingParameters * cps)1416 void set_global_coding_par(VideoParameters *p_Vid, CodingParameters *cps)
1417 {
1418 p_Vid->bitdepth_chroma = 0;
1419 p_Vid->width_cr = 0;
1420 p_Vid->height_cr = 0;
1421 p_Vid->lossless_qpprime_flag = cps->lossless_qpprime_flag;
1422 p_Vid->max_vmv_r = cps->max_vmv_r;
1423
1424 // Fidelity Range Extensions stuff (part 1)
1425 p_Vid->bitdepth_luma = cps->bitdepth_luma;
1426 p_Vid->bitdepth_scale[0] = cps->bitdepth_scale[0];
1427 p_Vid->bitdepth_chroma = cps->bitdepth_chroma;
1428 p_Vid->bitdepth_scale[1] = cps->bitdepth_scale[1];
1429
1430 p_Vid->max_frame_num = cps->max_frame_num;
1431 p_Vid->PicWidthInMbs = cps->PicWidthInMbs;
1432 p_Vid->PicHeightInMapUnits = cps->PicHeightInMapUnits;
1433 p_Vid->FrameHeightInMbs = cps->FrameHeightInMbs;
1434 p_Vid->FrameSizeInMbs = cps->FrameSizeInMbs;
1435
1436 p_Vid->yuv_format = cps->yuv_format;
1437 p_Vid->separate_colour_plane_flag = cps->separate_colour_plane_flag;
1438 p_Vid->ChromaArrayType = cps->ChromaArrayType;
1439
1440 p_Vid->width = cps->width;
1441 p_Vid->height = cps->height;
1442 p_Vid->iLumaPadX = MCBUF_LUMA_PAD_X;
1443 p_Vid->iLumaPadY = MCBUF_LUMA_PAD_Y;
1444 p_Vid->iChromaPadX = MCBUF_CHROMA_PAD_X;
1445 p_Vid->iChromaPadY = MCBUF_CHROMA_PAD_Y;
1446 if (p_Vid->yuv_format == YUV420)
1447 {
1448 p_Vid->width_cr = (p_Vid->width >> 1);
1449 p_Vid->height_cr = (p_Vid->height >> 1);
1450 }
1451 else if (p_Vid->yuv_format == YUV422)
1452 {
1453 p_Vid->width_cr = (p_Vid->width >> 1);
1454 p_Vid->height_cr = p_Vid->height;
1455 p_Vid->iChromaPadY = MCBUF_CHROMA_PAD_Y*2;
1456 }
1457 else if (p_Vid->yuv_format == YUV444)
1458 {
1459 //YUV444
1460 p_Vid->width_cr = p_Vid->width;
1461 p_Vid->height_cr = p_Vid->height;
1462 p_Vid->iChromaPadX = p_Vid->iLumaPadX;
1463 p_Vid->iChromaPadY = p_Vid->iLumaPadY;
1464 }
1465
1466 init_frext(p_Vid);
1467 }
1468