]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec_hevc.c
avformat/rawenc: remove singlejpeg muxer
[ffmpeg] / libavformat / rtpdec_hevc.c
1 /*
2  * RTP parser for HEVC/H.265 payload format (draft version 6)
3  * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
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 #include "libavutil/avassert.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/base64.h"
25 #include "libavcodec/get_bits.h"
26
27 #include "avformat.h"
28 #include "internal.h"
29 #include "rtpdec.h"
30 #include "rtpdec_formats.h"
31
32 #define RTP_HEVC_PAYLOAD_HEADER_SIZE       2
33 #define RTP_HEVC_FU_HEADER_SIZE            1
34 #define RTP_HEVC_DONL_FIELD_SIZE           2
35 #define RTP_HEVC_DOND_FIELD_SIZE           1
36 #define RTP_HEVC_AP_NALU_LENGTH_FIELD_SIZE 2
37 #define HEVC_SPECIFIED_NAL_UNIT_TYPES      48
38
39 /* SDP out-of-band signaling data */
40 struct PayloadContext {
41     int using_donl_field;
42     int profile_id;
43     uint8_t *sps, *pps, *vps, *sei;
44     int sps_size, pps_size, vps_size, sei_size;
45 };
46
47 static const uint8_t start_sequence[] = { 0x00, 0x00, 0x00, 0x01 };
48
49 static av_cold int hevc_sdp_parse_fmtp_config(AVFormatContext *s,
50                                               AVStream *stream,
51                                               PayloadContext *hevc_data,
52                                               const char *attr, const char *value)
53 {
54     /* profile-space: 0-3 */
55     /* profile-id: 0-31 */
56     if (!strcmp(attr, "profile-id")) {
57         hevc_data->profile_id = atoi(value);
58         av_log(s, AV_LOG_TRACE, "SDP: found profile-id: %d\n", hevc_data->profile_id);
59     }
60
61     /* tier-flag: 0-1 */
62     /* level-id: 0-255 */
63     /* interop-constraints: [base16] */
64     /* profile-compatibility-indicator: [base16] */
65     /* sprop-sub-layer-id: 0-6, defines highest possible value for TID, default: 6 */
66     /* recv-sub-layer-id: 0-6 */
67     /* max-recv-level-id: 0-255 */
68     /* tx-mode: MSM,SSM */
69     /* sprop-vps: [base64] */
70     /* sprop-sps: [base64] */
71     /* sprop-pps: [base64] */
72     /* sprop-sei: [base64] */
73     if (!strcmp(attr, "sprop-vps") || !strcmp(attr, "sprop-sps") ||
74         !strcmp(attr, "sprop-pps") || !strcmp(attr, "sprop-sei")) {
75         uint8_t **data_ptr = NULL;
76         int *size_ptr = NULL;
77         if (!strcmp(attr, "sprop-vps")) {
78             data_ptr = &hevc_data->vps;
79             size_ptr = &hevc_data->vps_size;
80         } else if (!strcmp(attr, "sprop-sps")) {
81             data_ptr = &hevc_data->sps;
82             size_ptr = &hevc_data->sps_size;
83         } else if (!strcmp(attr, "sprop-pps")) {
84             data_ptr = &hevc_data->pps;
85             size_ptr = &hevc_data->pps_size;
86         } else if (!strcmp(attr, "sprop-sei")) {
87             data_ptr = &hevc_data->sei;
88             size_ptr = &hevc_data->sei_size;
89         } else
90             av_assert0(0);
91
92         ff_h264_parse_sprop_parameter_sets(s, data_ptr,
93                                            size_ptr, value);
94     }
95
96     /* max-lsr, max-lps, max-cpb, max-dpb, max-br, max-tr, max-tc */
97     /* max-fps */
98
99     /* sprop-max-don-diff: 0-32767
100
101          When the RTP stream depends on one or more other RTP
102          streams (in this case tx-mode MUST be equal to "MSM" and
103          MSM is in use), this parameter MUST be present and the
104          value MUST be greater than 0.
105     */
106     if (!strcmp(attr, "sprop-max-don-diff")) {
107         if (atoi(value) > 0)
108             hevc_data->using_donl_field = 1;
109         av_log(s, AV_LOG_TRACE, "Found sprop-max-don-diff in SDP, DON field usage is: %d\n",
110                 hevc_data->using_donl_field);
111     }
112
113     /* sprop-depack-buf-nalus: 0-32767 */
114     if (!strcmp(attr, "sprop-depack-buf-nalus")) {
115         if (atoi(value) > 0)
116             hevc_data->using_donl_field = 1;
117         av_log(s, AV_LOG_TRACE, "Found sprop-depack-buf-nalus in SDP, DON field usage is: %d\n",
118                 hevc_data->using_donl_field);
119     }
120
121     /* sprop-depack-buf-bytes: 0-4294967295 */
122     /* depack-buf-cap */
123     /* sprop-segmentation-id: 0-3 */
124     /* sprop-spatial-segmentation-idc: [base16] */
125     /* dec-parallel-ca: */
126     /* include-dph */
127
128     return 0;
129 }
130
131 static av_cold int hevc_parse_sdp_line(AVFormatContext *ctx, int st_index,
132                                        PayloadContext *hevc_data, const char *line)
133 {
134     AVStream *current_stream;
135     AVCodecParameters *par;
136     const char *sdp_line_ptr = line;
137
138     if (st_index < 0)
139         return 0;
140
141     current_stream = ctx->streams[st_index];
142     par  = current_stream->codecpar;
143
144     if (av_strstart(sdp_line_ptr, "framesize:", &sdp_line_ptr)) {
145         ff_h264_parse_framesize(par, sdp_line_ptr);
146     } else if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
147         int ret = ff_parse_fmtp(ctx, current_stream, hevc_data, sdp_line_ptr,
148                                 hevc_sdp_parse_fmtp_config);
149         if (hevc_data->vps_size || hevc_data->sps_size ||
150             hevc_data->pps_size || hevc_data->sei_size) {
151             par->extradata_size = hevc_data->vps_size + hevc_data->sps_size +
152                                   hevc_data->pps_size + hevc_data->sei_size;
153             if ((ret = ff_alloc_extradata(par, par->extradata_size)) >= 0) {
154                 int pos = 0;
155                 memcpy(par->extradata + pos, hevc_data->vps, hevc_data->vps_size);
156                 pos += hevc_data->vps_size;
157                 memcpy(par->extradata + pos, hevc_data->sps, hevc_data->sps_size);
158                 pos += hevc_data->sps_size;
159                 memcpy(par->extradata + pos, hevc_data->pps, hevc_data->pps_size);
160                 pos += hevc_data->pps_size;
161                 memcpy(par->extradata + pos, hevc_data->sei, hevc_data->sei_size);
162             }
163
164             av_freep(&hevc_data->vps);
165             av_freep(&hevc_data->sps);
166             av_freep(&hevc_data->pps);
167             av_freep(&hevc_data->sei);
168             hevc_data->vps_size = 0;
169             hevc_data->sps_size = 0;
170             hevc_data->pps_size = 0;
171             hevc_data->sei_size = 0;
172         }
173         return ret;
174     }
175
176     return 0;
177 }
178
179 static int hevc_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_hevc_ctx,
180                               AVStream *st, AVPacket *pkt, uint32_t *timestamp,
181                               const uint8_t *buf, int len, uint16_t seq,
182                               int flags)
183 {
184     const uint8_t *rtp_pl = buf;
185     int tid, lid, nal_type;
186     int first_fragment, last_fragment, fu_type;
187     uint8_t new_nal_header[2];
188     int res = 0;
189
190     /* sanity check for size of input packet: 1 byte payload at least */
191     if (len < RTP_HEVC_PAYLOAD_HEADER_SIZE + 1) {
192         av_log(ctx, AV_LOG_ERROR, "Too short RTP/HEVC packet, got %d bytes\n", len);
193         return AVERROR_INVALIDDATA;
194     }
195
196     /*
197      * decode the HEVC payload header according to section 4 of draft version 6:
198      *
199      *    0                   1
200      *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
201      *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
202      *   |F|   Type    |  LayerId  | TID |
203      *   +-------------+-----------------+
204      *
205      *      Forbidden zero (F): 1 bit
206      *      NAL unit type (Type): 6 bits
207      *      NUH layer ID (LayerId): 6 bits
208      *      NUH temporal ID plus 1 (TID): 3 bits
209      */
210     nal_type =  (buf[0] >> 1) & 0x3f;
211     lid  = ((buf[0] << 5) & 0x20) | ((buf[1] >> 3) & 0x1f);
212     tid  =   buf[1] & 0x07;
213
214     /* sanity check for correct layer ID */
215     if (lid) {
216         /* future scalable or 3D video coding extensions */
217         avpriv_report_missing_feature(ctx, "Multi-layer HEVC coding");
218         return AVERROR_PATCHWELCOME;
219     }
220
221     /* sanity check for correct temporal ID */
222     if (!tid) {
223         av_log(ctx, AV_LOG_ERROR, "Illegal temporal ID in RTP/HEVC packet\n");
224         return AVERROR_INVALIDDATA;
225     }
226
227     /* sanity check for correct NAL unit type */
228     if (nal_type > 50) {
229         av_log(ctx, AV_LOG_ERROR, "Unsupported (HEVC) NAL type (%d)\n", nal_type);
230         return AVERROR_INVALIDDATA;
231     }
232
233     switch (nal_type) {
234     /* video parameter set (VPS) */
235     case 32:
236     /* sequence parameter set (SPS) */
237     case 33:
238     /* picture parameter set (PPS) */
239     case 34:
240     /*  supplemental enhancement information (SEI) */
241     case 39:
242     /* single NAL unit packet */
243     default:
244         /* create A/V packet */
245         if ((res = av_new_packet(pkt, sizeof(start_sequence) + len)) < 0)
246             return res;
247         /* A/V packet: copy start sequence */
248         memcpy(pkt->data, start_sequence, sizeof(start_sequence));
249         /* A/V packet: copy NAL unit data */
250         memcpy(pkt->data + sizeof(start_sequence), buf, len);
251
252         break;
253     /* aggregated packet (AP) - with two or more NAL units */
254     case 48:
255         /* pass the HEVC payload header */
256         buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
257         len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
258
259         /* pass the HEVC DONL field */
260         if (rtp_hevc_ctx->using_donl_field) {
261             buf += RTP_HEVC_DONL_FIELD_SIZE;
262             len -= RTP_HEVC_DONL_FIELD_SIZE;
263         }
264
265         res = ff_h264_handle_aggregated_packet(ctx, rtp_hevc_ctx, pkt, buf, len,
266                                                rtp_hevc_ctx->using_donl_field ?
267                                                RTP_HEVC_DOND_FIELD_SIZE : 0,
268                                                NULL, 0);
269         if (res < 0)
270             return res;
271         break;
272     /* fragmentation unit (FU) */
273     case 49:
274         /* pass the HEVC payload header */
275         buf += RTP_HEVC_PAYLOAD_HEADER_SIZE;
276         len -= RTP_HEVC_PAYLOAD_HEADER_SIZE;
277
278         /*
279          *    decode the FU header
280          *
281          *     0 1 2 3 4 5 6 7
282          *    +-+-+-+-+-+-+-+-+
283          *    |S|E|  FuType   |
284          *    +---------------+
285          *
286          *       Start fragment (S): 1 bit
287          *       End fragment (E): 1 bit
288          *       FuType: 6 bits
289          */
290         first_fragment = buf[0] & 0x80;
291         last_fragment  = buf[0] & 0x40;
292         fu_type        = buf[0] & 0x3f;
293
294         /* pass the HEVC FU header */
295         buf += RTP_HEVC_FU_HEADER_SIZE;
296         len -= RTP_HEVC_FU_HEADER_SIZE;
297
298         /* pass the HEVC DONL field */
299         if (rtp_hevc_ctx->using_donl_field) {
300             buf += RTP_HEVC_DONL_FIELD_SIZE;
301             len -= RTP_HEVC_DONL_FIELD_SIZE;
302         }
303
304         av_log(ctx, AV_LOG_TRACE, " FU type %d with %d bytes\n", fu_type, len);
305
306         /* sanity check for size of input packet: 1 byte payload at least */
307         if (len <= 0) {
308             if (len < 0) {
309                 av_log(ctx, AV_LOG_ERROR,
310                        "Too short RTP/HEVC packet, got %d bytes of NAL unit type %d\n",
311                        len, nal_type);
312                 return AVERROR_INVALIDDATA;
313             } else {
314                 return AVERROR(EAGAIN);
315             }
316         }
317
318         if (first_fragment && last_fragment) {
319             av_log(ctx, AV_LOG_ERROR, "Illegal combination of S and E bit in RTP/HEVC packet\n");
320             return AVERROR_INVALIDDATA;
321         }
322
323         new_nal_header[0] = (rtp_pl[0] & 0x81) | (fu_type << 1);
324         new_nal_header[1] = rtp_pl[1];
325
326         res = ff_h264_handle_frag_packet(pkt, buf, len, first_fragment,
327                                          new_nal_header, sizeof(new_nal_header));
328
329         break;
330     /* PACI packet */
331     case 50:
332         /* Temporal scalability control information (TSCI) */
333         avpriv_report_missing_feature(ctx, "PACI packets for RTP/HEVC");
334         res = AVERROR_PATCHWELCOME;
335         break;
336     }
337
338     pkt->stream_index = st->index;
339
340     return res;
341 }
342
343 const RTPDynamicProtocolHandler ff_hevc_dynamic_handler = {
344     .enc_name         = "H265",
345     .codec_type       = AVMEDIA_TYPE_VIDEO,
346     .codec_id         = AV_CODEC_ID_HEVC,
347     .need_parsing     = AVSTREAM_PARSE_FULL,
348     .priv_data_size   = sizeof(PayloadContext),
349     .parse_sdp_a_line = hevc_parse_sdp_line,
350     .parse_packet     = hevc_handle_packet,
351 };