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