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

Audio

开发平台:

Visual C++

  1. /*!
  2.  *************************************************************************************
  3.  * file annexb.c
  4.  *
  5.  * brief
  6.  *    Annex B Byte Stream format NAL Unit writing routines
  7.  *
  8.  * author
  9.  *    Main contributors (see contributors.h for copyright, address and affiliation details)
  10.  *      - Stephan Wenger                  <stewe@cs.tu-berlin.de>
  11.  *************************************************************************************
  12.  */
  13. #include "global.h"
  14. #include "nalucommon.h"
  15. static FILE *f = NULL;    // the output file
  16. /*!
  17.  ********************************************************************************************
  18.  * brief
  19.  *    Writes a NALU to the Annex B Byte Stream
  20.  *
  21.  * return
  22.  *    number of bits written
  23.  *
  24.  ********************************************************************************************
  25. */
  26. int WriteAnnexbNALU (NALU_t *n)
  27. {
  28.   int BitsWritten = 0;
  29.   int offset = 0;
  30.   int length = 4;
  31.   static const byte startcode[] = {0,0,0,1};
  32.   byte first_byte;
  33.   assert (n != NULL);
  34.   assert (n->forbidden_bit == 0);
  35.   assert (f != NULL);
  36.   assert (n->startcodeprefix_len == 3 || n->startcodeprefix_len == 4);
  37. // printf ("WriteAnnexbNALU: writing %d bytes w/ startcode_len %dn", n->len+1, n->startcodeprefix_len);
  38.   if (n->startcodeprefix_len < 4)
  39.   {
  40.     offset = 1;
  41.     length = 3;
  42.   }
  43.   if ( length != fwrite (startcode+offset, 1, length, f))
  44.   {
  45.     printf ("Fatal: cannot write %d bytes to bitstream file, exit (-1)n", length);
  46.     exit (-1);
  47.   }
  48.   BitsWritten = (length) << 3;
  49.   first_byte = (unsigned char) ((n->forbidden_bit << 7) | (n->nal_reference_idc << 5) | n->nal_unit_type);
  50.   if ( 1 != fwrite (&first_byte, 1, 1, f))
  51.   {
  52.     printf ("Fatal: cannot write %d bytes to bitstream file, exit (-1)n", 1);
  53.     exit (-1);
  54.   }
  55.   BitsWritten += 8;
  56. // printf ("First Byte %x, nal_ref_idc %x, nal_unit_type %dn", first_byte, n->nal_reference_idc, n->nal_unit_type);
  57.   if (n->len != fwrite (n->buf, 1, n->len, f))
  58.   {
  59.     printf ("Fatal: cannot write %d bytes to bitstream file, exit (-1)n", n->len);
  60.     exit (-1);
  61.   }
  62.   BitsWritten += n->len * 8;
  63.   fflush (f);
  64. #if TRACE
  65.   fprintf (p_trace, "nnAnnex B NALU w/ %s startcode, len %d, forbidden_bit %d, nal_reference_idc %d, nal_unit_type %dnn",
  66.     n->startcodeprefix_len == 4?"long":"short", n->len + 1, n->forbidden_bit, n->nal_reference_idc, n->nal_unit_type);
  67.   fflush (p_trace);
  68. #endif
  69.   return BitsWritten;
  70. }
  71. /*!
  72.  ********************************************************************************************
  73.  * brief
  74.  *    Opens the output file for the bytestream
  75.  *
  76.  * param Filename
  77.  *    The filename of the file to be opened
  78.  *
  79.  * return
  80.  *    none.  Function terminates the program in case of an error
  81.  *
  82.  ********************************************************************************************
  83. */
  84. void OpenAnnexbFile (char *Filename)
  85. {
  86.   if ((f = fopen (Filename, "wb")) == NULL)
  87.   {
  88.     printf ("Fatal: cannot open Annex B bytestream file '%s', exit (-1)n", Filename);
  89.     exit (-1);
  90.   }
  91. }
  92. /*!
  93.  ********************************************************************************************
  94.  * brief
  95.  *    Closes the output bit stream file
  96.  *
  97.  * return
  98.  *    none.  Funtion trerminates the program in case of an error
  99.  ********************************************************************************************
  100. */
  101. void CloseAnnexbFile(void) 
  102. {
  103.   if (fclose (f))
  104.   {
  105.     printf ("Fatal: cannot close Annex B bytestream file, exit (-1)n");
  106.     exit (-1);
  107.   }
  108. }