00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifdef WIN32
00015 #include <Winsock2.h>
00016 #else
00017 #include <netinet/in.h>
00018 #endif
00019
00020 #include "global.h"
00021 #include "rtp.h"
00022 #include "sei.h"
00023
00024
00025 #if TRACE
00026 #define SYMTRACESTRING(s) strncpy(sym.tracestring,s,TRACESTRING_SIZE)
00027 #else
00028 #define SYMTRACESTRING(s) // to nothing
00029 #endif
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 int ComposeRTPPacket (RTPpacket_t *p)
00061
00062 {
00063 unsigned int temp32;
00064 uint16 temp16;
00065
00066
00067 assert (p->v == 2);
00068 assert (p->p == 0);
00069 assert (p->x == 0);
00070 assert (p->cc == 0);
00071 assert (p->m == 0 || p->m == 1);
00072 assert (p->pt < 128);
00073 assert (p->payload != NULL);
00074 assert (p->paylen < 65536 - 40);
00075 assert (p->packet != NULL);
00076
00077
00078
00079 p->packet[0] = (byte)
00080 ( ((p->v & 0x03) << 6)
00081 | ((p->p & 0x01) << 5)
00082 | ((p->x & 0x01) << 4)
00083 | ((p->cc & 0x0F) << 0) );
00084
00085 p->packet[1] = (byte)
00086 ( ((p->m & 0x01) << 7)
00087 | ((p->pt & 0x7F) << 0) );
00088
00089
00090 temp16 = htons((uint16)p->seq);
00091 memcpy (&p->packet[2], &temp16, 2);
00092
00093
00094 temp32 = htonl(p->timestamp);
00095 memcpy (&p->packet[4], &temp32, 4);
00096
00097 temp32 = htonl(p->ssrc);
00098 memcpy (&p->packet[8], &temp32, 4);
00099
00100
00101
00102 memcpy (&p->packet[12], p->payload, p->paylen);
00103 p->packlen = p->paylen+12;
00104 return 0;
00105 }
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 int WriteRTPPacket (RTPpacket_t *p, FILE *f)
00132
00133 {
00134 int intime = -1;
00135
00136 assert (f != NULL);
00137 assert (p != NULL);
00138
00139
00140 if (1 != fwrite (&p->packlen, 4, 1, f))
00141 return -1;
00142 if (1 != fwrite (&intime, 4, 1, f))
00143 return -1;
00144 if (1 != fwrite (p->packet, p->packlen, 1, f))
00145 return -1;
00146 return 0;
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 int WriteRTPNALU (ImageParameters *p_Img, NALU_t *n)
00174 {
00175 RTPpacket_t *p;
00176
00177 byte first_byte;
00178
00179 assert (p_Img->f_rtp != NULL);
00180 assert (n != NULL);
00181 assert (n->len < 65000);
00182
00183 first_byte = (byte)
00184 (n->forbidden_bit << 7 |
00185 n->nal_reference_idc << 5 |
00186 n->nal_unit_type );
00187
00188
00189 if ((p = (RTPpacket_t *) malloc (sizeof (RTPpacket_t))) == NULL)
00190 no_mem_exit ("RTPWriteNALU-1");
00191 if ((p->packet = malloc (MAXRTPPACKETSIZE)) == NULL)
00192 no_mem_exit ("RTPWriteNALU-2");
00193 if ((p->payload = malloc (MAXRTPPACKETSIZE)) == NULL)
00194 no_mem_exit ("RTPWriteNALU-3");
00195
00196 p->v=2;
00197 p->p=0;
00198 p->x=0;
00199 p->cc=0;
00200 p->m=(n->startcodeprefix_len==4)&1;
00201
00202
00203
00204
00205
00206 p->pt=H264PAYLOADTYPE;
00207 p->seq = p_Img->CurrentRTPSequenceNumber++;
00208 p->timestamp = p_Img->CurrentRTPTimestamp;
00209 p->ssrc=H264SSRC;
00210 p->paylen = 1 + n->len;
00211
00212 p->payload[0] = first_byte;
00213 memcpy (p->payload+1, n->buf, n->len);
00214
00215
00216 if (ComposeRTPPacket (p) < 0)
00217 {
00218 printf ("Cannot compose RTP packet, exit\n");
00219 exit (-1);
00220 }
00221 if (WriteRTPPacket (p, p_Img->f_rtp) < 0)
00222 {
00223 printf ("Cannot write %d bytes of RTP packet to outfile, exit\n", p->packlen);
00224 exit (-1);
00225 }
00226 free (p->packet);
00227 free (p->payload);
00228 free (p);
00229 return (n->len * 8);
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 void RTPUpdateTimestamp (ImageParameters *p_Img, int tr)
00251 {
00252 int delta;
00253 static int oldtr = -1;
00254
00255 if (oldtr == -1)
00256 {
00257 p_Img->CurrentRTPTimestamp = 0;
00258
00259 oldtr = 0;
00260 return;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 delta = tr - oldtr;
00274
00275 if (delta < -10)
00276 delta+=256;
00277
00278 p_Img->CurrentRTPTimestamp += delta * RTP_TR_TIMESTAMP_MULT;
00279 oldtr = tr;
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 void OpenRTPFile (ImageParameters *p_Img, char *Filename)
00300 {
00301 if ((p_Img->f_rtp = fopen (Filename, "wb")) == NULL)
00302 {
00303 printf ("Fatal: cannot open bitstream file '%s', exit (-1)\n", Filename);
00304 exit (-1);
00305 }
00306 }
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 void CloseRTPFile (ImageParameters *p_Img)
00321 {
00322 fclose(p_Img->f_rtp);
00323 }
00324
00325 #if 0
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 int aggregationRTPWriteBits (int Marker, int PacketType, int subPacketType, void * bitstream,
00349 int BitStreamLenInByte, FILE *out)
00350 {
00351 RTPpacket_t *p;
00352 int offset;
00353
00354
00355 assert (out != NULL);
00356 assert (BitStreamLenInByte < 65000);
00357 assert (bitstream != NULL);
00358 assert ((PacketType&0xf) == 4);
00359
00360
00361 p = (RTPpacket_t *) alloca (sizeof (RTPpacket_t));
00362 p->packet=alloca (MAXRTPPACKETSIZE);
00363 p->payload=alloca (MAXRTPPACKETSIZE);
00364 p->v=2;
00365 p->p=0;
00366 p->x=0;
00367 p->cc=0;
00368 p->m=Marker&1;
00369 p->pt=H264PAYLOADTYPE;
00370 p->seq = p_Img->CurrentRTPSequenceNumber++;
00371 p->timestamp = p_Img->CurrentRTPTimestamp;
00372 p->ssrc=H264SSRC;
00373
00374 offset = 0;
00375 p->payload[offset++] = PacketType;
00376
00377
00378 if ( HaveAggregationSEI(p_Img) )
00379 {
00380 p->payload[offset++] = sei_message[AGGREGATION_SEI].subPacketType;
00381 *(short*)&(p->payload[offset]) = sei_message[AGGREGATION_SEI].payloadSize;
00382 offset += 2;
00383 memcpy (&p->payload[offset], sei_message[AGGREGATION_SEI].data, sei_message[AGGREGATION_SEI].payloadSize);
00384 offset += sei_message[AGGREGATION_SEI].payloadSize;
00385
00386 clear_sei_message(p_SEI, AGGREGATION_SEI);
00387 }
00388
00389
00390
00391
00392
00393 p->payload[offset++] = subPacketType;
00394 *(short*)&(p->payload[offset]) = BitStreamLenInByte;
00395 offset += 2;
00396 memcpy (&p->payload[offset], bitstream, BitStreamLenInByte);
00397 offset += BitStreamLenInByte;
00398
00399 p->paylen = offset;
00400
00401
00402
00403 if (ComposeRTPPacket (p) < 0)
00404 {
00405 printf ("Cannot compose RTP packet, exit\n");
00406 exit (-1);
00407 }
00408 if (WriteRTPPacket (p, out) < 0)
00409 {
00410 printf ("Cannot write %d bytes of RTP packet to outfile, exit\n", p->packlen);
00411 exit (-1);
00412 }
00413 return (p->packlen);
00414
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434 Boolean isAggregationPacket(ImageParameters *p_Img)
00435 {
00436 if (HaveAggregationSEI(p_Img))
00437 {
00438 return TRUE;
00439 }
00440
00441
00442 return FALSE;
00443 }
00444 #endif
00445
00446 #if 0
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 void begin_sub_sequence_rtp(ImageParameters *p_Img, InputParameters *p_Inp)
00461 {
00462 if ( p_Inp->of_mode != PAR_OF_RTP || p_Inp->NumFramesInELSubSeq == 0 )
00463 return;
00464
00465
00466 if ( p_Img->gop_number == 0 )
00467 {
00468
00469 InitSubseqInfo(p_Img->p_SEI, 0);
00470 if (1)
00471 UpdateSubseqChar(p_Img);
00472 }
00473
00474 if ( p_Img->gop_number % (p_Inp->NumFramesInELSubSeq+1) == 1 )
00475 {
00476
00477 InitSubseqInfo(p_Img->p_SEI, 1);
00478
00479 if (1)
00480 UpdateSubseqChar(p_Img);
00481 }
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 void end_sub_sequence_rtp(ImageParameters *p_Img, InputParameters *p_Inp)
00497 {
00498
00499 if ( p_Img->number == p_Inp->no_frm_base - 1 )
00500 {
00501
00502 CloseSubseqInfo(p_Img->p_SEI, 0);
00503
00504 }
00505
00506
00507 if ( ((p_Img->gop_number % (p_Inp->NumFramesInELSubSeq+1)==0) && (p_Inp->NumberBFrames != 0) && (p_Img->gop_number > 0)) ||
00508 ((p_Img->gop_number % (p_Inp->NumFramesInELSubSeq+1)==p_Inp->NumFramesInELSubSeq) && (p_Inp->NumberBFrames==0))
00509 )
00510 {
00511
00512 CloseSubseqInfo(p_Img->p_SEI, 1);
00513
00514
00515 }
00516 }
00517
00518 #endif
00519