2006-11-08 00:10:07 +02:00
|
|
|
/*
|
2016-04-27 19:45:23 +02:00
|
|
|
* RTP H.264 Protocol (RFC3984)
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2006 Ryan Martell
|
2006-11-08 00:10:07 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-05-05 00:05:52 +03:00
|
|
|
* @file
|
2006-11-08 00:10:07 +02:00
|
|
|
* @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
|
2012-05-05 00:05:52 +03:00
|
|
|
* Interleaved Mode (2). (This requires implementing STAP-B, MTAP16, MTAP24,
|
|
|
|
* FU-B packet types)
|
2006-11-08 00:10:07 +02:00
|
|
|
*/
|
|
|
|
|
2013-04-18 16:54:26 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavutil/base64.h"
|
2015-02-19 21:04:45 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2008-05-09 14:56:36 +03:00
|
|
|
#include "libavutil/avstring.h"
|
2006-11-08 00:10:07 +02:00
|
|
|
#include "avformat.h"
|
|
|
|
|
2009-02-06 12:35:52 +02:00
|
|
|
#include "rtpdec.h"
|
2010-07-30 15:04:27 +03:00
|
|
|
#include "rtpdec_formats.h"
|
2006-11-08 00:10:07 +02:00
|
|
|
|
2008-10-04 07:11:12 +03:00
|
|
|
struct PayloadContext {
|
2012-05-05 00:05:52 +03:00
|
|
|
// sdp setup parameters
|
2012-05-04 23:46:18 +03:00
|
|
|
uint8_t profile_idc;
|
|
|
|
uint8_t profile_iop;
|
|
|
|
uint8_t level_idc;
|
|
|
|
int packetization_mode;
|
2006-11-08 00:10:07 +02:00
|
|
|
#ifdef DEBUG
|
|
|
|
int packet_types_received[32];
|
|
|
|
#endif
|
2008-10-04 07:11:12 +03:00
|
|
|
};
|
2006-11-08 00:10:07 +02:00
|
|
|
|
2012-05-05 00:28:25 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define COUNT_NAL_TYPE(data, nal) data->packet_types_received[(nal) & 0x1f]++
|
2015-02-19 22:12:21 +02:00
|
|
|
#define NAL_COUNTERS data->packet_types_received
|
2012-05-05 00:28:25 +03:00
|
|
|
#else
|
|
|
|
#define COUNT_NAL_TYPE(data, nal) do { } while (0)
|
2015-02-19 22:12:21 +02:00
|
|
|
#define NAL_COUNTERS NULL
|
2012-05-05 00:28:25 +03:00
|
|
|
#endif
|
2015-02-19 22:12:21 +02:00
|
|
|
#define NAL_MASK 0x1f
|
2012-05-05 00:28:25 +03:00
|
|
|
|
2012-05-05 00:33:39 +03:00
|
|
|
static const uint8_t start_sequence[] = { 0, 0, 0, 1 };
|
|
|
|
|
2015-02-14 00:38:14 +02:00
|
|
|
static void parse_profile_level_id(AVFormatContext *s,
|
|
|
|
PayloadContext *h264_data,
|
2015-02-24 12:31:23 +02:00
|
|
|
const char *value)
|
2015-02-14 00:38:14 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-19 22:18:55 +02:00
|
|
|
int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s,
|
|
|
|
uint8_t **data_ptr, int *size_ptr,
|
|
|
|
const char *value)
|
2015-02-14 00:38:14 +02:00
|
|
|
{
|
|
|
|
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) {
|
2015-02-19 21:19:29 +02:00
|
|
|
uint8_t *dest = av_realloc(*data_ptr,
|
2015-02-19 21:12:06 +02:00
|
|
|
packet_size + sizeof(start_sequence) +
|
2015-02-19 21:19:29 +02:00
|
|
|
*size_ptr +
|
2015-06-29 23:48:34 +02:00
|
|
|
AV_INPUT_BUFFER_PADDING_SIZE);
|
2015-02-14 00:38:14 +02:00
|
|
|
if (!dest) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Unable to allocate memory for extradata!\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2015-02-19 21:19:29 +02:00
|
|
|
*data_ptr = dest;
|
2015-02-14 00:38:14 +02:00
|
|
|
|
2015-02-19 21:19:29 +02:00
|
|
|
memcpy(dest + *size_ptr, start_sequence,
|
2015-02-14 00:38:14 +02:00
|
|
|
sizeof(start_sequence));
|
2015-02-19 21:19:29 +02:00
|
|
|
memcpy(dest + *size_ptr + sizeof(start_sequence),
|
2015-02-14 00:38:14 +02:00
|
|
|
decoded_packet, packet_size);
|
2015-02-19 21:19:29 +02:00
|
|
|
memset(dest + *size_ptr + sizeof(start_sequence) +
|
2015-06-29 23:48:34 +02:00
|
|
|
packet_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2015-02-14 00:38:14 +02:00
|
|
|
|
2015-02-19 21:19:29 +02:00
|
|
|
*size_ptr += sizeof(start_sequence) + packet_size;
|
2015-02-14 00:38:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-05 10:01:07 +03:00
|
|
|
static int sdp_parse_fmtp_config_h264(AVFormatContext *s,
|
|
|
|
AVStream *stream,
|
2012-05-05 17:42:15 +03:00
|
|
|
PayloadContext *h264_data,
|
2015-02-24 12:32:17 +02:00
|
|
|
const char *attr, const char *value)
|
2006-11-08 00:10:07 +02:00
|
|
|
{
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
AVCodecParameters *par = stream->codecpar;
|
2006-11-08 00:10:07 +02:00
|
|
|
|
|
|
|
if (!strcmp(attr, "packetization-mode")) {
|
2014-07-05 10:01:07 +03:00
|
|
|
av_log(s, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value));
|
2007-08-10 16:34:56 +03:00
|
|
|
h264_data->packetization_mode = atoi(value);
|
2006-11-08 00:10:07 +02:00
|
|
|
/*
|
2012-05-05 00:05:52 +03:00
|
|
|
* 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.
|
2006-11-08 00:10:07 +02:00
|
|
|
*/
|
|
|
|
if (h264_data->packetization_mode > 1)
|
2014-07-05 10:01:07 +03:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
2012-05-05 20:48:08 +03:00
|
|
|
"Interleaved RTP mode is not supported yet.\n");
|
2006-11-08 00:10:07 +02:00
|
|
|
} else if (!strcmp(attr, "profile-level-id")) {
|
2015-02-14 00:38:14 +02:00
|
|
|
if (strlen(value) == 6)
|
|
|
|
parse_profile_level_id(s, h264_data, value);
|
2012-05-05 17:42:15 +03:00
|
|
|
} else if (!strcmp(attr, "sprop-parameter-sets")) {
|
2015-02-19 21:19:29 +02:00
|
|
|
int ret;
|
2016-02-27 21:32:53 +02:00
|
|
|
if (value[strlen(value) - 1] == ',') {
|
|
|
|
av_log(s, AV_LOG_WARNING, "Missing PPS in sprop-parameter-sets, ignoring\n");
|
|
|
|
return 0;
|
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->extradata_size = 0;
|
|
|
|
av_freep(&par->extradata);
|
|
|
|
ret = ff_h264_parse_sprop_parameter_sets(s, &par->extradata,
|
|
|
|
&par->extradata_size, value);
|
2015-02-19 21:19:29 +02:00
|
|
|
av_log(s, AV_LOG_DEBUG, "Extradata set to %p (size: %d)\n",
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->extradata, par->extradata_size);
|
2015-02-19 21:19:29 +02:00
|
|
|
return ret;
|
2006-11-08 00:10:07 +02:00
|
|
|
}
|
2010-06-28 14:25:39 +03:00
|
|
|
return 0;
|
2006-11-08 00:10:07 +02:00
|
|
|
}
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
void ff_h264_parse_framesize(AVCodecParameters *par, const char *p)
|
2015-02-23 20:59:41 +02:00
|
|
|
{
|
|
|
|
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
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
par->width = atoi(buf1);
|
|
|
|
par->height = atoi(p + 1); // skip the -
|
2015-02-23 20:59:41 +02:00
|
|
|
}
|
|
|
|
|
2015-02-21 13:56:08 +02:00
|
|
|
int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, PayloadContext *data, AVPacket *pkt,
|
2015-02-19 21:40:48 +02:00
|
|
|
const uint8_t *buf, int len,
|
2015-02-22 23:25:28 +02:00
|
|
|
int skip_between, int *nal_counters,
|
2015-02-19 22:12:21 +02:00
|
|
|
int nal_mask)
|
2015-02-14 00:38:14 +02:00
|
|
|
{
|
|
|
|
int pass = 0;
|
|
|
|
int total_length = 0;
|
|
|
|
uint8_t *dst = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2015-02-19 21:49:18 +02:00
|
|
|
// first we are going to figure out the total size
|
2015-02-14 00:38:14 +02:00
|
|
|
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);
|
2015-02-19 22:12:21 +02:00
|
|
|
if (nal_counters)
|
|
|
|
nal_counters[(*src) & nal_mask]++;
|
2015-02-14 00:38:14 +02:00
|
|
|
dst += nal_size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
|
|
|
"nal size exceeds length: %d %d\n", nal_size, src_len);
|
2015-02-19 21:28:01 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
2015-02-14 00:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// eat what we handled
|
2015-02-22 23:25:28 +02:00
|
|
|
src += nal_size + skip_between;
|
|
|
|
src_len -= nal_size + skip_between;
|
2015-02-14 00:38:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-23 23:28:34 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-16 19:05:28 +02:00
|
|
|
static int h264_handle_packet_fu_a(AVFormatContext *ctx, PayloadContext *data, AVPacket *pkt,
|
2015-02-19 22:12:21 +02:00
|
|
|
const uint8_t *buf, int len,
|
|
|
|
int *nal_counters, int nal_mask)
|
2015-02-14 00:38:14 +02:00
|
|
|
{
|
|
|
|
uint8_t fu_indicator, fu_header, start_bit, nal_type, nal;
|
|
|
|
|
|
|
|
if (len < 3) {
|
2016-04-27 19:45:23 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H.264 RTP packet\n");
|
2015-02-14 00:38:14 +02:00
|
|
|
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;
|
|
|
|
|
2015-02-23 23:28:34 +02:00
|
|
|
if (start_bit && nal_counters)
|
|
|
|
nal_counters[nal_type & nal_mask]++;
|
|
|
|
return ff_h264_handle_frag_packet(pkt, buf, len, start_bit, &nal, 1);
|
2015-02-14 00:38:14 +02:00
|
|
|
}
|
|
|
|
|
2012-05-05 00:05:52 +03:00
|
|
|
// return 0 on packet, no more left, 1 on packet, 1 on partial packet
|
2012-05-05 17:42:15 +03:00
|
|
|
static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
|
|
|
|
AVStream *st, AVPacket *pkt, uint32_t *timestamp,
|
2012-12-10 15:38:32 +03:00
|
|
|
const uint8_t *buf, int len, uint16_t seq,
|
|
|
|
int flags)
|
2006-11-08 00:10:07 +02:00
|
|
|
{
|
2012-05-04 22:31:46 +03:00
|
|
|
uint8_t nal;
|
|
|
|
uint8_t type;
|
2012-05-05 17:42:15 +03:00
|
|
|
int result = 0;
|
2006-11-08 00:10:07 +02:00
|
|
|
|
2012-05-04 22:31:46 +03:00
|
|
|
if (!len) {
|
2016-04-27 19:45:23 +02:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Empty H.264 RTP packet\n");
|
2012-05-04 22:31:46 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
nal = buf[0];
|
|
|
|
type = nal & 0x1f;
|
|
|
|
|
2016-04-27 19:45:23 +02:00
|
|
|
/* Simplify the case (these are all the NAL types used internally by
|
|
|
|
* the H.264 codec). */
|
2006-11-08 00:10:07 +02:00
|
|
|
if (type >= 1 && type <= 23)
|
2012-05-05 00:05:52 +03:00
|
|
|
type = 1;
|
2006-11-08 00:10:07 +02:00
|
|
|
switch (type) {
|
2010-12-06 14:44:09 +02:00
|
|
|
case 0: // undefined, but pass them through
|
2006-11-08 00:10:07 +02:00
|
|
|
case 1:
|
2013-12-13 03:27:26 +03:00
|
|
|
if ((result = av_new_packet(pkt, len + sizeof(start_sequence))) < 0)
|
|
|
|
return result;
|
2006-11-08 00:10:07 +02:00
|
|
|
memcpy(pkt->data, start_sequence, sizeof(start_sequence));
|
2012-05-05 17:42:15 +03:00
|
|
|
memcpy(pkt->data + sizeof(start_sequence), buf, len);
|
2012-05-05 00:28:25 +03:00
|
|
|
COUNT_NAL_TYPE(data, nal);
|
2006-11-08 00:10:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 24: // STAP-A (one packet, multiple nals)
|
|
|
|
// consume the STAP-A NAL
|
|
|
|
buf++;
|
|
|
|
len--;
|
2015-02-21 13:56:08 +02:00
|
|
|
result = ff_h264_handle_aggregated_packet(ctx, data, pkt, buf, len, 0,
|
2015-02-19 22:18:55 +02:00
|
|
|
NAL_COUNTERS, NAL_MASK);
|
2006-11-08 00:10:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 25: // STAP-B
|
|
|
|
case 26: // MTAP-16
|
|
|
|
case 27: // MTAP-24
|
|
|
|
case 29: // FU-B
|
2016-04-29 21:19:33 +02:00
|
|
|
avpriv_report_missing_feature(ctx, "RTP H.264 NAL unit type %d", type);
|
2016-04-29 19:37:24 +02:00
|
|
|
result = AVERROR_PATCHWELCOME;
|
2006-11-08 00:10:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 28: // FU-A (fragmented nal)
|
2015-02-21 13:44:09 +02:00
|
|
|
result = h264_handle_packet_fu_a(ctx, data, pkt, buf, len,
|
2015-02-19 22:12:21 +02:00
|
|
|
NAL_COUNTERS, NAL_MASK);
|
2006-11-08 00:10:07 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 30: // undefined
|
|
|
|
case 31: // undefined
|
|
|
|
default:
|
2012-05-05 20:48:08 +03:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)\n", type);
|
2012-05-05 00:29:15 +03:00
|
|
|
result = AVERROR_INVALIDDATA;
|
2006-11-08 00:10:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-03 15:51:34 +02:00
|
|
|
pkt->stream_index = st->index;
|
|
|
|
|
2006-11-08 00:10:07 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:01:48 +02:00
|
|
|
static void h264_close_context(PayloadContext *data)
|
2006-11-08 00:10:07 +02:00
|
|
|
{
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
2008-12-06 20:41:17 +02:00
|
|
|
static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
|
|
|
|
PayloadContext *h264_data, const char *line)
|
2006-11-08 00:10:07 +02:00
|
|
|
{
|
2012-04-06 23:07:12 +03:00
|
|
|
AVStream *stream;
|
2006-11-08 00:10:07 +02:00
|
|
|
const char *p = line;
|
|
|
|
|
2012-04-06 23:07:12 +03:00
|
|
|
if (st_index < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
stream = s->streams[st_index];
|
2006-11-08 00:10:07 +02:00
|
|
|
|
2007-06-24 14:27:12 +03:00
|
|
|
if (av_strstart(p, "framesize:", &p)) {
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 21:42:52 +03:00
|
|
|
ff_h264_parse_framesize(stream->codecpar, p);
|
2007-06-24 14:27:12 +03:00
|
|
|
} else if (av_strstart(p, "fmtp:", &p)) {
|
2014-07-05 10:01:07 +03:00
|
|
|
return ff_parse_fmtp(s, stream, h264_data, p, sdp_parse_fmtp_config_h264);
|
2007-06-24 14:27:12 +03:00
|
|
|
} else if (av_strstart(p, "cliprect:", &p)) {
|
2006-11-08 00:10:07 +02:00
|
|
|
// could use this if we wanted.
|
|
|
|
}
|
|
|
|
|
2012-05-04 23:46:18 +03:00
|
|
|
return 0;
|
2006-11-08 00:10:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
|
2009-07-27 17:29:06 +03:00
|
|
|
.enc_name = "H264",
|
2010-03-31 02:30:55 +03:00
|
|
|
.codec_type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 12:11:04 +03:00
|
|
|
.codec_id = AV_CODEC_ID_H264,
|
2015-02-23 21:51:05 +02:00
|
|
|
.need_parsing = AVSTREAM_PARSE_FULL,
|
2015-02-23 22:18:32 +02:00
|
|
|
.priv_data_size = sizeof(PayloadContext),
|
2009-07-27 17:29:06 +03:00
|
|
|
.parse_sdp_a_line = parse_h264_sdp_line,
|
2015-02-24 17:01:48 +02:00
|
|
|
.close = h264_close_context,
|
2015-02-23 22:06:41 +02:00
|
|
|
.parse_packet = h264_handle_packet,
|
2006-11-08 00:10:07 +02:00
|
|
|
};
|