OpenCores
URL https://opencores.org/ocsvn/bluespec-h264/bluespec-h264/trunk

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [test/] [decoder/] [ldecod/] [src/] [ldecod.c] - Blame information for rev 100

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jamey.hick
 
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
 *  \author
9
 *     The main contributors are listed in contributors.h
10
 *
11
 *  \version
12
 *     JM 12.1 (FRExt)
13
 *
14
 *  \note
15
 *     tags are used for document system "doxygen"
16
 *     available at http://www.doxygen.org
17
 */
18
/*!
19
 *  \file
20
 *     ldecod.c
21
 *  \brief
22
 *     H.264/AVC reference decoder project main()
23
 *  \author
24
 *     Main contributors (see contributors.h for copyright, address and affiliation details)
25
 *     - Inge Lille-Langøy       <inge.lille-langoy@telenor.com>
26
 *     - Rickard Sjoberg         <rickard.sjoberg@era.ericsson.se>
27
 *     - Stephan Wenger          <stewe@cs.tu-berlin.de>
28
 *     - Jani Lainema            <jani.lainema@nokia.com>
29
 *     - Sebastian Purreiter     <sebastian.purreiter@mch.siemens.de>
30
 *     - Byeong-Moon Jeon        <jeonbm@lge.com>
31
 *     - Gabi Blaettermann       <blaetter@hhi.de>
32
 *     - Ye-Kui Wang             <wyk@ieee.org>
33
 *     - Valeri George           <george@hhi.de>
34
 *     - Karsten Suehring        <suehring@hhi.de>
35
 *
36
 ***********************************************************************
37
 */
38
 
39
#include "contributors.h"
40
 
41
#include <stdlib.h>
42
#include <stdio.h>
43
#include <string.h>
44
#include <time.h>
45
#include <sys/timeb.h>
46
 
47
#if defined WIN32
48
  #include <io.h>
49
#else
50
  #include <unistd.h>
51
#endif
52
#include <sys/stat.h>
53
#include <fcntl.h>
54
 
55
 
56
#include <assert.h>
57
 
58
#include "global.h"
59
#include "rtp.h"
60
#include "memalloc.h"
61
#include "mbuffer.h"
62
#include "leaky_bucket.h"
63
#include "fmo.h"
64
#include "annexb.h"
65
#include "output.h"
66
#include "cabac.h"
67
#include "parset.h"
68
 
69
#include "erc_api.h"
70
 
71
#define JM          "12 (FRExt)"
72
#define VERSION     "12.1"
73
#define EXT_VERSION "(FRExt)"
74
 
75
#define LOGFILE     "log.dec"
76
#define DATADECFILE "dataDec.txt"
77
#define TRACEFILE   "trace_dec.txt"
78
 
79
extern objectBuffer_t *erc_object_list;
80
extern ercVariables_t *erc_errorVar;
81
extern ColocatedParams *Co_located;
82
 
83
// I have started to move the inp and img structures into global variables.
84
// They are declared in the following lines.  Since inp is defined in conio.h
85
// and cannot be overridden globally, it is defined here as input
86
//
87
// Everywhere, input-> and img-> can now be used either globally or with
88
// the local override through the formal parameter mechanism
89
 
90
extern FILE* bits;
91
extern StorablePicture* dec_picture;
92
 
93
struct inp_par    *input;       //!< input parameters from input configuration file
94
struct snr_par    *snr;         //!< statistics
95
struct img_par    *img;         //!< image parameters
96
 
97
int global_init_done = 0;
98
 
99
/*!
100
 ***********************************************************************
101
 * \brief
102
 *   print help message and exit
103
 ***********************************************************************
104
 */
105
void JMDecHelpExit ()
106
{
107
  fprintf( stderr, "\n   ldecod [-h] {[defdec.cfg] | {[-p pocScale][-i bitstream.264]...[-o output.yuv] [-r reference.yuv] [-uv]}}\n\n"
108
    "## Parameters\n\n"
109
 
110
    "## Options\n"
111
    "   -h  :  prints function usage\n"
112
    "       :  parse <defdec.cfg> for decoder operation.\n"
113
    "   -i  :  Input file name. \n"
114
    "   -o  :  Output file name. If not specified default output is set as test_dec.yuv\n\n"
115
    "   -r  :  Reference file name. If not specified default output is set as test_rec.yuv\n\n"
116
    "   -p  :  Poc Scale. \n"
117
    "   -uv :  write chroma components for monochrome streams(4:2:0)\n\n"
118
 
119
    "## Supported video file formats\n"
120
    "   Input : .264 -> H.264 bitstream files. \n"
121
    "   Output: .yuv -> RAW file. Format depends on bitstream information. \n\n"
122
 
123
    "## Examples of usage:\n"
124
    "   ldecod\n"
125
    "   ldecod  -h\n"
126
    "   ldecod  default.cfg\n"
127
    "   ldecod  -i bitstream.264 -o output.yuv -r reference.yuv\n");
128
 
129
  exit(-1);
130
}
131
 
