OSDN Git Service

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