2 * Pro-MPEG Code of Practice #3 Release 2 FEC
3 * Copyright (c) 2016 Mobibase, France (http://www.mobibase.com)
5 * This file is part of FFmpeg.
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.
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.
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
24 * Pro-MPEG Code of Practice #3 Release 2 FEC protocol
25 * @author Vlad Tarca <vlad.tarca@gmail.com>
31 [RFC 2733] FEC Packet Structure
33 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
47 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48 |V=2|P|X| CC |M| PT | sequence number |
49 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52 | synchronization source (SSRC) identifier |
53 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
54 | contributing source (CSRC) identifiers |
56 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 [RFC 3550] RTP header extension (after CSRC)
61 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
62 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 | defined by profile | length |
64 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 [Pro-MPEG COP3] FEC Header
71 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72 | SNBase low bits | length recovery |
73 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
74 |E| PT recovery | mask |
75 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
77 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
78 |X|D|type |index| offset | NA |SNBase ext bits|
79 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83 #include "libavutil/intreadwrite.h"
84 #include "libavutil/mem.h"
85 #include "libavutil/opt.h"
86 #include "libavutil/random_seed.h"
91 #define PROMPEG_RTP_PT 0x60
92 #define PROMPEG_FEC_COL 0x0
93 #define PROMPEG_FEC_ROW 0x1
95 typedef struct PrompegFec
{
101 typedef struct PrompegContext
{
102 const AVClass
*class;
103 URLContext
*fec_col_hd
, *fec_row_hd
;
104 PrompegFec
**fec_arr
, **fec_col_tmp
, **fec_col
, *fec_row
;
108 uint16_t rtp_col_sn
, rtp_row_sn
;
109 uint16_t length_recovery
;
111 int packet_idx
, packet_idx_max
;
119 #define OFFSET(x) offsetof(PrompegContext, x)
120 #define E AV_OPT_FLAG_ENCODING_PARAM
122 static const AVOption options
[] = {
123 { "ttl", "Time to live (in milliseconds, multicast only)", OFFSET(ttl
), AV_OPT_TYPE_INT
, { .i64
= -1 }, -1, INT_MAX
, .flags
= E
},
124 { "l", "FEC L", OFFSET(l
), AV_OPT_TYPE_INT
, { .i64
= 5 }, 4, 20, .flags
= E
},
125 { "d", "FEC D", OFFSET(d
), AV_OPT_TYPE_INT
, { .i64
= 5 }, 4, 20, .flags
= E
},
129 static const AVClass prompeg_class
= {
130 .class_name
= "prompeg",
131 .item_name
= av_default_item_name
,
133 .version
= LIBAVUTIL_VERSION_INT
,
136 static void xor_fast(const uint8_t *in1
, const uint8_t *in2
, uint8_t *out
, int size
) {
142 n
= size
/ sizeof (uint64_t);
143 s
= n
* sizeof (uint64_t);
145 for (i
= 0; i
< n
; i
++) {
148 AV_WN64A(out
, v1
^ v2
);
156 n
= size
/ sizeof (uint32_t);
157 s
= n
* sizeof (uint32_t);
159 for (i
= 0; i
< n
; i
++) {
162 AV_WN32A(out
, v1
^ v2
);
171 for (i
= 0; i
< n
; i
++) {
172 out
[i
] = in1
[i
] ^ in2
[i
];
176 static int prompeg_create_bitstring(URLContext
*h
, const uint8_t *buf
, int size
,
177 uint8_t **bitstring
) {
178 PrompegContext
*s
= h
->priv_data
;
181 if (size
< 12 || (buf
[0] & 0xc0) != 0x80 || (buf
[1] & 0x7f) != 0x21) {
182 av_log(h
, AV_LOG_ERROR
, "Unsupported stream format (expected MPEG-TS over RTP)\n");
183 return AVERROR(EINVAL
);
185 if (size
!= s
->packet_size
) {
186 av_log(h
, AV_LOG_ERROR
, "The RTP packet size must be constant (set pkt_size)\n");
187 return AVERROR(EINVAL
);
190 *bitstring
= av_malloc(s
->bitstring_size
);
192 av_log(h
, AV_LOG_ERROR
, "Failed to allocate the bitstring buffer\n");
193 return AVERROR(ENOMEM
);
198 b
[0] = buf
[0] & 0x3f;
207 * length_recovery: the unsigned network-ordered sum of lengths of CSRC,
208 * padding, extension and media payload
210 AV_WB16(b
+ 6, s
->length_recovery
);
212 memcpy(b
+ 8, buf
+ 12, s
->length_recovery
);
217 static int prompeg_write_fec(URLContext
*h
, PrompegFec
*fec
, uint8_t type
) {
218 PrompegContext
*s
= h
->priv_data
;
220 uint8_t *buf
= s
->rtp_buf
; // zero-filled
221 uint8_t *b
= fec
->bitstring
;
225 sn
= type
== PROMPEG_FEC_COL
? ++s
->rtp_col_sn
: ++s
->rtp_row_sn
;
228 buf
[0] = 0x80 | (b
[0] & 0x3f);
230 buf
[1] = (b
[1] & 0x80) | PROMPEG_RTP_PT
;
232 AV_WB16(buf
+ 2, sn
);
234 AV_WB32(buf
+ 4, fec
->ts
);
236 //AV_WB32(buf + 8, 0);
238 AV_WB16(buf
+ 12, fec
->sn
);
243 buf
[16] = 0x80 | b
[1];
253 // X=0, D, type=0, index=0
254 buf
[24] = type
== PROMPEG_FEC_COL
? 0x0 : 0x40;
256 buf
[25] = type
== PROMPEG_FEC_COL
? s
->l
: 0x1;
258 buf
[26] = type
== PROMPEG_FEC_COL
? s
->d
: s
->l
;
262 memcpy(buf
+ 28, b
+ 8, s
->length_recovery
);
264 hd
= type
== PROMPEG_FEC_COL
? s
->fec_col_hd
: s
->fec_row_hd
;
265 ret
= ffurl_write(hd
, buf
, s
->rtp_buf_size
);
269 static int prompeg_open(URLContext
*h
, const char *uri
, int flags
) {
270 PrompegContext
*s
= h
->priv_data
;
271 AVDictionary
*udp_opts
= NULL
;
276 s
->fec_col_hd
= NULL
;
277 s
->fec_row_hd
= NULL
;
279 if (s
->l
* s
->d
> 100) {
280 av_log(h
, AV_LOG_ERROR
, "L * D must be <= 100\n");
281 return AVERROR(EINVAL
);
284 av_url_split(NULL
, 0, NULL
, 0, hostname
, sizeof (hostname
), &rtp_port
,
287 if (rtp_port
< 1 || rtp_port
> UINT16_MAX
- 4) {
288 av_log(h
, AV_LOG_ERROR
, "Invalid RTP base port %d\n", rtp_port
);
289 return AVERROR(EINVAL
);
293 av_dict_set_int(&udp_opts
, "ttl", s
->ttl
, 0);
296 ff_url_join(buf
, sizeof (buf
), "udp", NULL
, hostname
, rtp_port
+ 2, NULL
);
297 if (ffurl_open_whitelist(&s
->fec_col_hd
, buf
, flags
, &h
->interrupt_callback
,
298 &udp_opts
, h
->protocol_whitelist
, h
->protocol_blacklist
, h
) < 0)
300 ff_url_join(buf
, sizeof (buf
), "udp", NULL
, hostname
, rtp_port
+ 4, NULL
);
301 if (ffurl_open_whitelist(&s
->fec_row_hd
, buf
, flags
, &h
->interrupt_callback
,
302 &udp_opts
, h
->protocol_whitelist
, h
->protocol_blacklist
, h
) < 0)
305 h
->max_packet_size
= s
->fec_col_hd
->max_packet_size
;
308 av_dict_free(&udp_opts
);
309 av_log(h
, AV_LOG_INFO
, "ProMPEG CoP#3-R2 FEC L=%d D=%d\n", s
->l
, s
->d
);
313 ffurl_closep(&s
->fec_col_hd
);
314 ffurl_closep(&s
->fec_row_hd
);
315 av_dict_free(&udp_opts
);
319 static int prompeg_init(URLContext
*h
, const uint8_t *buf
, int size
) {
320 PrompegContext
*s
= h
->priv_data
;
327 if (size
< 12 || size
> UINT16_MAX
+ 12) {
328 av_log(h
, AV_LOG_ERROR
, "Invalid RTP packet size\n");
329 return AVERROR_INVALIDDATA
;
333 s
->packet_idx_max
= s
->l
* s
->d
;
334 s
->packet_size
= size
;
335 s
->length_recovery
= size
- 12;
336 s
->rtp_buf_size
= 28 + s
->length_recovery
; // 12 + 16: RTP + FEC headers
337 s
->bitstring_size
= 8 + s
->length_recovery
; // 8: P, X, CC, M, PT, SN, TS
338 s
->fec_arr_len
= 1 + 2 * s
->l
; // row + column tmp + column out
340 if (h
->flags
& AVFMT_FLAG_BITEXACT
) {
344 seed
= av_get_random_seed();
345 s
->rtp_col_sn
= seed
& 0x0fff;
346 s
->rtp_row_sn
= (seed
>> 16) & 0x0fff;
349 s
->fec_arr
= av_malloc_array(s
->fec_arr_len
, sizeof (PrompegFec
*));
353 for (i
= 0; i
< s
->fec_arr_len
; i
++) {
354 s
->fec_arr
[i
] = av_malloc(sizeof (PrompegFec
));
355 if (!s
->fec_arr
[i
]) {
358 s
->fec_arr
[i
]->bitstring
= av_malloc_array(s
->bitstring_size
, sizeof (uint8_t));
359 if (!s
->fec_arr
[i
]->bitstring
) {
360 av_freep(&s
->fec_arr
[i
]);
364 s
->fec_row
= *s
->fec_arr
;
365 s
->fec_col
= s
->fec_arr
+ 1;
366 s
->fec_col_tmp
= s
->fec_arr
+ 1 + s
->l
;
368 s
->rtp_buf
= av_malloc_array(s
->rtp_buf_size
, sizeof (uint8_t));
372 memset(s
->rtp_buf
, 0, s
->rtp_buf_size
);
380 av_log(h
, AV_LOG_ERROR
, "Failed to allocate the FEC buffer\n");
381 return AVERROR(ENOMEM
);
384 static int prompeg_write(URLContext
*h
, const uint8_t *buf
, int size
) {
385 PrompegContext
*s
= h
->priv_data
;
387 uint8_t *bitstring
= NULL
;
388 int col_idx
, col_out_idx
, row_idx
;
391 if (s
->init
&& ((ret
= prompeg_init(h
, buf
, size
)) < 0))
394 if ((ret
= prompeg_create_bitstring(h
, buf
, size
, &bitstring
)) < 0)
397 col_idx
= s
->packet_idx
% s
->l
;
398 row_idx
= s
->packet_idx
/ s
->l
% s
->d
;
400 // FEC' (row) send block-aligned, xor
402 if (!s
->first
|| s
->packet_idx
> 0) {
403 if ((ret
= prompeg_write_fec(h
, s
->fec_row
, PROMPEG_FEC_ROW
)) < 0)
406 memcpy(s
->fec_row
->bitstring
, bitstring
, s
->bitstring_size
);
407 s
->fec_row
->sn
= AV_RB16(buf
+ 2);
408 s
->fec_row
->ts
= AV_RB32(buf
+ 4);
410 xor_fast(s
->fec_row
->bitstring
, bitstring
, s
->fec_row
->bitstring
,
417 // swap fec_col and fec_col_tmp
418 fec_tmp
= s
->fec_col
[col_idx
];
419 s
->fec_col
[col_idx
] = s
->fec_col_tmp
[col_idx
];
420 s
->fec_col_tmp
[col_idx
] = fec_tmp
;
422 memcpy(s
->fec_col_tmp
[col_idx
]->bitstring
, bitstring
, s
->bitstring_size
);
423 s
->fec_col_tmp
[col_idx
]->sn
= AV_RB16(buf
+ 2);
424 s
->fec_col_tmp
[col_idx
]->ts
= AV_RB32(buf
+ 4);
426 xor_fast(s
->fec_col_tmp
[col_idx
]->bitstring
, bitstring
,
427 s
->fec_col_tmp
[col_idx
]->bitstring
, s
->bitstring_size
);
430 // FEC (column) send block-aligned
431 if (!s
->first
&& s
->packet_idx
% s
->d
== 0) {
432 col_out_idx
= s
->packet_idx
/ s
->d
;
433 if ((ret
= prompeg_write_fec(h
, s
->fec_col
[col_out_idx
], PROMPEG_FEC_COL
)) < 0)
437 if (++s
->packet_idx
>= s
->packet_idx_max
) {
450 static int prompeg_close(URLContext
*h
) {
451 PrompegContext
*s
= h
->priv_data
;
454 ffurl_closep(&s
->fec_col_hd
);
455 ffurl_closep(&s
->fec_row_hd
);
458 for (i
= 0; i
< s
->fec_arr_len
; i
++) {
459 av_free(s
->fec_arr
[i
]->bitstring
);
460 av_freep(&s
->fec_arr
[i
]);
462 av_freep(&s
->fec_arr
);
464 av_freep(&s
->rtp_buf
);
469 const URLProtocol ff_prompeg_protocol
= {
471 .url_open
= prompeg_open
,
472 .url_write
= prompeg_write
,
473 .url_close
= prompeg_close
,
474 .priv_data_size
= sizeof(PrompegContext
),
475 .flags
= URL_PROTOCOL_FLAG_NETWORK
,
476 .priv_data_class
= &prompeg_class
,