132
 
133
void Configure(int ac, char *av[])
134
{
135
  int CLcount;
136
  char *config_filename=NULL;
137
  CLcount = 1;
138
 
139
 
140
  strcpy(input->infile,"test.264");      //! set default bitstream name
141
  strcpy(input->outfile,"test_dec.yuv"); //! set default output file name
142
  strcpy(input->reffile,"test_rec.yuv"); //! set default reference file name
143
  input->FileFormat = PAR_OF_ANNEXB;
144
  input->ref_offset=0;
145
  input->poc_scale=2;
146
  input->silent = FALSE;
147
 
148
#ifdef _LEAKYBUCKET_
149
  input->R_decoder=500000;          //! Decoder rate
150
  input->B_decoder=104000;          //! Decoder buffer size
151
  input->F_decoder=73000;           //! Decoder initial delay
152
  strcpy(input->LeakyBucketParamFile,"leakybucketparam.cfg");    // file where Leaky Bucket params (computed by encoder) are stored
153
#endif
154
 
155
  if (ac==2)
156
  {
157
    if (0 == strncmp (av[1], "-h", 2))
158
    {
159
      JMDecHelpExit();
160
    }
161
    else if (0 == strncmp (av[1], "-s", 2))
162
    {
163
      input->silent = TRUE;
164
    }
165
    else
166
    {
167
      config_filename=av[1];
168
      init_conf(input, av[1]);
169
    }
170
    CLcount=2;
171
  }
172
 
173
  if (ac>=3)
174
  {
175
    if (0 == strncmp (av[1], "-i", 2))
176
    {
177
      strcpy(input->infile,av[2]);
178
      CLcount = 3;
179
    }
180
    if (0 == strncmp (av[1], "-h", 2))
181
    {
182
      JMDecHelpExit();
183
    }
184
    if (0 == strncmp (av[1], "-s", 2))
185
    {
186
      input->silent = TRUE;
187
    }
188
  }
189
 
190
  // Parse the command line
191
 
192
  while (CLcount < ac)
193
  {
194
    if (0 == strncmp (av[CLcount], "-h", 2))
195
    {
196
      JMDecHelpExit();
197
    }
198
 
199
    if (0 == strncmp (av[CLcount], "-s", 2))
200
    {
201
      input->silent = TRUE;
202
      CLcount ++;
203
    }
204
 
205
    if (0 == strncmp (av[CLcount], "-i", 2))  //! Input file
206
    {
207
      strcpy(input->infile,av[CLcount+1]);
208
      CLcount += 2;
209
    }
210
    else if (0 == strncmp (av[CLcount], "-o", 2))  //! Output File
211
    {
212
      strcpy(input->outfile,av[CLcount+1]);
213
      CLcount += 2;
214
    }
215
    else if (0 == strncmp (av[CLcount], "-r", 2))  //! Reference File
216
    {
217
      strcpy(input->reffile,av[CLcount+1]);
218
      CLcount += 2;
219
    }
220
    else if (0 == strncmp (av[CLcount], "-p", 2))  //! Poc Scale
221
    {
222
      sscanf (av[CLcount+1], "%d", &input->poc_scale);
223
      CLcount += 2;
224
    }
225
    else if (0 == strncmp (av[CLcount], "-uv", 3))  //! indicate UV writing for 4:0:0
226
    {
227
      input->write_uv = 1;
228
      CLcount ++;
229
    }
230
    else
231
    {
232
      //config_filename=av[CLcount];
233
      //init_conf(input, config_filename);
234
      snprintf(errortext, ET_SIZE, "Invalid syntax. Use ldecod -h for proper usage");
235
      error(errortext, 300);
236
    }
237
  }
238
 
239
 
240
#if TRACE
241
  if ((p_trace=fopen(TRACEFILE,"w"))==0)             // append new statistic at the end
242
  {
243
    snprintf(errortext, ET_SIZE, "Error open file %s!",TRACEFILE);
244
    error(errortext,500);
245
  }
246
#endif
247
 
248
 
249
  if ((p_out=open(input->outfile, OPENFLAGS_WRITE, OPEN_PERMISSIONS))==-1)
250
  {
251
    snprintf(errortext, ET_SIZE, "Error open file %s ",input->outfile);
252
    error(errortext,500);
253
  }
254
/*  if ((p_out2=fopen("out.yuv","wb"))==0)
255
  {
256
    snprintf(errortext, ET_SIZE, "Error open file %s ",input->outfile);
257
    error(errortext,500);
258
  }*/
259
 
260
 
261
  fprintf(stdout,"----------------------------- JM %s %s -----------------------------\n", VERSION, EXT_VERSION);
262
  fprintf(stdout," Decoder config file                    : %s \n",config_filename);
263
  fprintf(stdout,"--------------------------------------------------------------------------\n");
264
  fprintf(stdout," Input H.264 bitstream                  : %s \n",input->infile);
265
  fprintf(stdout," Output decoded YUV                     : %s \n",input->outfile);
266
  fprintf(stdout," Output status file                     : %s \n",LOGFILE);
267
  if ((p_ref=open(input->reffile,OPENFLAGS_READ))==-1)
268
  {
269
    fprintf(stdout," Input reference file                   : %s does not exist \n",input->reffile);
270
    fprintf(stdout,"                                          SNR values are not available\n");
271
  }
272
  else
273
    fprintf(stdout," Input reference file                   : %s \n",input->reffile);
274
 
275
  fprintf(stdout,"--------------------------------------------------------------------------\n");
276
#ifdef _LEAKYBUCKET_
277
  fprintf(stdout," Rate_decoder        : %8ld \n",input->R_decoder);
278
  fprintf(stdout," B_decoder           : %8ld \n",input->B_decoder);
279
  fprintf(stdout," F_decoder           : %8ld \n",input->F_decoder);
280
  fprintf(stdout," LeakyBucketParamFile: %s \n",input->LeakyBucketParamFile); // Leaky Bucket Param file
281
  calc_buffer(input);
282
  fprintf(stdout,"--------------------------------------------------------------------------\n");
283
#endif
284
  fprintf(stdout,"POC must = frame# or field# for SNRs to be correct\n");
285
  fprintf(stdout,"--------------------------------------------------------------------------\n");
286
  fprintf(stdout,"  Frame       POC   Pic#   QP   SnrY    SnrU    SnrV   Y:U:V  Time(ms)\n");
287
  fprintf(stdout,"--------------------------------------------------------------------------\n");
288
 
289
}
290
 
