1 /*
2 * RTP Depacketization of RAW video (TR-03)
3 * Copyright (c) 2016 Savoir-faire Linux, Inc
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /* Development sponsored by CBC/Radio-Canada */
23
24 #include "avio_internal.h"
25 #include "rtpdec_formats.h"
26 #include "libavutil/avstring.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavutil/parseutils.h"
29
30 struct PayloadContext {
31 char *sampling;
32 AVRational framerate;
33 int depth;
34 int width;
35 int height;
36 int interlaced;
37 int field;
38
39 uint8_t *frame;
40 unsigned int frame_size;
41 unsigned int pgroup; /* size of the pixel group in bytes */
42 unsigned int xinc;
43
44 uint32_t timestamp;
45 };
46
rfc4175_parse_format(AVStream * stream,PayloadContext * data)47 static int rfc4175_parse_format(AVStream *stream, PayloadContext *data)
48 {
49 enum AVPixelFormat pixfmt;
50 int tag;
51 const AVPixFmtDescriptor *desc;
52
53 if (!strncmp(data->sampling, "YCbCr-4:2:2", 11)) {
54 tag = MKTAG('U', 'Y', 'V', 'Y');
55 data->xinc = 2;
56
57 if (data->depth == 8) {
58 data->pgroup = 4;
59 pixfmt = AV_PIX_FMT_UYVY422;
60 stream->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
61 } else if (data->depth == 10) {
62 data->pgroup = 5;
63 pixfmt = AV_PIX_FMT_YUV422P10;
64 stream->codecpar->codec_id = AV_CODEC_ID_BITPACKED;
65 } else {
66 return AVERROR_INVALIDDATA;
67 }
68 } else if (!strncmp(data->sampling, "YCbCr-4:2:0", 11)) {
69 tag = MKTAG('I', '4', '2', '0');
70 data->xinc = 4;
71
72 if (data->depth == 8) {
73 data->pgroup = 6;
74 pixfmt = AV_PIX_FMT_YUV420P;
75 stream->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
76 } else {
77 return AVERROR_INVALIDDATA;
78 }
79 } else if (!strncmp(data->sampling, "RGB", 3)) {
80 tag = MKTAG('R', 'G', 'B', 24);
81 if (data->depth == 8) {
82 data->xinc = 1;
83 data->pgroup = 3;
84 pixfmt = AV_PIX_FMT_RGB24;
85 stream->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
86 } else {
87 return AVERROR_INVALIDDATA;
88 }
89 } else if (!strncmp(data->sampling, "BGR", 3)) {
90 tag = MKTAG('B', 'G', 'R', 24);
91 if (data->depth == 8) {
92 data->xinc = 1;
93 data->pgroup = 3;
94 pixfmt = AV_PIX_FMT_BGR24;
95 stream->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
96 } else {
97 return AVERROR_INVALIDDATA;
98 }
99 } else {
100 return AVERROR_INVALIDDATA;
101 }
102
103 desc = av_pix_fmt_desc_get(pixfmt);
104 stream->codecpar->format = pixfmt;
105 stream->codecpar->codec_tag = tag;
106 stream->codecpar->bits_per_coded_sample = av_get_bits_per_pixel(desc);
107 data->frame_size = data->width * data->height * data->pgroup / data->xinc;
108
109 if (data->interlaced)
110 stream->codecpar->field_order = AV_FIELD_TT;
111 else
112 stream->codecpar->field_order = AV_FIELD_PROGRESSIVE;
113
114 if (data->framerate.den > 0) {
115 stream->avg_frame_rate = data->framerate;
116 stream->codecpar->bit_rate = data->frame_size * av_q2d(data->framerate) * 8;
117 }
118
119 return 0;
120 }
121
rfc4175_parse_fmtp(AVFormatContext * s,AVStream * stream,PayloadContext * data,const char * attr,const char * value)122 static int rfc4175_parse_fmtp(AVFormatContext *s, AVStream *stream,
123 PayloadContext *data, const char *attr,
124 const char *value)
125 {
126 if (!strncmp(attr, "width", 5))
127 data->width = atoi(value);
128 else if (!strncmp(attr, "height", 6))
129 data->height = atoi(value);
130 else if (!strncmp(attr, "sampling", 8))
131 data->sampling = av_strdup(value);
132 else if (!strncmp(attr, "depth", 5))
133 data->depth = atoi(value);
134 else if (!strncmp(attr, "interlace", 9))
135 data->interlaced = 1;
136 else if (!strncmp(attr, "exactframerate", 14)) {
137 if (av_parse_video_rate(&data->framerate, value) < 0)
138 return AVERROR(EINVAL);
139 } else if (!strncmp(attr, "TCS", 3)) {
140 if (!strncmp(value, "SDR", 3))
141 stream->codecpar->color_trc = AVCOL_TRC_BT709;
142 else if (!strncmp(value, "PQ", 2))
143 stream->codecpar->color_trc = AVCOL_TRC_SMPTE2084;
144 else if (!strncmp(value, "HLG", 3))
145 stream->codecpar->color_trc = AVCOL_TRC_ARIB_STD_B67;
146 else if (!strncmp(value, "LINEAR", 6))
147 stream->codecpar->color_trc = AVCOL_TRC_LINEAR;
148 else if (!strncmp(value, "ST428-1", 7))
149 stream->codecpar->color_trc = AVCOL_TRC_SMPTEST428_1;
150 else
151 stream->codecpar->color_trc = AVCOL_TRC_UNSPECIFIED;
152 } else if (!strncmp(attr, "colorimetry", 11)) {
153 if (!strncmp(value, "BT601", 5)) {
154 stream->codecpar->color_primaries = AVCOL_PRI_BT470BG;
155 stream->codecpar->color_space = AVCOL_SPC_BT470BG;
156 } else if (!strncmp(value, "BT709", 5)) {
157 stream->codecpar->color_primaries = AVCOL_PRI_BT709;
158 stream->codecpar->color_space = AVCOL_SPC_BT709;
159 } else if (!strncmp(value, "BT2020", 6)) {
160 stream->codecpar->color_primaries = AVCOL_PRI_BT2020;
161 stream->codecpar->color_space = AVCOL_SPC_BT2020_NCL;
162 }
163 } else if (!strncmp(attr, "RANGE", 5)) {
164 if (!strncmp(value, "NARROW", 6))
165 stream->codecpar->color_range = AVCOL_RANGE_MPEG;
166 else if (!strncmp(value, "FULL", 4))
167 stream->codecpar->color_range = AVCOL_RANGE_JPEG;
168 }
169
170 return 0;
171 }
172
rfc4175_parse_sdp_line(AVFormatContext * s,int st_index,PayloadContext * data,const char * line)173 static int rfc4175_parse_sdp_line(AVFormatContext *s, int st_index,
174 PayloadContext *data, const char *line)
175 {
176 const char *p;
177
178 if (st_index < 0)
179 return 0;
180
181 if (av_strstart(line, "fmtp:", &p)) {
182 AVStream *stream = s->streams[st_index];
183 int ret = ff_parse_fmtp(s, stream, data, p, rfc4175_parse_fmtp);
184
185 if (ret < 0)
186 return ret;
187
188
189 if (!data->sampling || !data->depth || !data->width || !data->height)
190 return AVERROR(EINVAL);
191
192 stream->codecpar->width = data->width;
193 stream->codecpar->height = data->height;
194
195 ret = rfc4175_parse_format(stream, data);
196 av_freep(&data->sampling);
197
198 return ret;
199 }
200
201 return 0;
202 }
203
rfc4175_finalize_packet(PayloadContext * data,AVPacket * pkt,int stream_index)204 static int rfc4175_finalize_packet(PayloadContext *data, AVPacket *pkt,
205 int stream_index)
206 {
207 int ret = 0;
208
209 pkt->stream_index = stream_index;
210 if (!data->interlaced || data->field) {
211 ret = av_packet_from_data(pkt, data->frame, data->frame_size);
212 if (ret < 0) {
213 av_freep(&data->frame);
214 }
215 data->frame = NULL;
216 }
217
218 data->field = 0;
219
220 return ret;
221 }
222
rfc4175_handle_packet(AVFormatContext * ctx,PayloadContext * data,AVStream * st,AVPacket * pkt,uint32_t * timestamp,const uint8_t * buf,int len,uint16_t seq,int flags)223 static int rfc4175_handle_packet(AVFormatContext *ctx, PayloadContext *data,
224 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
225 const uint8_t * buf, int len,
226 uint16_t seq, int flags)
227 {
228 int length, line, offset, cont, field;
229 const uint8_t *headers = buf + 2; /* skip extended seqnum */
230 const uint8_t *payload = buf + 2;
231 int payload_len = len - 2;
232 int missed_last_packet = 0;
233
234 uint8_t *dest;
235
236 if (*timestamp != data->timestamp) {
237 if (data->frame && (!data->interlaced || data->field)) {
238 /*
239 * if we're here, it means that we missed the cue to return
240 * the previous AVPacket, that cue being the RTP_FLAG_MARKER
241 * in the last packet of either the previous frame (progressive)
242 * or the previous second field (interlace). Let's finalize the
243 * previous frame (or pair of fields) anyway by filling the AVPacket.
244 */
245 av_log(ctx, AV_LOG_ERROR, "Missed previous RTP Marker\n");
246 missed_last_packet = 1;
247 rfc4175_finalize_packet(data, pkt, st->index);
248 }
249
250 if (!data->frame)
251 data->frame = av_malloc(data->frame_size);
252
253 data->timestamp = *timestamp;
254
255 if (!data->frame) {
256 av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
257 return AVERROR(ENOMEM);
258 }
259 }
260
261 /*
262 * looks for the 'Continuation bit' in scan lines' headers
263 * to find where data start
264 */
265 do {
266 if (payload_len < 6)
267 return AVERROR_INVALIDDATA;
268
269 cont = payload[4] & 0x80;
270 payload += 6;
271 payload_len -= 6;
272 } while (cont);
273
274 /* and now iterate over every scan lines */
275 do {
276 int copy_offset;
277
278 if (payload_len < data->pgroup)
279 return AVERROR_INVALIDDATA;
280
281 length = (headers[0] << 8) | headers[1];
282 field = (headers[2] & 0x80) >> 7;
283 line = ((headers[2] & 0x7f) << 8) | headers[3];
284 offset = ((headers[4] & 0x7f) << 8) | headers[5];
285 cont = headers[4] & 0x80;
286 headers += 6;
287 data->field = field;
288
289 if (!data->pgroup || length % data->pgroup)
290 return AVERROR_INVALIDDATA;
291
292 if (length > payload_len)
293 length = payload_len;
294
295 if (data->interlaced)
296 line = 2 * line + field;
297
298 /* prevent ill-formed packets to write after buffer's end */
299 copy_offset = (line * data->width + offset) * data->pgroup / data->xinc;
300 if (copy_offset + length > data->frame_size || !data->frame)
301 return AVERROR_INVALIDDATA;
302
303 dest = data->frame + copy_offset;
304 memcpy(dest, payload, length);
305
306 payload += length;
307 payload_len -= length;
308 } while (cont);
309
310 if ((flags & RTP_FLAG_MARKER)) {
311 return rfc4175_finalize_packet(data, pkt, st->index);
312 } else if (missed_last_packet) {
313 return 0;
314 }
315
316 return AVERROR(EAGAIN);
317 }
318
319 const RTPDynamicProtocolHandler ff_rfc4175_rtp_handler = {
320 .enc_name = "raw",
321 .codec_type = AVMEDIA_TYPE_VIDEO,
322 .codec_id = AV_CODEC_ID_NONE,
323 .priv_data_size = sizeof(PayloadContext),
324 .parse_sdp_a_line = rfc4175_parse_sdp_line,
325 .parse_packet = rfc4175_handle_packet,
326 };
327