Browse code

rtpenc: HEVC/H.265 support

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Thomas Volkert authored on 2014/09/21 19:10:42
Showing 6 changed files
... ...
@@ -2,6 +2,7 @@ Entries are sorted chronologically from oldest to youngest within each release,
2 2
 releases are sorted from youngest to oldest.
3 3
 
4 4
 version <next>:
5
+- HEVC/H.265 RTP payload format (draft v6) packetizer
5 6
 
6 7
 version 2.4:
7 8
 - Icecast protocol
... ...
@@ -367,6 +367,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
367 367
                                             rtpenc_h261.o    \
368 368
                                             rtpenc_h263.o    \
369 369
                                             rtpenc_h263_rfc2190.o \
370
+                                            rtpenc_hevc.o    \
370 371
                                             rtpenc_jpeg.o \
371 372
                                             rtpenc_mpv.o     \
372 373
                                             rtpenc.o      \
... ...
@@ -53,6 +53,7 @@ static int is_supported(enum AVCodecID id)
53 53
     case AV_CODEC_ID_H263:
54 54
     case AV_CODEC_ID_H263P:
55 55
     case AV_CODEC_ID_H264:
56
+    case AV_CODEC_ID_HEVC:
56 57
     case AV_CODEC_ID_MPEG1VIDEO:
57 58
     case AV_CODEC_ID_MPEG2VIDEO:
58 59
     case AV_CODEC_ID_MPEG4:
... ...
@@ -200,6 +201,13 @@ static int rtp_write_header(AVFormatContext *s1)
200 200
             s->nal_length_size = (st->codec->extradata[4] & 0x03) + 1;
201 201
         }
202 202
         break;
203
+    case AV_CODEC_ID_HEVC:
204
+        if (st->codec->extradata_size > 21 &&
205
+            (st->codec->extradata[0] || st->codec->extradata[1] ||
206
+             st->codec->extradata[2] > 1)) {
207
+            s->nal_length_size = (st->codec->extradata[21] & 0x03) + 1;
208
+        }
209
+        break;
203 210
     case AV_CODEC_ID_VORBIS:
204 211
     case AV_CODEC_ID_THEORA:
205 212
         if (!s->max_frames_per_packet) s->max_frames_per_packet = 15;
... ...
@@ -577,6 +585,9 @@ static int rtp_write_packet(AVFormatContext *s1, AVPacket *pkt)
577 577
     case AV_CODEC_ID_H263P:
578 578
         ff_rtp_send_h263(s1, pkt->data, size);
579 579
         break;
580
+    case AV_CODEC_ID_HEVC:
581
+        ff_rtp_send_hevc(s1, pkt->data, size);
582
+        break;
580 583
     case AV_CODEC_ID_VORBIS:
581 584
     case AV_CODEC_ID_THEORA:
582 585
         ff_rtp_send_xiph(s1, pkt->data, size);
... ...
@@ -85,6 +85,7 @@ void ff_rtp_send_h261(AVFormatContext *s1, const uint8_t *buf1, int size);
85 85
 void ff_rtp_send_h263(AVFormatContext *s1, const uint8_t *buf1, int size);
86 86
 void ff_rtp_send_h263_rfc2190(AVFormatContext *s1, const uint8_t *buf1, int size,
87 87
                               const uint8_t *mb_info, int mb_info_size);
88
+void ff_rtp_send_hevc(AVFormatContext *s1, const uint8_t *buf1, int size);
88 89
 void ff_rtp_send_aac(AVFormatContext *s1, const uint8_t *buff, int size);
89 90
 void ff_rtp_send_latm(AVFormatContext *s1, const uint8_t *buff, int size);
90 91
 void ff_rtp_send_amr(AVFormatContext *s1, const uint8_t *buff, int size);