291
/*!
292
 ***********************************************************************
293
 * \brief
294
 *    main function for TML decoder
295
 ***********************************************************************
296
 */
297
int main(int argc, char **argv)
298
{
299
  int i;
300
 
301
  // allocate memory for the structures
302
  if ((input =  (struct inp_par *)calloc(1, sizeof(struct inp_par)))==NULL) no_mem_exit("main: input");
303
  if ((snr   =  (struct snr_par *)calloc(1, sizeof(struct snr_par)))==NULL) no_mem_exit("main: snr");
304
  if ((img   =  (struct img_par *)calloc(1, sizeof(struct img_par)))==NULL) no_mem_exit("main: img");
305
 
306
 
307
  Configure (argc, argv);
308
 
309
  init_old_slice();
310
 
311
  switch (input->FileFormat)
312
  {
313
  case 0:
314
    OpenBitstreamFile (input->infile);
315
    break;
316
  case 1:
317
    OpenRTPFile (input->infile);
318
    break;
319
  default:
320
    printf ("Unsupported file format %d, exit\n", input->FileFormat);
321
  }
322
 
323
  // Allocate Slice data struct
324
  malloc_slice(input,img);
325
 
326
  init(img);
327
 
328
  dec_picture = NULL;
329
 
330
  dpb.init_done = 0;
331
  g_nFrame = 0;
332
 
333
  init_out_buffer();
334
 
335
  img->idr_psnr_number=input->ref_offset;
336
  img->psnr_number=0;
337
 
338
  img->number=0;
339
  img->type = I_SLICE;
340
  img->dec_ref_pic_marking_buffer = NULL;
341
 
342
  // B pictures
343
  Bframe_ctr=snr->frame_ctr=0;
344
 
345
  // time for total decoding session
346
  tot_time = 0;
347
 
348
  // reference flag initialization
349
  for(i=0;i<17;i++)
350
  {
351
    ref_flag[i]=1;
352
  }
353
 
354
  while (decode_one_frame(img, input, snr) != EOS)
355
    ;
356
 
357
  report(input, img, snr);
358
  free_slice(input,img);
359
  FmoFinit();
360
  free_global_buffers();
361
 
362
  flush_dpb();
363
 
364
#ifdef PAIR_FIELDS_IN_OUTPUT
365
  flush_pending_output(p_out);
366
#endif
367
 
368
  CloseBitstreamFile();
369
 
370
  close(p_out);
371
//  fclose(p_out2);
372
  if (p_ref!=-1)
373
    close(p_ref);
374
#if TRACE
375
  fclose(p_trace);
376
#endif
377
 
378
  ercClose(erc_errorVar);
379
 
380
  CleanUpPPS();
381
 
382
  free_dpb();
383
  uninit_out_buffer();
384
  free_colocated(Co_located);
385
  free (input);
386
  free (snr);
387
  free (img);
388
 
389
  //while( !kbhit() );
390
  return 0;
391
}
392
 
393
 
394
/*!
395
 ***********************************************************************
396
 * \brief
397
 *    Initilize some arrays
398
 ***********************************************************************
399
 */
400
void init(struct img_par *img)  //!< image parameters
401
{
402
  img->oldFrameSizeInMbs = -1;
403
 
404
  imgY_ref  = NULL;
405
  imgUV_ref = NULL;
406
 
407
  img->recovery_point = 0;
408
  img->recovery_point_found = 0;
409
  img->recovery_poc = 0x7fffffff; /* set to a max value */
410
}
411
 
