FFmpeg
rtpdec_rfc4175.c
Go to the documentation of this file.
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;
33  int depth;
34  int width;
35  int height;
36 
37  uint8_t *frame;
38  unsigned int frame_size;
39  unsigned int pgroup; /* size of the pixel group in bytes */
40  unsigned int xinc;
41 
42  uint32_t timestamp;
43 };
44 
46 {
47  enum AVPixelFormat pixfmt;
48  int tag;
49  const AVPixFmtDescriptor *desc;
50 
51  if (!strncmp(data->sampling, "YCbCr-4:2:2", 11)) {
52  tag = MKTAG('U', 'Y', 'V', 'Y');
53  data->xinc = 2;
54 
55  if (data->depth == 8) {
56  data->pgroup = 4;
59  } else if (data->depth == 10) {
60  data->pgroup = 5;
63  } else {
64  return AVERROR_INVALIDDATA;
65  }
66  } else if (!strncmp(data->sampling, "YCbCr-4:2:0", 11)) {
67  tag = MKTAG('I', '4', '2', '0');
68  data->xinc = 4;
69 
70  if (data->depth == 8) {
71  data->pgroup = 6;
74  } else {
75  return AVERROR_INVALIDDATA;
76  }
77  } else if (!strncmp(data->sampling, "RGB", 3)) {
78  tag = MKTAG('R', 'G', 'B', 24);
79  if (data->depth == 8) {
80  data->xinc = 1;
81  data->pgroup = 3;
84  } else {
85  return AVERROR_INVALIDDATA;
86  }
87  } else if (!strncmp(data->sampling, "BGR", 3)) {
88  tag = MKTAG('B', 'G', 'R', 24);
89  if (data->depth == 8) {
90  data->xinc = 1;
91  data->pgroup = 3;
94  } else {
95  return AVERROR_INVALIDDATA;
96  }
97  } else {
98  return AVERROR_INVALIDDATA;
99  }
100 
102  stream->codecpar->format = pixfmt;
103  stream->codecpar->codec_tag = tag;
105  data->frame_size = data->width * data->height * data->pgroup / data->xinc;
106 
107  if (data->framerate.den > 0) {
108  stream->avg_frame_rate = data->framerate;
109  stream->codecpar->bit_rate = data->frame_size * av_q2d(data->framerate) * 8;
110  }
111 
112  return 0;
113 }
114 
116  PayloadContext *data, const char *attr,
117  const char *value)
118 {
119  if (!strncmp(attr, "width", 5))
120  data->width = atoi(value);
121  else if (!strncmp(attr, "height", 6))
122  data->height = atoi(value);
123  else if (!strncmp(attr, "sampling", 8))
124  data->sampling = av_strdup(value);
125  else if (!strncmp(attr, "depth", 5))
126  data->depth = atoi(value);
127  else if (!strncmp(attr, "exactframerate", 14)) {
128  if (av_parse_video_rate(&data->framerate, value) < 0)
129  return AVERROR(EINVAL);
130  } else if (!strncmp(attr, "TCS", 3)) {
131  if (!strncmp(value, "SDR", 3))
133  else if (!strncmp(value, "PQ", 2))
135  else if (!strncmp(value, "HLG", 3))
137  else if (!strncmp(value, "LINEAR", 6))
139  else if (!strncmp(value, "ST428-1", 7))
141  else
143  } else if (!strncmp(attr, "colorimetry", 11)) {
144  if (!strncmp(value, "BT601", 5)) {
147  } else if (!strncmp(value, "BT709", 5)) {
150  } else if (!strncmp(value, "BT2020", 6)) {
153  }
154  } else if (!strncmp(attr, "RANGE", 5)) {
155  if (!strncmp(value, "NARROW", 6))
157  else if (!strncmp(value, "FULL", 4))
159  }
160 
161  return 0;
162 }
163 
164 static int rfc4175_parse_sdp_line(AVFormatContext *s, int st_index,
165  PayloadContext *data, const char *line)
166 {
167  const char *p;
168 
169  if (st_index < 0)
170  return 0;
171 
172  if (av_strstart(line, "fmtp:", &p)) {
173  AVStream *stream = s->streams[st_index];
174  int ret = ff_parse_fmtp(s, stream, data, p, rfc4175_parse_fmtp);
175 
176  if (ret < 0)
177  return ret;
178 
179 
180  if (!data->sampling || !data->depth || !data->width || !data->height)
181  return AVERROR(EINVAL);
182 
183  stream->codecpar->width = data->width;
184  stream->codecpar->height = data->height;
185 
186  ret = rfc4175_parse_format(stream, data);
187  av_freep(&data->sampling);
188 
189  return ret;
190  }
191 
192  return 0;
193 }
194 
196  int stream_index)
197 {
198  int ret;
199 
200  pkt->stream_index = stream_index;
201  ret = av_packet_from_data(pkt, data->frame, data->frame_size);
202  if (ret < 0) {
203  av_freep(&data->frame);
204  }
205 
206  data->frame = NULL;
207 
208  return ret;
209 }
210 
212  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
213  const uint8_t * buf, int len,
214  uint16_t seq, int flags)
215 {
216  int length, line, offset, cont;
217  const uint8_t *headers = buf + 2; /* skip extended seqnum */
218  const uint8_t *payload = buf + 2;
219  int payload_len = len - 2;
220  int missed_last_packet = 0;
221 
222  uint8_t *dest;
223 
224  if (*timestamp != data->timestamp) {
225  if (data->frame) {
226  /*
227  * if we're here, it means that two RTP packets didn't have the
228  * same timestamp, which is a sign that they were packets from two
229  * different frames, but we didn't get the flag RTP_FLAG_MARKER on
230  * the first one of these frames (last packet of a frame).
231  * Finalize the previous frame anyway by filling the AVPacket.
232  */
233  av_log(ctx, AV_LOG_ERROR, "Missed previous RTP Marker\n");
234  missed_last_packet = 1;
236  }
237 
238  data->frame = av_malloc(data->frame_size);
239 
240  data->timestamp = *timestamp;
241 
242  if (!data->frame) {
243  av_log(ctx, AV_LOG_ERROR, "Out of memory.\n");
244  return AVERROR(ENOMEM);
245  }
246  }
247 
248  /*
249  * looks for the 'Continuation bit' in scan lines' headers
250  * to find where data start
251  */
252  do {
253  if (payload_len < 6)
254  return AVERROR_INVALIDDATA;
255 
256  cont = payload[4] & 0x80;
257  payload += 6;
258  payload_len -= 6;
259  } while (cont);
260 
261  /* and now iterate over every scan lines */
262  do {
263  int copy_offset;
264 
265  if (payload_len < data->pgroup)
266  return AVERROR_INVALIDDATA;
267 
268  length = (headers[0] << 8) | headers[1];
269  line = ((headers[2] & 0x7f) << 8) | headers[3];
270  offset = ((headers[4] & 0x7f) << 8) | headers[5];
271  cont = headers[4] & 0x80;
272  headers += 6;
273 
274  if (!data->pgroup || length % data->pgroup)
275  return AVERROR_INVALIDDATA;
276 
277  if (length > payload_len)
278  length = payload_len;
279 
280  /* prevent ill-formed packets to write after buffer's end */
281  copy_offset = (line * data->width + offset) * data->pgroup / data->xinc;
282  if (copy_offset + length > data->frame_size)
283  return AVERROR_INVALIDDATA;
284 
285  dest = data->frame + copy_offset;
286  memcpy(dest, payload, length);
287 
288  payload += length;
289  payload_len -= length;
290  } while (cont);
291 
292  if ((flags & RTP_FLAG_MARKER)) {
293  return rfc4175_finalize_packet(data, pkt, st->index);
294  } else if (missed_last_packet) {
295  return 0;
296  }
297 
298  return AVERROR(EAGAIN);
299 }
300 
302  .enc_name = "raw",
303  .codec_type = AVMEDIA_TYPE_VIDEO,
304  .codec_id = AV_CODEC_ID_NONE,
305  .priv_data_size = sizeof(PayloadContext),
306  .parse_sdp_a_line = rfc4175_parse_sdp_line,
308 };
rfc4175_finalize_packet
static int rfc4175_finalize_packet(PayloadContext *data, AVPacket *pkt, int stream_index)
Definition: rtpdec_rfc4175.c:195
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
PayloadContext::frame
uint8_t * frame
Definition: rtpdec_rfc4175.c:37
rfc4175_handle_packet
static int 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)
Definition: rtpdec_rfc4175.c:211
AVCodecParameters::color_space
enum AVColorSpace color_space
Definition: codec_par.h:149
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2660
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:503
rtpdec_formats.h
ff_parse_fmtp
int ff_parse_fmtp(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *p, int(*parse_fmtp)(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *attr, const char *value))
Definition: rtpdec.c:959
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:63
pixdesc.h
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:597
RTP_FLAG_MARKER
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:94
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:497
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:1015
data
const char data[16]
Definition: mxf.c:143
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
av_get_bits_per_pixel
int av_get_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel used by the pixel format described by pixdesc.
Definition: pixdesc.c:2612
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
PayloadContext::timestamp
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
RTPDynamicProtocolHandler::enc_name
const char * enc_name
Definition: rtpdec.h:117
AVCodecParameters::color_primaries
enum AVColorPrimaries color_primaries
Definition: codec_par.h:147
AVCOL_SPC_BT470BG
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:529
rfc4175_parse_fmtp
static int rfc4175_parse_fmtp(AVFormatContext *s, AVStream *stream, PayloadContext *data, const char *attr, const char *value)
Definition: rtpdec_rfc4175.c:115
AVCOL_TRC_SMPTEST428_1
@ AVCOL_TRC_SMPTEST428_1
Definition: pixfmt.h:514
AVCodecParameters::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: codec_par.h:148
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
ff_rfc4175_rtp_handler
const RTPDynamicProtocolHandler ff_rfc4175_rtp_handler
Definition: rtpdec_rfc4175.c:301
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
AVCOL_PRI_BT470BG
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:476
if
if(ret)
Definition: filter_design.txt:179
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:471
parseutils.h
PayloadContext::sampling
char * sampling
Definition: rtpdec_rfc4175.c:31
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:405
PayloadContext::framerate
AVRational framerate
Definition: rtpdec_rfc4175.c:32
AVCOL_PRI_BT2020
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition: pixfmt.h:480
PayloadContext::width
int width
Definition: rtpdec_rfc4175.c:34
AVCOL_TRC_SMPTE2084
@ AVCOL_TRC_SMPTE2084
SMPTE ST 2084 for 10-, 12-, 14- and 16-bit systems.
Definition: pixfmt.h:511
av_packet_from_data
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
Definition: avpacket.c:173
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
PayloadContext::xinc
unsigned int xinc
Definition: rtpdec_rfc4175.c:40
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
line
Definition: graph2dot.c:48
av_strstart
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:34
AV_CODEC_ID_BITPACKED
@ AV_CODEC_ID_BITPACKED
Definition: codec_id.h:280
AVCOL_TRC_BT709
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition: pixfmt.h:496
av_parse_video_rate
int av_parse_video_rate(AVRational *rate, const char *arg)
Parse str and store the detected values in *rate.
Definition: parseutils.c:181
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
avio_internal.h
AVCOL_SPC_BT2020_NCL
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:534
PayloadContext::height
int height
Definition: rtpdec_rfc4175.c:35
AVCodecParameters::height
int height
Definition: codec_par.h:127
rfc4175_parse_format
static int rfc4175_parse_format(AVStream *stream, PayloadContext *data)
Definition: rtpdec_rfc4175.c:45
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AVCodecParameters::color_range
enum AVColorRange color_range
Video only.
Definition: codec_par.h:146
len
int len
Definition: vorbis_enc_data.h:426
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:580
tag
uint32_t tag
Definition: movenc.c:1596
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:935
pixfmt
enum AVPixelFormat pixfmt
Definition: kmsgrab.c:365
AV_PIX_FMT_UYVY422
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:81
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:943
AVCOL_TRC_ARIB_STD_B67
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
Definition: pixfmt.h:515
headers
FFmpeg currently uses a custom build this text attempts to document some of its obscure features and options Makefile the full command issued by make and its output will be shown on the screen DBG Preprocess x86 external assembler files to a dbg asm file in the object which then gets compiled Helps in developing those assembler files DESTDIR Destination directory for the install useful to prepare packages or install FFmpeg in cross environments GEN Set to ‘1’ to generate the missing or mismatched references Makefile builds all the libraries and the executables fate Run the fate test note that you must have installed it fate list List all fate regression test targets install Install headers
Definition: build_system.txt:34
AVPacket::stream_index
int stream_index
Definition: packet.h:375
parse_packet
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index, int flush)
Parse a packet, add all split parts to parse_queue.
Definition: demux.c:1105
PayloadContext::depth
int depth
Definition: rtpdec_rfc4175.c:33
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:279
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
PayloadContext::frame_size
unsigned int frame_size
Definition: rtpdec_rfc4175.c:38
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
PayloadContext::pgroup
unsigned int pgroup
Definition: rtpdec_rfc4175.c:39
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
rfc4175_parse_sdp_line
static int rfc4175_parse_sdp_line(AVFormatContext *s, int st_index, PayloadContext *data, const char *line)
Definition: rtpdec_rfc4175.c:164
avstring.h
PayloadContext
RTP/JPEG specific private data.
Definition: rdt.c:83
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition: pixfmt.h:525
line
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:40
RTPDynamicProtocolHandler
Definition: rtpdec.h:116