00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "global.h"
00016 #include "nalucommon.h"
00017
00018 static FILE *f = NULL;
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 int WriteAnnexbNALU (NALU_t *n)
00032 {
00033 int BitsWritten = 0;
00034 int offset = 0;
00035 int length = 4;
00036 static const byte startcode[] = {0,0,0,1};
00037 byte first_byte;
00038
00039 assert (n != NULL);
00040 assert (n->forbidden_bit == 0);
00041 assert (f != NULL);
00042 assert (n->startcodeprefix_len == 3 || n->startcodeprefix_len == 4);
00043
00044
00045 if (n->startcodeprefix_len < 4)
00046 {
00047 offset = 1;
00048 length = 3;
00049 }
00050
00051 if ( length != fwrite (startcode+offset, 1, length, f))
00052 {
00053 printf ("Fatal: cannot write %d bytes to bitstream file, exit (-1)\n", length);
00054 exit (-1);
00055 }
00056
00057 BitsWritten = (length) << 3;
00058
00059 first_byte = (unsigned char) ((n->forbidden_bit << 7) | (n->nal_reference_idc << 5) | n->nal_unit_type);
00060
00061 if ( 1 != fwrite (&first_byte, 1, 1, f))
00062 {
00063 printf ("Fatal: cannot write %d bytes to bitstream file, exit (-1)\n", 1);
00064 exit (-1);
00065 }
00066
00067 BitsWritten += 8;
00068
00069
00070
00071 if (n->len != fwrite (n->buf, 1, n->len, f))
00072 {
00073 printf ("Fatal: cannot write %d bytes to bitstream file, exit (-1)\n", n->len);
00074 exit (-1);
00075 }
00076 BitsWritten += n->len * 8;
00077
00078 fflush (f);
00079 #if TRACE
00080 fprintf (p_trace, "\n\nAnnex B NALU w/ %s startcode, len %d, forbidden_bit %d, nal_reference_idc %d, nal_unit_type %d\n\n",
00081 n->startcodeprefix_len == 4?"long":"short", n->len + 1, n->forbidden_bit, n->nal_reference_idc, n->nal_unit_type);
00082 fflush (p_trace);
00083 #endif
00084 return BitsWritten;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 void OpenAnnexbFile (char *Filename)
00102 {
00103 if ((f = fopen (Filename, "wb")) == NULL)
00104 {
00105 printf ("Fatal: cannot open Annex B bytestream file '%s', exit (-1)\n", Filename);
00106 exit (-1);
00107 }
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 void CloseAnnexbFile(void)
00121 {
00122 if (fclose (f))
00123 {
00124 printf ("Fatal: cannot close Annex B bytestream file, exit (-1)\n");
00125 exit (-1);
00126 }
00127 }
00128