412
/*!
413
 ***********************************************************************
414
 * \brief
415
 *    Initialize FREXT variables
416
 ***********************************************************************
417
 */
418
void init_frext(struct img_par *img)  //!< image parameters
419
{
420
  //pel bitdepth init
421
  img->bitdepth_luma_qp_scale   = 6*(img->bitdepth_luma   - 8);
422
  if(img->bitdepth_luma > img->bitdepth_chroma || active_sps->chroma_format_idc == YUV400)
423
    img->pic_unit_bitsize_on_disk = (img->bitdepth_luma > 8)? 16:8;
424
  else
425
    img->pic_unit_bitsize_on_disk = (img->bitdepth_chroma > 8)? 16:8;
426
  img->dc_pred_value_luma = 1<<(img->bitdepth_luma - 1);
427
  img->max_imgpel_value = (1<<img->bitdepth_luma) - 1;
428
  img->mb_size[0][0] = img->mb_size[0][1] = MB_BLOCK_SIZE;
429
 
430
  if (active_sps->chroma_format_idc != YUV400)
431
  {
432
    //for chrominance part
433
    img->bitdepth_chroma_qp_scale = 6*(img->bitdepth_chroma - 8);
434
    img->dc_pred_value_chroma     = 1<<(img->bitdepth_chroma - 1);
435
    img->max_imgpel_value_uv      = (1<<img->bitdepth_chroma) - 1;
436
    img->num_blk8x8_uv = (1<<active_sps->chroma_format_idc)&(~(0x1));
437
    img->num_cdc_coeff = img->num_blk8x8_uv<<1;
438
    img->mb_size[1][0] = img->mb_size[2][0] = img->mb_cr_size_x  = (active_sps->chroma_format_idc==YUV420 || active_sps->chroma_format_idc==YUV422)? 8:16;
439
    img->mb_size[1][1] = img->mb_size[2][1] = img->mb_cr_size_y  = (active_sps->chroma_format_idc==YUV444 || active_sps->chroma_format_idc==YUV422)? 16:8;
440
  }
441
  else
442
  {
443
    img->bitdepth_chroma_qp_scale = 0;
444
    img->max_imgpel_value_uv      = 0;
445
    img->num_blk8x8_uv = 0;
446
    img->num_cdc_coeff = 0;
447
    img->mb_size[1][0] = img->mb_size[2][0] = img->mb_cr_size_x  = 0;
448
    img->mb_size[1][1] = img->mb_size[2][1] = img->mb_cr_size_y  = 0;
449
  }
450
  img->mb_size_blk[0][0] = img->mb_size_blk[0][1] = img->mb_size[0][0] >> 2;
451
  img->mb_size_blk[1][0] = img->mb_size_blk[2][0] = img->mb_size[1][0] >> 2;
452
  img->mb_size_blk[1][1] = img->mb_size_blk[2][1] = img->mb_size[1][1] >> 2;
453
}
454
 
455
 
456
/*!
457
 ************************************************************************
458
 * \brief
459
 *    Read input from configuration file
460
 *
461
 * \par Input:
462
 *    Name of configuration filename
463
 *
464
 * \par Output
465
 *    none
466
 ************************************************************************
467
 */
