FFmpeg
Loading...
Searching...
No Matches
rtpenc_rfc4175.c
Go to the documentation of this file.
1/*
2 * RTP Packetization of RAW video (RFC4175)
3 * Copyright (c) 2021 Limin Wang
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#include "avformat.h"
22#include "rtpenc.h"
23
24void ff_rtp_send_raw_rfc4175(AVFormatContext *s1, const uint8_t *buf, int size, int interlaced, int field)
25{
27 int width = s1->streams[0]->codecpar->width;
28 int height = s1->streams[0]->codecpar->height;
29 int xinc, yinc, pgroup;
30 int i = field;
31 int offset = 0;
32
33 s->timestamp = s->cur_timestamp;
34 switch (s1->streams[0]->codecpar->format) {
36 xinc = 2;
37 yinc = 1 << interlaced;
38 pgroup = 4;
39 break;
41 xinc = 2;
42 yinc = 1 << interlaced;
43 pgroup = 5;
44 break;
46 xinc = 4;
47 yinc = 1 << interlaced;
48 pgroup = 6;
49 break;
51 xinc = 1;
52 yinc = 1 << interlaced;
53 pgroup = 3;
54 break;
56 xinc = 1;
57 yinc = 1 << interlaced;
58 pgroup = 3;
59 break;
60 default:
61 return;
62 }
63
64 if (s->max_payload_size < 2 + 6 + pgroup) {
65 av_log(s1, AV_LOG_ERROR, "Max payload size %d too low\n", s->max_payload_size);
66 return;
67 }
68
69 while (i < height) {
70 int left = s->max_payload_size;
71 uint8_t *dest = s->buf;
72 uint8_t *headers;
73 const int head_size = 6;
74 int next_line;
75 int length, cont, pixels;
76
77 /* Extended Sequence Number */
78 *dest++ = 0;
79 *dest++ = 0;
80 left -= 2;
81
82 headers = dest;
83 do {
84 int l_line;
85
86 pixels = width - offset;
87 length = (pixels * pgroup) / xinc;
88
89 left -= head_size;
90 if (left >= length && length <= 0xFFFF) {
91 next_line = 1;
92 } else {
93 pixels = (FFMIN(left, 0xFFFF) / pgroup) * xinc;
94 length = (pixels * pgroup) / xinc;
95 next_line = 0;
96 }
97 left -= length;
98
99 /* Length */
100 *dest++ = (length >> 8) & 0xff;
101 *dest++ = length & 0xff;
102
103 /* Line No */
104 l_line = i >> interlaced;
105 *dest++ = ((l_line >> 8) & 0x7f) | ((field << 7) & 0x80);
106 *dest++ = l_line & 0xff;
107 if (next_line) i += yinc;
108
109 cont = (left > (head_size + pgroup) && i < height) ? 0x80 : 0x00;
110 /* Offset and Continuation marker */
111 *dest++ = ((offset >> 8) & 0x7f) | cont;
112 *dest++ = offset & 0xff;
113
114 if (next_line)
115 offset = 0;
116 else
117 offset += pixels;
118 } while (cont);
119
120 do {
121 int l_field;
122 int l_line;
123 int l_off;
124 int64_t copy_offset;
125
126 length = (headers[0] << 8) | headers[1];
127 l_field = (headers[2] & 0x80) >> 7;
128 l_line = ((headers[2] & 0x7f) << 8) | headers[3];
129 l_off = ((headers[4] & 0x7f) << 8) | headers[5];
130 cont = headers[4] & 0x80;
131 headers += head_size;
132
133 if (interlaced)
134 l_line = 2 * l_line + l_field;
135 copy_offset = (l_line * (int64_t)width + l_off) * pgroup / xinc;
136 if (copy_offset + length > size)
137 return;
138 memcpy (dest, buf + copy_offset, length);
139 dest += length;
140 } while (cont);
141
142 ff_rtp_send_data (s1, s->buf, s->max_payload_size - left, i >= height);
143 }
144}
Main libavformat public API header.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
long long int64_t
Definition coverity.c:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
unsigned offset
Definition libaomenc.c:763
#define FFMIN(a, b)
Definition macros.h:49
uint8_t interlaced
Definition mxfenc.c:2336
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:549
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition pixfmt.h:88
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
void ff_rtp_send_data(AVFormatContext *s1, const uint8_t *buf1, int len, int m)
Definition rtpenc.c:370
void ff_rtp_send_raw_rfc4175(AVFormatContext *s1, const uint8_t *buf, int size, int interlaced, int field)
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int width
The width of the video frame in pixels.
Definition codec_par.h:143
Format I/O context.
Definition avformat.h:1400
void * priv_data
Format private data.
Definition avformat.h:1428
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1468
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:791
#define av_log(a,...)
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
int size