1d1be919 |
/* |
41ed7ab4 |
* RTP H.264 Protocol (RFC3984) |
406792e7 |
* Copyright (c) 2006 Ryan Martell |
1d1be919 |
*
* 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
*/
/** |
f3d471f4 |
* @file |
1d1be919 |
* @brief H.264 / RTP Code (RFC3984)
* @author Ryan Martell <rdm4@martellventures.com>
*
* @note Notes:
* Notes:
* This currently supports packetization mode:
* Single Nal Unit Mode (0), or
* Non-Interleaved Mode (1). It currently does not support |
f3d471f4 |
* Interleaved Mode (2). (This requires implementing STAP-B, MTAP16, MTAP24,
* FU-B packet types) |
1d1be919 |
*/
|
7abd35a1 |
#include "libavutil/attributes.h" |
245976da |
#include "libavutil/base64.h" |
8bdbf49c |
#include "libavutil/intreadwrite.h" |
245976da |
#include "libavutil/avstring.h" |
1d1be919 |
#include "avformat.h"
|
302879cb |
#include "rtpdec.h" |
965a3ddb |
#include "rtpdec_formats.h" |
1d1be919 |
|
ed0aacc7 |
struct PayloadContext { |
f3d471f4 |
// sdp setup parameters |
8d43b8b8 |
uint8_t profile_idc;
uint8_t profile_iop;
uint8_t level_idc;
int packetization_mode; |
1d1be919 |
#ifdef DEBUG
int packet_types_received[32];
#endif |
ed0aacc7 |
}; |
1d1be919 |
|
48666c2b |
#ifdef DEBUG
#define COUNT_NAL_TYPE(data, nal) data->packet_types_received[(nal) & 0x1f]++ |
16163e6b |
#define NAL_COUNTERS data->packet_types_received |
48666c2b |
#else
#define COUNT_NAL_TYPE(data, nal) do { } while (0) |
16163e6b |
#define NAL_COUNTERS NULL |
48666c2b |
#endif |
16163e6b |
#define NAL_MASK 0x1f |
48666c2b |
|
b3688617 |
static const uint8_t start_sequence[] = { 0, 0, 0, 1 };
|
b8df0b71 |
static void parse_profile_level_id(AVFormatContext *s,
PayloadContext *h264_data, |
ac0e54fd |
const char *value) |
b8df0b71 |
{
char buffer[3];
// 6 characters=3 bytes, in hex.
uint8_t profile_idc;
uint8_t profile_iop;
uint8_t level_idc;
buffer[0] = value[0];
buffer[1] = value[1];
buffer[2] = '\0';
profile_idc = strtol(buffer, NULL, 16);
buffer[0] = value[2];
buffer[1] = value[3];
profile_iop = strtol(buffer, NULL, 16);
buffer[0] = value[4];
buffer[1] = value[5];
level_idc = strtol(buffer, NULL, 16);
av_log(s, AV_LOG_DEBUG,
"RTP Profile IDC: %x Profile IOP: %x Level: %x\n",
profile_idc, profile_iop, level_idc);
h264_data->profile_idc = profile_idc;
h264_data->profile_iop = profile_iop;
h264_data->level_idc = level_idc;
}
|
0de07510 |
int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s,
uint8_t **data_ptr, int *size_ptr,
const char *value) |
a9a0b8d6 |
{
char base64packet[1024];
uint8_t decoded_packet[1024];
int packet_size;
while (*value) {
char *dst = base64packet;
while (*value && *value != ','
&& (dst - base64packet) < sizeof(base64packet) - 1) {
*dst++ = *value++;
}
*dst++ = '\0';
if (*value == ',')
value++;
packet_size = av_base64_decode(decoded_packet, base64packet,
sizeof(decoded_packet));
if (packet_size > 0) { |
48183b3a |
uint8_t *dest = av_realloc(*data_ptr, |
7650caf0 |
packet_size + sizeof(start_sequence) + |
48183b3a |
*size_ptr + |
059a9348 |
AV_INPUT_BUFFER_PADDING_SIZE); |
a9a0b8d6 |
if (!dest) {
av_log(s, AV_LOG_ERROR,
"Unable to allocate memory for extradata!\n");
return AVERROR(ENOMEM);
} |
48183b3a |
*data_ptr = dest; |
a9a0b8d6 |
|
48183b3a |
memcpy(dest + *size_ptr, start_sequence, |
a9a0b8d6 |
sizeof(start_sequence)); |
48183b3a |
memcpy(dest + *size_ptr + sizeof(start_sequence), |
a9a0b8d6 |
decoded_packet, packet_size); |
48183b3a |
memset(dest + *size_ptr + sizeof(start_sequence) + |
059a9348 |
packet_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
a9a0b8d6 |
|
48183b3a |
*size_ptr += sizeof(start_sequence) + packet_size; |
a9a0b8d6 |
}
}
return 0;
}
|
0307cc22 |
static int sdp_parse_fmtp_config_h264(AVFormatContext *s,
AVStream *stream, |
0b3ac9fe |
PayloadContext *h264_data, |
ec96a89c |
const char *attr, const char *value) |
1d1be919 |
{ |
9200514a |
AVCodecParameters *par = stream->codecpar; |
1d1be919 |
if (!strcmp(attr, "packetization-mode")) { |
0307cc22 |
av_log(s, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value)); |
272872e4 |
h264_data->packetization_mode = atoi(value); |
1d1be919 |
/* |
f3d471f4 |
* Packetization Mode:
* 0 or not present: Single NAL mode (Only nals from 1-23 are allowed)
* 1: Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed.
* 2: Interleaved Mode: 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (FU-A),
* and 29 (FU-B) are allowed. |
1d1be919 |
*/
if (h264_data->packetization_mode > 1) |
0307cc22 |
av_log(s, AV_LOG_ERROR, |
2ed503af |
"Interleaved RTP mode is not supported yet.\n"); |
1d1be919 |
} else if (!strcmp(attr, "profile-level-id")) { |
b8df0b71 |
if (strlen(value) == 6)
parse_profile_level_id(s, h264_data, value); |
0b3ac9fe |
} else if (!strcmp(attr, "sprop-parameter-sets")) { |
48183b3a |
int ret; |
c42a1388 |
if (*value == 0 || value[strlen(value) - 1] == ',') { |
1c7e2cf9 |
av_log(s, AV_LOG_WARNING, "Missing PPS in sprop-parameter-sets, ignoring\n");
return 0;
} |
9200514a |
par->extradata_size = 0;
av_freep(&par->extradata);
ret = ff_h264_parse_sprop_parameter_sets(s, &par->extradata,
&par->extradata_size, value); |
48183b3a |
av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n", |
9200514a |
par->extradata, par->extradata_size); |
48183b3a |
return ret; |
1d1be919 |
} |
7aa0118c |
return 0; |
1d1be919 |
}
|
9200514a |
void ff_h264_parse_framesize(AVCodecParameters *par, const char *p) |
8633fb47 |
{
char buf1[50];
char *dst = buf1;
// remove the protocol identifier
while (*p && *p == ' ')
p++; // strip spaces.
while (*p && *p != ' ')
p++; // eat protocol identifier
while (*p && *p == ' ')
p++; // strip trailing spaces.
while (*p && *p != '-' && (dst - buf1) < sizeof(buf1) - 1)
*dst++ = *p++;
*dst = '\0';
// a='framesize:96 320-240'
// set our parameters |
9200514a |
par->width = atoi(buf1);
par->height = atoi(p + 1); // skip the - |
8633fb47 |
}
|
a10529a3 |
int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, PayloadContext *data, AVPacket *pkt, |
a3cc519d |
const uint8_t *buf, int len, |
fe208ca5 |
int skip_between, int *nal_counters, |
16163e6b |
int nal_mask) |
f0a87479 |
{
int pass = 0;
int total_length = 0;
uint8_t *dst = NULL;
int ret;
|
46ad9ac9 |
// first we are going to figure out the total size |
f0a87479 |
for (pass = 0; pass < 2; pass++) {
const uint8_t *src = buf;
int src_len = len;
while (src_len > 2) {
uint16_t nal_size = AV_RB16(src);
// consume the length of the aggregate
src += 2;
src_len -= 2;
if (nal_size <= src_len) {
if (pass == 0) {
// counting
total_length += sizeof(start_sequence) + nal_size;
} else {
// copying
memcpy(dst, start_sequence, sizeof(start_sequence));
dst += sizeof(start_sequence);
memcpy(dst, src, nal_size); |
16163e6b |
if (nal_counters)
nal_counters[(*src) & nal_mask]++; |
f0a87479 |
dst += nal_size;
}
} else {
av_log(ctx, AV_LOG_ERROR,
"nal size exceeds length: %d %d\n", nal_size, src_len); |
176903ce |
return AVERROR_INVALIDDATA; |
f0a87479 |
}
// eat what we handled |
fe208ca5 |
src += nal_size + skip_between;
src_len -= nal_size + skip_between; |
f0a87479 |
}
if (pass == 0) {
/* now we know the total size of the packet (with the
* start sequences added) */
if ((ret = av_new_packet(pkt, total_length)) < 0)
return ret;
dst = pkt->data;
}
}
return 0;
}
|
3567b91e |
int ff_h264_handle_frag_packet(AVPacket *pkt, const uint8_t *buf, int len,
int start_bit, const uint8_t *nal_header,
int nal_header_len)
{
int ret;
int tot_len = len;
int pos = 0;
if (start_bit)
tot_len += sizeof(start_sequence) + nal_header_len;
if ((ret = av_new_packet(pkt, tot_len)) < 0)
return ret;
if (start_bit) {
memcpy(pkt->data + pos, start_sequence, sizeof(start_sequence));
pos += sizeof(start_sequence);
memcpy(pkt->data + pos, nal_header, nal_header_len);
pos += nal_header_len;
}
memcpy(pkt->data + pos, buf, len);
return 0;
}
|
88868d81 |
static int h264_handle_packet_fu_a(AVFormatContext *ctx, PayloadContext *data, AVPacket *pkt, |
16163e6b |
const uint8_t *buf, int len,
int *nal_counters, int nal_mask) |
3b33dfb2 |
{
uint8_t fu_indicator, fu_header, start_bit, nal_type, nal;
if (len < 3) { |
41ed7ab4 |
av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H.264 RTP packet\n"); |
3b33dfb2 |
return AVERROR_INVALIDDATA;
}
fu_indicator = buf[0];
fu_header = buf[1];
start_bit = fu_header >> 7;
nal_type = fu_header & 0x1f;
nal = fu_indicator & 0xe0 | nal_type;
// skip the fu_indicator and fu_header
buf += 2;
len -= 2;
|
3567b91e |
if (start_bit && nal_counters)
nal_counters[nal_type & nal_mask]++;
return ff_h264_handle_frag_packet(pkt, buf, len, start_bit, &nal, 1); |
3b33dfb2 |
}
|
f3d471f4 |
// return 0 on packet, no more left, 1 on packet, 1 on partial packet |
0b3ac9fe |
static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
AVStream *st, AVPacket *pkt, uint32_t *timestamp, |
90c784cc |
const uint8_t *buf, int len, uint16_t seq,
int flags) |
1d1be919 |
{ |
de26a4b6 |
uint8_t nal;
uint8_t type; |
0b3ac9fe |
int result = 0; |
1d1be919 |
|
de26a4b6 |
if (!len) { |
41ed7ab4 |
av_log(ctx, AV_LOG_ERROR, "Empty H.264 RTP packet\n"); |
de26a4b6 |
return AVERROR_INVALIDDATA;
}
nal = buf[0];
type = nal & 0x1f;
|
41ed7ab4 |
/* Simplify the case (these are all the NAL types used internally by
* the H.264 codec). */ |
1d1be919 |
if (type >= 1 && type <= 23) |
f3d471f4 |
type = 1; |
1d1be919 |
switch (type) { |
86042de8 |
case 0: // undefined, but pass them through |
1d1be919 |
case 1: |
c5f15f40 |
if ((result = av_new_packet(pkt, len + sizeof(start_sequence))) < 0)
return result; |
1d1be919 |
memcpy(pkt->data, start_sequence, sizeof(start_sequence)); |
0b3ac9fe |
memcpy(pkt->data + sizeof(start_sequence), buf, len); |
48666c2b |
COUNT_NAL_TYPE(data, nal); |
1d1be919 |
break;
case 24: // STAP-A (one packet, multiple nals)
// consume the STAP-A NAL
buf++;
len--; |
a10529a3 |
result = ff_h264_handle_aggregated_packet(ctx, data, pkt, buf, len, 0, |
0de07510 |
NAL_COUNTERS, NAL_MASK); |
1d1be919 |
break;
case 25: // STAP-B
case 26: // MTAP-16
case 27: // MTAP-24
case 29: // FU-B |
d50b3fe4 |
avpriv_report_missing_feature(ctx, "RTP H.264 NAL unit type %d", type); |
94dee906 |
result = AVERROR_PATCHWELCOME; |
1d1be919 |
break;
case 28: // FU-A (fragmented nal) |
7fd732b7 |
result = h264_handle_packet_fu_a(ctx, data, pkt, buf, len, |
16163e6b |
NAL_COUNTERS, NAL_MASK); |
1d1be919 |
break;
case 30: // undefined
case 31: // undefined
default: |
2ed503af |
av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)\n", type); |
b7b7354c |
result = AVERROR_INVALIDDATA; |
1d1be919 |
break;
}
|
eafb17d1 |
pkt->stream_index = st->index;
|
1d1be919 |
return result;
}
|
d594dbec |
static void h264_close_context(PayloadContext *data) |
1d1be919 |
{
#ifdef DEBUG
int ii;
for (ii = 0; ii < 32; ii++) {
if (data->packet_types_received[ii])
av_log(NULL, AV_LOG_DEBUG, "Received %d packets of type %d\n",
data->packet_types_received[ii], ii);
}
#endif
}
|
7b2a0708 |
static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
PayloadContext *h264_data, const char *line) |
1d1be919 |
{ |
45600148 |
AVStream *stream; |
1d1be919 |
const char *p = line;
|
45600148 |
if (st_index < 0)
return 0;
stream = s->streams[st_index]; |
1d1be919 |
|
f7d78f36 |
if (av_strstart(p, "framesize:", &p)) { |
9200514a |
ff_h264_parse_framesize(stream->codecpar, p); |
f7d78f36 |
} else if (av_strstart(p, "fmtp:", &p)) { |
0307cc22 |
return ff_parse_fmtp(s, stream, h264_data, p, sdp_parse_fmtp_config_h264); |
f7d78f36 |
} else if (av_strstart(p, "cliprect:", &p)) { |
1d1be919 |
// could use this if we wanted.
}
|
8d43b8b8 |
return 0; |
1d1be919 |
}
RTPDynamicProtocolHandler ff_h264_dynamic_handler = { |
202a6697 |
.enc_name = "H264", |
72415b2a |
.codec_type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.codec_id = AV_CODEC_ID_H264, |
2b982e92 |
.need_parsing = AVSTREAM_PARSE_FULL, |
5d8cae45 |
.priv_data_size = sizeof(PayloadContext), |
202a6697 |
.parse_sdp_a_line = parse_h264_sdp_line, |
d594dbec |
.close = h264_close_context, |
3cd5828f |
.parse_packet = h264_handle_packet, |
1d1be919 |
}; |