468
void init_conf(struct inp_par *inp, char *config_filename)
469
{
470
  FILE *fd;
471
  int NAL_mode;
472
 
473
  // picture error concealment
474
  long int temp;
475
  char tempval[100];
476
 
477
  // read the decoder configuration file
478
  if((fd=fopen(config_filename,"r")) == NULL)
479
  {
480
    snprintf(errortext, ET_SIZE, "Error: Control file %s not found\n",config_filename);
481
    error(errortext, 300);
482
  }
483
 
484
  fscanf(fd,"%s",inp->infile);                // H.264 compressed input bitstream
485
  fscanf(fd,"%*[^\n]");
486
 
487
  fscanf(fd,"%s",inp->outfile);               // RAW (YUV/RGB) output file
488
  fscanf(fd,"%*[^\n]");
489
 
490
  fscanf(fd,"%s",inp->reffile);               // reference file
491
  fscanf(fd,"%*[^\n]");
492
 
493
  fscanf(fd,"%d",&(inp->write_uv));           // write UV in YUV 4:0:0 mode
494
  fscanf(fd,"%*[^\n]");
495
 
496
  fscanf(fd,"%d",&(NAL_mode));                // NAL mode
497
  fscanf(fd,"%*[^\n]");
498
 
499
  switch(NAL_mode)
500
  {
501
  case 0:
502
    inp->FileFormat = PAR_OF_ANNEXB;
503
    break;
504
  case 1:
505
    inp->FileFormat = PAR_OF_RTP;
506
    break;
507
  default:
508
    snprintf(errortext, ET_SIZE, "NAL mode %i is not supported", NAL_mode);
509
    error(errortext,400);
510
  }
511
 
512
  fscanf(fd,"%d,",&inp->ref_offset);   // offset used for SNR computation
513
  fscanf(fd,"%*[^\n]");
514
 
515
  fscanf(fd,"%d,",&inp->poc_scale);   // offset used for SNR computation
516
  fscanf(fd,"%*[^\n]");
517
 
518
 
519
  if (inp->poc_scale < 1 || inp->poc_scale > 10)
520
  {
521
    snprintf(errortext, ET_SIZE, "Poc Scale is %d. It has to be within range 1 to 10",inp->poc_scale);
522
    error(errortext,1);
523
  }
524
 
525
  inp->write_uv=1;
526
 
527
  // picture error concealment
528
  img->conceal_mode = inp->conceal_mode = 0;
529
  img->ref_poc_gap = inp->ref_poc_gap = 2;
530
  img->poc_gap = inp->poc_gap = 2;
531
 
532
#ifdef _LEAKYBUCKET_
533
  fscanf(fd,"%ld,",&inp->R_decoder);             // Decoder rate
534
  fscanf(fd, "%*[^\n]");
535
  fscanf(fd,"%ld,",&inp->B_decoder);             // Decoder buffer size
536
  fscanf(fd, "%*[^\n]");
537
  fscanf(fd,"%ld,",&inp->F_decoder);             // Decoder initial delay
538
  fscanf(fd, "%*[^\n]");
539
  fscanf(fd,"%s",inp->LeakyBucketParamFile);    // file where Leaky Bucket params (computed by encoder) are stored
540
  fscanf(fd,"%*[^\n]");
541
#endif
542
 
543
  /* since error concealment parameters are added at the end of
544
  decoder conf file we need to read the leakybucket params to get to
545
  those parameters */
546
#ifndef _LEAKYBUCKET_
547
  fscanf(fd,"%ld,",&temp);
548
  fscanf(fd, "%*[^\n]");
549
  fscanf(fd,"%ld,",&temp);
550
  fscanf(fd, "%*[^\n]");
551
  fscanf(fd,"%ld,",&temp);
552
  fscanf(fd, "%*[^\n]");
553
  fscanf(fd,"%s",tempval);
554
  fscanf(fd,"%*[^\n]");
555
#endif
556
 
557
  fscanf(fd,"%d",&inp->conceal_mode);   // Mode of Error Concealment
558
  fscanf(fd,"%*[^\n]");
559
  img->conceal_mode = inp->conceal_mode;
560
  fscanf(fd,"%d",&inp->ref_poc_gap);   // POC gap depending on pattern
561
  fscanf(fd,"%*[^\n]");
562
  img->ref_poc_gap = inp->ref_poc_gap;
563
  fscanf(fd,"%d",&inp->poc_gap);   // POC gap between consecutive frames in display order
564
  fscanf(fd,"%*[^\n]");
565
  img->poc_gap = inp->poc_gap;
566
  fscanf(fd,"%d,",&inp->silent);     // use silent decode mode
567
  fscanf(fd,"%*[^\n]");
568
 
569
  fclose (fd);
570
}
571
 
572
/*!
573
 ************************************************************************
574
 * \brief
575
 *    Reports the gathered information to appropriate outputs
576
 *
577
 * \par Input:
578
 *    struct inp_par *inp,
579
 *    struct img_par *img,
580
 *    struct snr_par *stat
581
 *
582
 * \par Output:
583
 *    None
584
 ************************************************************************
585
 */
