UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
yuri_decoders.cpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2013 CESNET z.s.p.o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifdef HAVE_CONFIG_H
39#include "config.h"
40#include "config_unix.h"
41#include "config_win32.h"
42#endif // HAVE_CONFIG_H
43
44#include "audio/utils.h"
45#include "rtp/rtp.h"
46#include "rtp/rtp_callback.h"
47#include "rtp/pbuf.h"
48#include "rtp/audio_decoders.h"
49#include "rtp/yuri_decoders.h"
50#include "rtp/video_decoders.h"
51
52#include "yuri/core/frame/RawAudioFrame.h"
53#include "yuri/core/frame/CompressedVideoFrame.h"
54#include "yuri/core/frame/RawVideoFrame.h"
55#include "modules/ultragrid/YuriUltragrid.h"
56
57using namespace std;
58
60{
61 rtp_packet *pckt = NULL;
62 uint32_t *hdr;
63 int buffer_length;
64 struct audio_desc network_desc;
65 char *received_data = NULL;
66
67 pckt = cdata->data;
68 hdr = (uint32_t *)(void *) pckt->data;
69 parse_audio_hdr(hdr, &network_desc);
70 buffer_length = ntohl(hdr[2]);
71
72 if (network_desc.codec != AC_PCM) {
73 s->log[yuri::log::warning] << "Compressed audio is not yet supported!";
74 return FALSE;
75 }
76
77 yuri::core::pRawAudioFrame frame = yuri::core::RawAudioFrame::create_empty(
78 yuri::ultragrid::audio_uv_to_yuri(network_desc.bps * 8), network_desc.ch_count,
79 network_desc.sample_rate, yuri::uvector<uint8_t>(buffer_length * network_desc.ch_count));
80 received_data = reinterpret_cast<char *>(frame->data());
81 s->yuri_frame = frame;
82 if (!s->yuri_frame) {
83 return FALSE;
84 }
85
86 while (cdata != NULL) {
87 uint32_t *hdr;
88 uint32_t substream;
89 char *data;
90 uint32_t data_pos;
91 int len;
92
93 pckt = cdata->data;
94
95 hdr = (uint32_t *)(void *) pckt->data;
96 data_pos = ntohl(hdr[1]);
97 substream = ntohl(hdr[0]) >> 22;
98
99 len = pckt->data_len - sizeof(audio_payload_hdr_t);
100 data = (char *) hdr + sizeof(audio_payload_hdr_t);
101
102 mux_channel(received_data + data_pos * network_desc.ch_count, data, network_desc.bps,
103 len, network_desc.ch_count, substream, 1.0);
104
105 cdata = cdata->nxt;
106 }
107
108 return TRUE;
109}
110
112{
113 char *received_data = NULL;
114
115 rtp_packet *pckt = NULL;
116 int buffer_length;
117 uint32_t *hdr;
118 struct video_desc network_desc;
119
120 pckt = cdata->data;
121 hdr = (uint32_t *)(void *) pckt->data;
122 parse_video_hdr(hdr, &network_desc);
123 buffer_length = ntohl(hdr[2]);
124
125 s->yuri_frame = yuri::ultragrid::create_yuri_from_uv_desc(&network_desc, buffer_length, s->log);
126 if (!s->yuri_frame) {
127 return FALSE;
128 }
129
130 auto raw = dynamic_pointer_cast<yuri::core::RawVideoFrame>(s->yuri_frame);
131 if (raw) {
132 received_data = reinterpret_cast<char *>(PLANE_RAW_DATA(raw, 0));
133 } else {
134 auto compressed = dynamic_pointer_cast<yuri::core::CompressedVideoFrame>(s->yuri_frame);
135 if (compressed) {
136 received_data = reinterpret_cast<char *>(compressed->begin());
137 }
138 }
139
140 while (cdata != NULL) {
141 char *data;
142 uint32_t data_pos;
143 int len;
144 uint32_t *hdr;
145 uint32_t substream;
146 pckt = cdata->data;
147
148 hdr = (uint32_t *)(void *) pckt->data;
149 data_pos = ntohl(hdr[1]);
150 substream = ntohl(hdr[0]) >> 22;
151
152 if (substream > 0) {
153 s->log[yuri::log::warning] << "Multiple substreams for video yet supported!";
154 return FALSE;
155 }
156
157 len = pckt->data_len - sizeof(video_payload_hdr_t);
158 data = (char *) hdr + sizeof(video_payload_hdr_t);
159
160 memcpy(received_data + data_pos, (unsigned char*) data, len);
161
162 cdata = cdata->nxt;
163 }
164
165 return TRUE;
166}
167
168int decode_yuri_frame(struct coded_data *cdata, void *decoder_data)
169{
170 yuri_decoder_data *s = (yuri_decoder_data *) decoder_data;
171 if (cdata->data->pt == PT_VIDEO) {
172 return decode_yuri_video_frame(cdata, s);
173 } else if (cdata->data ->pt == PT_AUDIO) {
174 return decode_yuri_audio_frame(cdata, s);
175 } else {
176 s->log[yuri::log::warning] << "Unsupported UltraGrid pt " << cdata->data->pt << "!";
177 return FALSE;
178 }
179}
180
struct video_frame * frame
int void size_t * len
@ AC_PCM
Definition types.h:53
bool parse_audio_hdr(uint32_t *hdr, struct audio_desc *desc)
#define TRUE
#define FALSE
bool parse_video_hdr(const uint32_t *hdr, struct video_desc *desc)
rtp_packet
Definition rtp.h:126
#define PT_AUDIO
Definition rtp_types.h:67
#define PT_VIDEO
Definition rtp_types.h:66
uint32_t audio_payload_hdr_t[5]
Definition rtp_types.h:129
uint32_t video_payload_hdr_t[6]
Definition rtp_types.h:107
int sample_rate
Definition types.h:78
int ch_count
Definition types.h:79
int bps
Definition types.h:77
audio_codec_t codec
Definition types.h:80
rtp_packet * data
Definition pbuf.h:75
struct coded_data * nxt
Definition pbuf.h:72
yuri::core::pFrame yuri_frame
yuri::log::Log & log
void mux_channel(char *out, const char *in, int bps, int in_len, int out_stream_channels, int pos_in_stream, double scale)
Definition utils.cpp:371
Video RTP decoder.
static int decode_yuri_audio_frame(struct coded_data *cdata, yuri_decoder_data *s)
static int decode_yuri_video_frame(struct coded_data *cdata, yuri_decoder_data *s)
int decode_yuri_frame(struct coded_data *cdata, void *decoder_data)