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