586
void report(struct inp_par *inp, struct img_par *img, struct snr_par *snr)
587
{
588
  #define OUTSTRING_SIZE 255
589
  char string[OUTSTRING_SIZE];
590
  FILE *p_log;
591
  char yuv_formats[4][4]= { {"400"}, {"420"}, {"422"}, {"444"} };
592
 
593
#ifndef WIN32
594
  time_t  now;
595
  struct tm *l_time;
596
#else
597
  char timebuf[128];
598
#endif
599
 
600
  if (input->silent == FALSE)
601
  {
602
    fprintf(stdout,"-------------------- Average SNR all frames ------------------------------\n");
603
    fprintf(stdout," SNR Y(dB)           : %5.2f\n",snr->snr_ya);
604
    fprintf(stdout," SNR U(dB)           : %5.2f\n",snr->snr_ua);
605
    fprintf(stdout," SNR V(dB)           : %5.2f\n",snr->snr_va);
606
    fprintf(stdout," Total decoding time : %.3f sec \n",tot_time*0.001);
607
    fprintf(stdout,"--------------------------------------------------------------------------\n");
608
    fprintf(stdout," Exit JM %s decoder, ver %s ",JM, VERSION);
609
    fprintf(stdout,"\n");
610
  }
611
  else
612
  {
613
    fprintf(stdout,"\n----------------------- Decoding Completed -------------------------------\n");
614
    fprintf(stdout," Total decoding time : %.3f sec \n",tot_time*0.001);
615
    fprintf(stdout,"--------------------------------------------------------------------------\n");
616
    fprintf(stdout," Exit JM %s decoder, ver %s ",JM, VERSION);
617
    fprintf(stdout,"\n");
618
  }
619
 
620
  // write to log file
621
 
622
  snprintf(string, OUTSTRING_SIZE, "%s", LOGFILE);
623
  if ((p_log=fopen(string,"r"))==0)                    // check if file exist
624
  {
625
    if ((p_log=fopen(string,"a"))==0)
626
    {
627
      snprintf(errortext, ET_SIZE, "Error open file %s for appending",string);
628
      error(errortext, 500);
629
    }
630
    else                                              // Create header to new file
631
    {
632
      fprintf(p_log," -------------------------------------------------------------------------------------------------------------------\n");
633
      fprintf(p_log,"|  Decoder statistics. This file is made first time, later runs are appended               |\n");
634
      fprintf(p_log," ------------------------------------------------------------------------------------------------------------------- \n");
635
      fprintf(p_log,"|   ver  | Date  | Time  |    Sequence        |#Img| Format  | YUV |Coding|SNRY 1|SNRU 1|SNRV 1|SNRY N|SNRU N|SNRV N|\n");
636
      fprintf(p_log," -------------------------------------------------------------------------------------------------------------------\n");
637
    }
638
  }
639
  else
640
  {
641
    fclose(p_log);
642
    p_log=fopen(string,"a");                    // File exist,just open for appending
643
  }
644
 
645
  fprintf(p_log,"|%s/%-4s", VERSION, EXT_VERSION);
646
 
647
#ifdef WIN32
648
  _strdate( timebuf );
649
  fprintf(p_log,"| %1.5s |",timebuf );
650
 
651
  _strtime( timebuf);
652
  fprintf(p_log," % 1.5s |",timebuf);
653
#else
654
  now = time ((time_t *) NULL); // Get the system time and put it into 'now' as 'calender time'
655
  time (&now);
656
  l_time = localtime (&now);
657
  strftime (string, sizeof string, "%d-%b-%Y", l_time);
658
  fprintf(p_log,"| %1.5s |",string );
659
 
660
  strftime (string, sizeof string, "%H:%M:%S", l_time);
661
  fprintf(p_log,"| %1.5s |",string );
662
#endif
663
 
664
  fprintf(p_log,"%20.20s|",inp->infile);
665
 
666
  fprintf(p_log,"%3d |",img->number);
667
  fprintf(p_log,"%4dx%-4d|", img->width, img->height);
668
  fprintf(p_log," %s |", &(yuv_formats[img->yuv_format][0]));
669
 
670
  if (active_pps)
671
  {
672
    if (active_pps->entropy_coding_mode_flag == UVLC)
673
      fprintf(p_log," CAVLC|");
674
    else
675
      fprintf(p_log," CABAC|");
676
  }
677
 
678
 
679
  fprintf(p_log,"%6.3f|",snr->snr_y1);
680
  fprintf(p_log,"%6.3f|",snr->snr_u1);
681
  fprintf(p_log,"%6.3f|",snr->snr_v1);
682
  fprintf(p_log,"%6.3f|",snr->snr_ya);
683
  fprintf(p_log,"%6.3f|",snr->snr_ua);
684
  fprintf(p_log,"%6.3f|\n",snr->snr_va);
685
 
686
  fclose(p_log);
687
 
688
  snprintf(string, OUTSTRING_SIZE,"%s", DATADECFILE);
689
  p_log=fopen(string,"a");
690
 
691
  if(Bframe_ctr != 0) // B picture used
692
  {
693
    fprintf(p_log, "%3d %2d %2d %2.2f %2.2f %2.2f %5d "
694
      "%2.2f %2.2f %2.2f %5d "
695
      "%2.2f %2.2f %2.2f %5d %.3f\n",
696
      img->number, 0, img->qp,
697
      snr->snr_y1,
698
      snr->snr_u1,
699
      snr->snr_v1,
700
      0,
701
      0.0,
702
      0.0,
703
      0.0,
704
      0,
705
      snr->snr_ya,
706
      snr->snr_ua,
707
      snr->snr_va,
708
      0,
709
      (double)0.001*tot_time/(img->number+Bframe_ctr-1));
710
  }
711
  else
712
  {
713
    fprintf(p_log, "%3d %2d %2d %2.2f %2.2f %2.2f %5d "
714
      "%2.2f %2.2f %2.2f %5d "
715
      "%2.2f %2.2f %2.2f %5d %.3f\n",
716
      img->number, 0, img->qp,
717
      snr->snr_y1,
718
      snr->snr_u1,
719
      snr->snr_v1,
720
      0,
721
      0.0,
722
      0.0,
723
      0.0,
724
      0,
725
      snr->snr_ya,
726
      snr->snr_ua,
727
      snr->snr_va,
728
      0,
729
      (double)0.001*tot_time/img->number);
730
  }
731
  fclose(p_log);
732
}
733
 
