00001
00089 #ifdef WIN32
00090 #include <Winsock2.h>
00091 #else
00092 #include <netinet/in.h>
00093 #endif
00094
00095 #include "contributors.h"
00096 #include "global.h"
00097 #include "errorconcealment.h"
00098 #include "rtp.h"
00099 #include "fmo.h"
00100 #include "sei.h"
00101 #include "memalloc.h"
00102
00103 int RTPReadPacket (RTPpacket_t *p, int bitstream);
00104
00113 void OpenRTPFile (ImageParameters *p_Img, char *fn)
00114 {
00115 if ((p_Img->BitStreamFile = open(fn, OPENFLAGS_READ)) == -1)
00116 {
00117 snprintf (errortext, ET_SIZE, "Cannot open RTP file '%s'", fn);
00118 error(errortext,500);
00119 }
00120 }
00121
00122
00129 void CloseRTPFile(ImageParameters *p_Img)
00130 {
00131 if (p_Img->BitStreamFile != -1)
00132 {
00133 close(p_Img->BitStreamFile);
00134 p_Img->BitStreamFile = - 1;
00135 }
00136 }
00137
00138
00154 int GetRTPNALU (ImageParameters *p_Img, NALU_t *nalu)
00155 {
00156 static uint16 first_call = 1;
00157 static uint16 old_seq = 0;
00158
00159 RTPpacket_t *p;
00160 int ret;
00161
00162 if ((p=malloc (sizeof (RTPpacket_t)))== NULL)
00163 no_mem_exit ("GetRTPNALU-1");
00164 if ((p->packet=malloc (MAXRTPPACKETSIZE))== NULL)
00165 no_mem_exit ("GetRTPNALU-2");
00166 if ((p->payload=malloc (MAXRTPPACKETSIZE))== NULL)
00167 no_mem_exit ("GetRTPNALU-3");
00168
00169 ret = RTPReadPacket (p, p_Img->BitStreamFile);
00170 nalu->forbidden_bit = 1;
00171 nalu->len = 0;
00172
00173 if (ret > 0)
00174 {
00175 if (first_call)
00176 {
00177 first_call = 0;
00178 old_seq = (uint16) (p->seq - 1);
00179 }
00180
00181 nalu->lost_packets = (uint16) ( p->seq - (old_seq + 1) );
00182 old_seq = p->seq;
00183
00184 assert (p->paylen < nalu->max_size);
00185
00186 nalu->len = p->paylen;
00187 memcpy (nalu->buf, p->payload, p->paylen);
00188 nalu->forbidden_bit = (nalu->buf[0]>>7) & 1;
00189 nalu->nal_reference_idc = (NalRefIdc) ((nalu->buf[0]>>5) & 3);
00190 nalu->nal_unit_type = (NaluType) ((nalu->buf[0]) & 0x1f);
00191 if (nalu->lost_packets)
00192 {
00193 printf ("Warning: RTP sequence number discontinuity detected\n");
00194 }
00195 }
00196
00197
00198 free (p->payload);
00199 free (p->packet);
00200 free (p);
00201
00202
00203
00204 if (ret>0)
00205
00206 return nalu->len;
00207 else
00208
00209 return ret;
00210 }
00211
00212
00213
00239 int DecomposeRTPpacket (RTPpacket_t *p)
00240
00241 {
00242
00243 assert (p->packlen < 65536 - 28);
00244 assert (p->packlen >= 12);
00245 assert (p->payload != NULL);
00246 assert (p->packet != NULL);
00247
00248
00249
00250 p->v = (p->packet[0] >> 6) & 0x03;
00251 p->p = (p->packet[0] >> 5) & 0x01;
00252 p->x = (p->packet[0] >> 4) & 0x01;
00253 p->cc = (p->packet[0] >> 0) & 0x0F;
00254
00255 p->m = (p->packet[1] >> 7) & 0x01;
00256 p->pt = (p->packet[1] >> 0) & 0x7F;
00257
00258 memcpy (&p->seq, &p->packet[2], 2);
00259 p->seq = ntohs((uint16)p->seq);
00260
00261 memcpy (&p->timestamp, &p->packet[4], 4);
00262 p->timestamp = ntohl(p->timestamp);
00263 memcpy (&p->ssrc, &p->packet[8], 4);
00264 p->ssrc = ntohl(p->ssrc);
00265
00266
00267 if ( (p->v != 2)
00268 || (p->p != 0)
00269 || (p->x != 0)
00270 || (p->cc != 0) )
00271 {
00272 printf ("DecomposeRTPpacket, RTP header consistency problem, header follows\n");
00273 DumpRTPHeader (p);
00274 return -1;
00275 }
00276 p->paylen = p->packlen-12;
00277 memcpy (p->payload, &p->packet[12], p->paylen);
00278 return 0;
00279 }
00280
00303 void DumpRTPHeader (RTPpacket_t *p)
00304
00305 {
00306 int i;
00307 for (i=0; i< 30; i++)
00308 printf ("%02x ", p->packet[i]);
00309 printf ("Version (V): %d\n", (int) p->v);
00310 printf ("Padding (P): %d\n", (int) p->p);
00311 printf ("Extension (X): %d\n", (int) p->x);
00312 printf ("CSRC count (CC): %d\n", (int) p->cc);
00313 printf ("Marker bit (M): %d\n", (int) p->m);
00314 printf ("Payload Type (PT): %d\n", (int) p->pt);
00315 printf ("Sequence Number: %d\n", (int) p->seq);
00316 printf ("Timestamp: %d\n", (int) p->timestamp);
00317 printf ("SSRC: %d\n", (int) p->ssrc);
00318 }
00319
00320
00348 int RTPReadPacket (RTPpacket_t *p, int bitstream)
00349 {
00350 int64 Filepos;
00351 int intime;
00352
00353 assert (p != NULL);
00354 assert (p->packet != NULL);
00355 assert (p->payload != NULL);
00356
00357 Filepos = tell (bitstream);
00358 if (4 != read (bitstream, &p->packlen, 4))
00359 {
00360 return 0;
00361 }
00362 if (4 != read (bitstream, &intime, 4))
00363 {
00364 lseek (bitstream, Filepos, SEEK_SET);
00365 printf ("RTPReadPacket: File corruption, could not read Timestamp, exit\n");
00366 exit (-1);
00367 }
00368
00369 assert (p->packlen < MAXRTPPACKETSIZE);
00370
00371 if (p->packlen != (unsigned int) read (bitstream, p->packet, p->packlen))
00372 {
00373 printf ("RTPReadPacket: File corruption, could not read %d bytes\n", (int) p->packlen);
00374 exit (-1);
00375 }
00376
00377 if (DecomposeRTPpacket (p) < 0)
00378 {
00379
00380
00381 printf ("Errors reported by DecomposePacket(), exit\n");
00382 exit (-700);
00383 }
00384 assert (p->pt == H264PAYLOADTYPE);
00385 assert (p->ssrc == H264SSRC);
00386 return p->packlen;
00387 }
00388