91 92
new file mode 100644
... ...
@@ -0,0 +1,146 @@
0
+/*
1
+ * RTP packetizer for HEVC/H.265 payload format (draft version 6)
2
+ * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "avformat.h"
22
+#include "avc.h"
23
+#include "rtpenc.h"
24
+
25
+#define RTP_HEVC_HEADERS_SIZE 3
26
+
27
+static const uint8_t *avc_mp4_find_startcode(const uint8_t *start, const uint8_t *end, int nal_length_size)
28
+{
29
+    unsigned int res = 0;
30
+
31
+    /* is the given data big enough for 1 NAL unit? */
32
+    if (end - start < nal_length_size)
33
+        return NULL;
34
+
35
+    while (nal_length_size--)
36
+        res = (res << 8) | *start++;
37
+
38
+    if (res > end - start)
39
+        return NULL;
40
+
41
+    return start + res;
42
+}
43
+
44
+static void nal_send(AVFormatContext *ctx, const uint8_t *buf, int len, int last_packet_of_frame)
45
+{
46
+    RTPMuxContext *rtp_ctx = ctx->priv_data;
47
+    int rtp_payload_size   = rtp_ctx->max_payload_size - RTP_HEVC_HEADERS_SIZE;
48
+    int nal_type           = (buf[0] >> 1) & 0x3F;
49
+
50
+    /* send it as one single NAL unit? */
51
+    if (len <= rtp_ctx->max_payload_size) {
52
+        /* use the original NAL unit buffer and transmit it as RTP payload */
53
+        ff_rtp_send_data(ctx, buf, len, last_packet_of_frame);
54
+    } else {
55
+        /*
56
+          create the HEVC payload header and transmit the buffer as fragmentation units (FU)
57
+
58
+             0                   1
59
+             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
60
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
61
+            |F|   Type    |  LayerId  | TID |
62
+            +-------------+-----------------+
63
+
64
+               F       = 0
65
+               Type    = 49 (fragmentation unit (FU))
66
+               LayerId = 0
67
+               TID     = 1
68
+         */
69
+        rtp_ctx->buf[0] = 49 << 1;
70
+        rtp_ctx->buf[1] = 1;
71
+
72
+        /*
73
+              create the FU header
74
+
75
+              0 1 2 3 4 5 6 7
76
+             +-+-+-+-+-+-+-+-+
77
+             |S|E|  FuType   |
78
+             +---------------+
79
+
80
+                S       = variable
81
+                E       = variable
82
+                FuType  = NAL unit type
83
+         */
84
+        rtp_ctx->buf[2]  = nal_type;
85
+        /* set the S bit: mark as start fragment */
86
+        rtp_ctx->buf[2] |= 1 << 7;
87
+
88
+        /* pass the original NAL header */
89
+        buf += 2;
90
+        len -= 2;
91
+
92
+        while (len + RTP_HEVC_HEADERS_SIZE > rtp_ctx->max_payload_size) {
93
+            /* complete and send current RTP packet */
94
+            memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, rtp_payload_size);
95
+            ff_rtp_send_data(ctx, rtp_ctx->buf, rtp_ctx->max_payload_size, 0);
96
+
97
+            buf += rtp_payload_size;
98
+            len -= rtp_payload_size;
99
+
100
+            /* reset the S bit */
101
+            rtp_ctx->buf[2] &= ~(1 << 7);
102
+        }
103
+
104
+        /* set the E bit: mark as last fragment */
105
+        rtp_ctx->buf[2] |= 1 << 6;
106
+
107
+        /* complete and send last RTP packet */
108
+        memcpy(&rtp_ctx->buf[RTP_HEVC_HEADERS_SIZE], buf, len);
109
+        ff_rtp_send_data(ctx, rtp_ctx->buf, len + 2, last_packet_of_frame);
110
+    }
111
+}
112
+
113
+void ff_rtp_send_hevc(AVFormatContext *ctx, const uint8_t *frame_buf, int frame_size)
114
+{
115
+    const uint8_t *next_NAL_unit;
116
+    const uint8_t *buf_ptr, *buf_end = frame_buf + frame_size;
117
+    RTPMuxContext *rtp_ctx = ctx->priv_data;
118
+
119
+    /* use the default 90 KHz time stamp */
120
+    rtp_ctx->timestamp = rtp_ctx->cur_timestamp;
121
+
122
+    if (rtp_ctx->nal_length_size)
123
+        buf_ptr = avc_mp4_find_startcode(frame_buf, buf_end, rtp_ctx->nal_length_size) ? frame_buf : buf_end;
124
+    else
125
+        buf_ptr = ff_avc_find_startcode(frame_buf, buf_end);
126
+
127
+    /* find all NAL units and send them as separate packets */
128
+    while (buf_ptr < buf_end) {
129
+        if (rtp_ctx->nal_length_size) {
130
+            next_NAL_unit = avc_mp4_find_startcode(buf_ptr, buf_end, rtp_ctx->nal_length_size);
131
+            if (!next_NAL_unit)
132
+                next_NAL_unit = buf_end;
133
+
134
+            buf_ptr += rtp_ctx->nal_length_size;
135
+        } else {
136
+            while (!*(buf_ptr++)) ;
137
+            next_NAL_unit = ff_avc_find_startcode(buf_ptr, buf_end);
138
+        }
139
+        /* send the next NAL unit */
140
+        nal_send(ctx, buf_ptr, next_NAL_unit - buf_ptr, next_NAL_unit == buf_end);
141
+
142
+        /* jump to the next NAL unit */
143
+        buf_ptr = next_NAL_unit;
144
+    }
145
+}
... ...
@@ -31,7 +31,7 @@
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR 56
33 33
 #define LIBAVFORMAT_VERSION_MINOR  4
34
-#define LIBAVFORMAT_VERSION_MICRO 102
34
+#define LIBAVFORMAT_VERSION_MICRO 103
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \