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