UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
rtpenc_h264.c
Go to the documentation of this file.
1
7/*
8 * Copyright (c) 2005-2010 Fundació i2CAT, Internet I Innovació Digital a Catalunya
9 * Copyright (c) 2014-2022 CESNET
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, is permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 *
25 * This product includes software developed by the University of Southern
26 * California Information Sciences Institute.
27 *
28 * 4. Neither the name of the University nor of the Institute may be used
29 * to endorse or promote products derived from this software without
30 * specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
34 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
35 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
36 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
37 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
42 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
43 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
45 */
46
47#ifdef HAVE_CONFIG_H
48#include "config.h"
49#include "config_unix.h"
50#include "config_win32.h"
51#endif // HAVE_CONFIG_H
52
53#include <stddef.h>
54#include <stdint.h>
55
56#include "debug.h"
57#include "rtp/rtpdec_h264.h"
58#include "rtp/rtpenc_h264.h"
59
60static uint32_t get4Bytes(const unsigned char *ptr) {
61 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
62}
63
70static const unsigned char *get_next_nal(const unsigned char *start, long len, _Bool with_start_code) {
71 const unsigned char * const stop = start + len;
72 while (stop - start >= 4) {
73 uint32_t next4Bytes = get4Bytes(start);
74 if (next4Bytes == 0x00000001) {
75 return start + (with_start_code ? 0 : 4);
76 }
77 if ((next4Bytes & 0xFFFFFF00) == 0x00000100) {
78 return start + (with_start_code ? 0 : 3);
79 }
80 // We save at least some of "next4Bytes".
81 if ((unsigned) (next4Bytes & 0xFF) > 1) {
82 // Common case: 0x00000001 or 0x000001 definitely doesn't begin anywhere in "next4Bytes", so we save all of it:
83 start += 4;
84 } else {
85 // Save the first byte, and continue testing the rest:
86 start += 1;
87 }
88 }
89 return NULL;
90}
91
100const unsigned char *
101rtpenc_get_next_nal(const unsigned char *start, long len,
102 const unsigned char **endptr)
103{
104 const unsigned char *nal = get_next_nal(start, len, 0);
105 if (endptr == NULL) {
106 return nal;
107 }
108 if (nal == NULL) {
109 return NULL;
110 }
111 const unsigned char *end = get_next_nal(nal, len - (nal - start), 1);
112 *endptr = end ? end : start + len;
113 return nal;
114}
115
117const unsigned char *
118rtpenc_get_first_nal(const unsigned char *src, long src_len, bool hevc)
119{
120 const unsigned char *nal = src;
121 int nalu_type = 0;
122 do {
123 nal =
124 rtpenc_get_next_nal(nal, src_len - (nal - src), NULL);
125 if (!nal) {
126 return NULL;
127 }
128 nalu_type = NALU_HDR_GET_TYPE(nal[0], hevc);
129 debug_msg("Received %s NALU.\n", get_nalu_name(nalu_type));
130 } while (nalu_type == NAL_H264_AUD || nalu_type == NAL_HEVC_AUD);
131 return nal;
132}
133
135const char *
137{
138 switch ((enum nal_type) type) {
139 case NAL_H264_NON_IDR:
140 return "H264 non-IDR";
141 case NAL_H264_IDR:
142 return "H264 IDR";
143 case NAL_H264_SEI:
144 return "H264 SEI";
145 case NAL_H264_SPS:
146 return "H264 SPS";
147 case NAL_H264_PPS:
148 return "H264 PPS";
149 case NAL_H264_AUD:
150 return "H264 AUD";
151 case NAL_HEVC_VPS:
152 return "HEVC VPS";
153 case NAL_HEVC_SPS:
154 return "HEVC SPS";
155 case NAL_HEVC_PPS:
156 return "HEVC PPS";
157 case NAL_HEVC_AUD:
158 return "HEVC AUD";
159 }
160 switch ((enum aux_nal_types) type) {
161 case RTP_STAP_A:
162 return "RTP STAP A";
163 case RTP_STAP_B:
164 return "RTP STAP B";
165 case RTP_MTAP16:
166 return "RTP MTAP 16";
167 case RTP_MTAP24:
168 return "RTP MTAP 24";
169 case RTP_FU_A:
170 return "RTP FU A";
171 case RTP_FU_B:
172 return "RTP FU B";
173 }
174 _Thread_local static char buf[32];
175 snprintf(buf, sizeof buf, "(NALU %d)", type);
176 return buf;
177}
int void size_t * len
#define debug_msg(...)
#define debug_msg(...) log_msg(LOG_LEVEL_DEBUG, "[pid/%d +%d %s] ", getpid(), LINE,...
Definition debug.h:92
aux_nal_types
NAL values >23 are invalid in H.264 codestream but used by RTP.
Definition rtpdec_h264.h:73
@ RTP_MTAP16
Definition rtpdec_h264.h:76
@ RTP_STAP_B
Definition rtpdec_h264.h:75
@ RTP_MTAP24
Definition rtpdec_h264.h:77
@ RTP_FU_B
Definition rtpdec_h264.h:79
@ RTP_FU_A
Definition rtpdec_h264.h:78
@ RTP_STAP_A
Definition rtpdec_h264.h:74
nal_type
Definition rtpdec_h264.h:57
@ NAL_H264_AUD
Definition rtpdec_h264.h:64
@ NAL_HEVC_SPS
Definition rtpdec_h264.h:67
@ NAL_HEVC_PPS
Definition rtpdec_h264.h:68
@ NAL_H264_NON_IDR
Definition rtpdec_h264.h:59
@ NAL_H264_PPS
Definition rtpdec_h264.h:63
@ NAL_H264_IDR
Definition rtpdec_h264.h:60
@ NAL_H264_SPS
Definition rtpdec_h264.h:62
@ NAL_HEVC_AUD
Definition rtpdec_h264.h:69
@ NAL_HEVC_VPS
Definition rtpdec_h264.h:66
@ NAL_H264_SEI
Definition rtpdec_h264.h:61
#define NALU_HDR_GET_TYPE(nal, is_hevc)
Definition rtpdec_h264.h:88
const char * get_nalu_name(int type)
const unsigned char * rtpenc_get_next_nal(const unsigned char *start, long len, const unsigned char **endptr)
const unsigned char * rtpenc_get_first_nal(const unsigned char *src, long src_len, bool hevc)
static uint32_t get4Bytes(const unsigned char *ptr)
Definition rtpenc_h264.c:60
static const unsigned char * get_next_nal(const unsigned char *start, long len, _Bool with_start_code)
Definition rtpenc_h264.c:70
static void start()
Definition sdp.c:202
Option::OptType type
Definition settings.cpp:194