1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*
* RTP parser for VC-2 HQ payload format (draft version 1) - experimental
* Copyright (c) 2016 Thomas Volkert <thomas@netzeal.de>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/intreadwrite.h"
#include "libavcodec/dirac.h"
#include "avio_internal.h"
#include "rtpdec_formats.h"
#define RTP_VC2HQ_PL_HEADER_SIZE 4
#define DIRAC_DATA_UNIT_HEADER_SIZE 13
#define DIRAC_PIC_NR_SIZE 4
#define DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT 0xEC
struct PayloadContext {
AVIOContext *buf;
uint32_t frame_size;
uint32_t frame_nr;
uint32_t timestamp;
uint32_t last_unit_size;
int seen_sequence_header;
};
static const uint8_t start_sequence[] = { 'B', 'B', 'C', 'D' };
static void fill_parse_info_header(PayloadContext *pl_ctx, uint8_t *buf,
uint8_t parse_code, uint32_t data_unit_size)
{
memcpy(buf, start_sequence, sizeof(start_sequence));
buf[4] = parse_code;
AV_WB32(&buf[5], data_unit_size);
AV_WB32(&buf[9], pl_ctx->last_unit_size);
pl_ctx->last_unit_size = data_unit_size;
}
static int vc2hq_handle_sequence_header(PayloadContext *pl_ctx, AVStream *st, AVPacket *pkt,
const uint8_t *buf, int len)
{
int res;
uint32_t size = DIRAC_DATA_UNIT_HEADER_SIZE + len;
if ((res = av_new_packet(pkt, DIRAC_DATA_UNIT_HEADER_SIZE + len)) < 0)
return res;
fill_parse_info_header(pl_ctx, pkt->data, 0x00, size);
/* payload of seq. header */
memcpy(pkt->data + DIRAC_DATA_UNIT_HEADER_SIZE, buf, len);
pkt->stream_index = st->index;
pl_ctx->seen_sequence_header = 1;
return 0;
}
static int vc2hq_mark_end_of_sequence(PayloadContext *pl_ctx, AVStream *st, AVPacket *pkt)
{
int res;
uint32_t size = 0;
/* create A/V packet */
if ((res = av_new_packet(pkt, DIRAC_DATA_UNIT_HEADER_SIZE)) < 0)
return res;
fill_parse_info_header(pl_ctx, pkt->data, 0x10, size);
pkt->stream_index = st->index;
pl_ctx->seen_sequence_header = 0;
return 0;
}
static int vc2hq_handle_frame_fragment(AVFormatContext *ctx, PayloadContext *pl_ctx, AVStream *st,
AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len,
int flags)
{
int res;
uint32_t pic_nr;
uint16_t frag_len;
uint16_t no_slices;
/* sanity check for size of input packet: 16 bytes header in any case as minimum */
if (len < 16) {
av_log(ctx, AV_LOG_ERROR, "Too short RTP/VC2hq packet, got %d bytes\n", len);
return AVERROR_INVALIDDATA;
}
pic_nr = AV_RB32(&buf[4]);
frag_len = AV_RB16(&buf[12]);
no_slices = AV_RB16(&buf[14]);
if (pl_ctx->buf && pl_ctx->frame_nr != pic_nr) {
av_log(ctx, AV_LOG_WARNING, "Dropping buffered RTP/VC2hq packet fragments - non-continuous picture numbers\n");
ffio_free_dyn_buf(&pl_ctx->buf);
}
/* transform parameters? */
if (no_slices == 0) {
if (len < frag_len + 16) {
av_log(ctx, AV_LOG_ERROR, "Too short RTP/VC2hq packet, got %d bytes\n", len);
return AVERROR_INVALIDDATA;
}
/* start frame buffering with new dynamic buffer */
if (!pl_ctx->buf) {
res = avio_open_dyn_buf(&pl_ctx->buf);
if (res < 0)
return res;
/* reserve memory for frame header */
res = avio_seek(pl_ctx->buf, DIRAC_DATA_UNIT_HEADER_SIZE + DIRAC_PIC_NR_SIZE, SEEK_SET);
if (res < 0)
return res;
pl_ctx->frame_nr = pic_nr;
pl_ctx->timestamp = *timestamp;
pl_ctx->frame_size = DIRAC_DATA_UNIT_HEADER_SIZE + DIRAC_PIC_NR_SIZE;
}
avio_write(pl_ctx->buf, buf + 16 /* skip pl header */, frag_len);
pl_ctx->frame_size += frag_len;
return AVERROR(EAGAIN);
} else {
if (len < frag_len + 20) {
av_log(ctx, AV_LOG_ERROR, "Too short RTP/VC2hq packet, got %d bytes\n", len);
return AVERROR_INVALIDDATA;
}
/* transform parameters were missed, no buffer available */
if (!pl_ctx->buf)
return AVERROR_INVALIDDATA;
avio_write(pl_ctx->buf, buf + 20 /* skip pl header */, frag_len);
pl_ctx->frame_size += frag_len;
/* RTP marker bit means: last fragment of current frame was received;
otherwise, an additional fragment is needed for the current frame */
if (!(flags & RTP_FLAG_MARKER))
return AVERROR(EAGAIN);
}
/* close frame buffering and create A/V packet */
res = ff_rtp_finalize_packet(pkt, &pl_ctx->buf, st->index);
if (res < 0)
return res;
fill_parse_info_header(pl_ctx, pkt->data, DIRAC_PCODE_PICTURE_HQ, pl_ctx->frame_size);
AV_WB32(&pkt->data[13], pl_ctx->frame_nr);
pl_ctx->frame_size = 0;
return 0;
}
static int vc2hq_handle_packet(AVFormatContext *ctx, PayloadContext *pl_ctx,
AVStream *st, AVPacket *pkt, uint32_t *timestamp,
const uint8_t *buf, int len, uint16_t seq,
int flags)
{
uint8_t parse_code = 0;
int res = 0;
if (pl_ctx->buf && pl_ctx->timestamp != *timestamp) {
av_log(ctx, AV_LOG_WARNING, "Dropping buffered RTP/VC2hq packet fragments - non-continuous timestamps\n");
ffio_free_dyn_buf(&pl_ctx->buf);
pl_ctx->frame_size = 0;
}
/* sanity check for size of input packet: needed header data as minimum */
if (len < RTP_VC2HQ_PL_HEADER_SIZE) {
av_log(ctx, AV_LOG_ERROR, "Too short RTP/VC2hq packet, got %d bytes\n", len);
return AVERROR_INVALIDDATA;
}
parse_code = buf[3];
/* wait for next sequence header? */
if (pl_ctx->seen_sequence_header || parse_code == DIRAC_PCODE_SEQ_HEADER) {
switch(parse_code) {
/* sequence header */
case DIRAC_PCODE_SEQ_HEADER:
res = vc2hq_handle_sequence_header(pl_ctx, st, pkt, buf + RTP_VC2HQ_PL_HEADER_SIZE, len - RTP_VC2HQ_PL_HEADER_SIZE);
break;
/* end of sequence */
case DIRAC_PCODE_END_SEQ:
res = vc2hq_mark_end_of_sequence(pl_ctx, st, pkt);
break;
/* HQ picture fragment */
case DIRAC_RTP_PCODE_HQ_PIC_FRAGMENT:
res = vc2hq_handle_frame_fragment(ctx, pl_ctx, st, pkt, timestamp, buf, len, flags);
break;
}
}
return res;
}
RTPDynamicProtocolHandler ff_vc2hq_dynamic_handler = {
.enc_name = "VC2",
.codec_type = AVMEDIA_TYPE_VIDEO,
.codec_id = AV_CODEC_ID_DIRAC,
.priv_data_size = sizeof(PayloadContext),
.parse_packet = vc2hq_handle_packet
};
|