734
/*!
735
 ************************************************************************
736
 * \brief
737
 *    Allocates a stand-alone partition structure.  Structure should
738
 *    be freed by FreePartition();
739
 *    data structures
740
 *
741
 * \par Input:
742
 *    n: number of partitions in the array
743
 * \par return
744
 *    pointer to DataPartition Structure, zero-initialized
745
 ************************************************************************
746
 */
747
 
748
DataPartition *AllocPartition(int n)
749
{
750
  DataPartition *partArr, *dataPart;
751
  int i;
752
 
753
  partArr = (DataPartition *) calloc(n, sizeof(DataPartition));
754
  if (partArr == NULL)
755
  {
756
    snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for Data Partition failed");
757
    error(errortext, 100);
758
  }
759
 
760
  for (i=0; i<n; i++) // loop over all data partitions
761
  {
762
    dataPart = &(partArr[i]);
763
    dataPart->bitstream = (Bitstream *) calloc(1, sizeof(Bitstream));
764
    if (dataPart->bitstream == NULL)
765
    {
766
      snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for Bitstream failed");
767
      error(errortext, 100);
768
    }
769
    dataPart->bitstream->streamBuffer = (byte *) calloc(MAX_CODED_FRAME_SIZE, sizeof(byte));
770
    if (dataPart->bitstream->streamBuffer == NULL)
771
    {
772
      snprintf(errortext, ET_SIZE, "AllocPartition: Memory allocation for streamBuffer failed");
773
      error(errortext, 100);
774
    }
775
  }
776
  return partArr;
777
}
778
 
779
 
780
 
781
 
782
/*!
783
 ************************************************************************
784
 * \brief
785
 *    Frees a partition structure (array).
786
 *
787
 * \par Input:
788
 *    Partition to be freed, size of partition Array (Number of Partitions)
789
 *
790
 * \par return
791
 *    None
792
 *
793
 * \note
794
 *    n must be the same as for the corresponding call of AllocPartition
795
 ************************************************************************
796
 */
797
 
798
 
799
void FreePartition (DataPartition *dp, int n)
800
{
801
  int i;
802
 
803
  assert (dp != NULL);
804
  assert (dp->bitstream != NULL);
805
  assert (dp->bitstream->streamBuffer != NULL);
806
  for (i=0; i<n; i++)
807
  {
808
    free (dp[i].bitstream->streamBuffer);
809
    free (dp[i].bitstream);
810
  }
811
  free (dp);
812
}
813
 
814
 
815
/*!
816
 ************************************************************************
817
 * \brief
818
 *    Allocates the slice structure along with its dependent
819
 *    data structures
820
 *
821
 * \par Input:
822
 *    Input Parameters struct inp_par *inp,  struct img_par *img
823
 ************************************************************************
824
 */
825
void malloc_slice(struct inp_par *inp, struct img_par *img)
826
{
827
  Slice *currSlice;
828
 
829
  img->currentSlice = (Slice *) calloc(1, sizeof(Slice));
830
  if ( (currSlice = img->currentSlice) == NULL)
831
  {
832
    snprintf(errortext, ET_SIZE, "Memory allocation for Slice datastruct in NAL-mode %d failed", inp->FileFormat);
833
    error(errortext,100);
834
  }
835
//  img->currentSlice->rmpni_buffer=NULL;
836
  //! you don't know whether we do CABAC hre, hence initialize CABAC anyway
837
  // if (inp->symbol_mode == CABAC)
838
  if (1)
839
  {
840
    // create all context models
841
    currSlice->mot_ctx = create_contexts_MotionInfo();
842
    currSlice->tex_ctx = create_contexts_TextureInfo();
843
  }
844
  currSlice->max_part_nr = 3;  //! assume data partitioning (worst case) for the following mallocs()
845
  currSlice->partArr = AllocPartition(currSlice->max_part_nr);
846
}
847
 
848
 
849
/*!
850
 ************************************************************************
851
 * \brief
852
 *    Memory frees of the Slice structure and of its dependent
853
 *    data structures
854
 *
855
 * \par Input:
856
 *    Input Parameters struct inp_par *inp,  struct img_par *img
857
 ************************************************************************
858
 */
859
void free_slice(struct inp_par *inp, struct img_par *img)
860
{
861
  Slice *currSlice = img->currentSlice;
862
 
863
  FreePartition (currSlice->partArr, 3);
864
//      if (inp->symbol_mode == CABAC)
865
  if (1)
866
  {
867
    // delete all context models
868
    delete_contexts_MotionInfo(currSlice->mot_ctx);
869
    delete_contexts_TextureInfo(currSlice->tex_ctx);
870
  }
871
  free(img->currentSlice);
872
 
873
  currSlice = NULL;
874
}
875
 
876
/*!
877
 ************************************************************************
878
 * \brief
879
 *    Dynamic memory allocation of frame size related global buffers
880
 *    buffers are defined in global.h, allocated memory must be freed in
881
 *    void free_global_buffers()
882
 *
883
 *  \par Input:
884
 *    Input Parameters struct inp_par *inp, Image Parameters struct img_par *img
885
 *
886
 *  \par Output:
887
 *     Number of allocated bytes
888
 ***********************************************************************
889
 */
890
int init_global_buffers()
891
{
892
  int memory_size=0;
893
  int quad_range, i;
894
 
895
  if (global_init_done)
896
  {
897
    free_global_buffers();
898
  }
899
 
900
  // allocate memory for reference frame in find_snr
901
  memory_size += get_mem2Dpel(&imgY_ref, img->height, img->width);
902
 
903
  if (active_sps->chroma_format_idc != YUV400)
904
    memory_size += get_mem3Dpel(&imgUV_ref, 2, img->height_cr, img->width_cr);
905
  else
906
    imgUV_ref=NULL;
907
 
908
  // allocate memory in structure img
909
  if(((img->mb_data) = (Macroblock *) calloc(img->FrameSizeInMbs, sizeof(Macroblock))) == NULL)
910
    no_mem_exit("init_global_buffers: img->mb_data");
911
 
912
  if(((img->intra_block) = (int*)calloc(img->FrameSizeInMbs, sizeof(int))) == NULL)
913
    no_mem_exit("init_global_buffers: img->intra_block");
914
 
915
  memory_size += get_mem2Dint(&PicPos,img->FrameSizeInMbs + 1,2);  //! Helper array to access macroblock positions. We add 1 to also consider last MB.
916
 
917
  for (i = 0; i < (int) img->FrameSizeInMbs + 1;i++)
918
  {
919
    PicPos[i][0] = (i % img->PicWidthInMbs);
920
    PicPos[i][1] = (i / img->PicWidthInMbs);
921
  }
922
 
923
  memory_size += get_mem2D(&(img->ipredmode), 4*img->FrameHeightInMbs, 4*img->PicWidthInMbs);
924
 
925
  memory_size += get_mem3Dint(&(img->wp_weight), 2, MAX_REFERENCE_PICTURES, 3);
926
  memory_size += get_mem3Dint(&(img->wp_offset), 6, MAX_REFERENCE_PICTURES, 3);
927
  memory_size += get_mem4Dint(&(img->wbp_weight), 6, MAX_REFERENCE_PICTURES, MAX_REFERENCE_PICTURES, 3);
928
 
929
  // CAVLC mem
930
  memory_size += get_mem3Dint(&(img->nz_coeff), img->FrameSizeInMbs, 4, 4 + img->num_blk8x8_uv);
931
 
932
  memory_size += get_mem2Dint(&(img->siblock), img->FrameHeightInMbs, img->PicWidthInMbs);
933
 
934
  if(img->max_imgpel_value > img->max_imgpel_value_uv || active_sps->chroma_format_idc == YUV400)
935
    quad_range = (img->max_imgpel_value + 1) * 2;
936
  else
937
    quad_range = (img->max_imgpel_value_uv + 1) * 2;
938
 
939
  if ((img->quad = (int*)calloc (quad_range, sizeof(int))) == NULL)
940
    no_mem_exit ("init_img: img->quad");
941
 
942
  img->quad+=quad_range/2;
943
  for (i=0; i < quad_range/2; ++i)
944
  {
945
    img->quad[i]=img->quad[-i]=i*i;
946
  }
947
 
948
  global_init_done = 1;
949
 
950
  img->oldFrameSizeInMbs = img->FrameSizeInMbs;
951
 
952
  return (memory_size);
953
}
954
 
955
/*!
956
 ************************************************************************
957
 * \brief
958
 *    Free allocated memory of frame size related global buffers
959
 *    buffers are defined in global.h, allocated memory is allocated in
960
 *    int init_global_buffers()
961
 *
962
 * \par Input:
963
 *    Input Parameters struct inp_par *inp, Image Parameters struct img_par *img
964
 *
965
 * \par Output:
966
 *    none
967
 *
968
 ************************************************************************
969
 */
970
void free_global_buffers()
971
{
972
  free_mem2Dpel (imgY_ref);
973
  if (imgUV_ref)
974
    free_mem3Dpel (imgUV_ref,2);
975
 
976
  // CAVLC free mem
977
  free_mem3Dint(img->nz_coeff, img->oldFrameSizeInMbs);
978
 
979
  free_mem2Dint(img->siblock);
980
 
981
  // free mem, allocated for structure img
982
  if (img->mb_data != NULL)
983
    free(img->mb_data);
984
 
985
  free_mem2Dint(PicPos);
986
 
987
  free (img->intra_block);
988
  free_mem2D(img->ipredmode);
989
 
990
  free_mem3Dint(img->wp_weight, 2);
991
  free_mem3Dint(img->wp_offset, 6);
992
  free_mem4Dint(img->wbp_weight, 6, MAX_REFERENCE_PICTURES);
993
 
994
  if(img->max_imgpel_value > img->max_imgpel_value_uv)
995
    free (img->quad-(img->max_imgpel_value + 1));
996
  else
997
    free (img->quad-(img->max_imgpel_value_uv + 1));
998
 
999
  global_init_done = 0;
1000
 
1001
}
1002
 
1003
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.