2006-11-07 22:10:07 +00:00
|
|
|
/*
|
|
|
|
* RTP H264 Protocol (RFC3984)
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2006 Ryan Martell
|
2006-11-07 22:10:07 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2006-11-07 22:10:07 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2006-11-07 22:10:07 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2006-11-07 22:10:07 +00:00
|
|
|
* 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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2006-11-07 22:10:07 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-05-04 21:05:52 +00:00
|
|
|
* @file
|
2006-11-07 22:10:07 +00: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-04 21:05:52 +00:00
|
|
|
* Interleaved Mode (2). (This requires implementing STAP-B, MTAP16, MTAP24,
|
|
|
|
* FU-B packet types)
|
2006-11-07 22:10:07 +00:00
|
|
|
*/
|
|
|
|
|
2013-04-18 13:54:26 +00:00
|
|
|
#include "libavutil/attributes.h"
|
2008-05-09 11:56:36 +00:00
|
|
|
#include "libavutil/base64.h"
|
2015-02-19 19:04:45 +00:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2008-05-09 11:56:36 +00:00
|
|
|
#include "libavutil/avstring.h"
|
2006-11-07 22:10:07 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
|
2009-02-06 10:35:52 +00:00
|
|
|
#include "rtpdec.h"
|
2010-07-30 12:04:27 +00:00
|
|
|
#include "rtpdec_formats.h"
|
2006-11-07 22:10:07 +00:00
|
|
|
|
2008-10-04 04:11:12 +00:00
|
|
|
struct PayloadContext {
|
2012-05-04 21:05:52 +00:00
|
|
|
// sdp setup parameters
|
2012-05-04 20:46:18 +00:00
|
|
|
uint8_t profile_idc;
|
|
|
|
uint8_t profile_iop;
|
|
|
|
uint8_t level_idc;
|
|
|
|
int packetization_mode;
|
2006-11-07 22:10:07 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
int packet_types_received[32];
|
|
|
|
#endif
|
2008-10-04 04:11:12 +00:00
|
|
|
};
|
2006-11-07 22:10:07 +00:00
|
|
|
|
2012-05-04 21:28:25 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define COUNT_NAL_TYPE(data, nal) data->packet_types_received[(nal) & 0x1f]++
|
2015-02-19 20:12:21 +00:00
|
|
|
#define NAL_COUNTERS data->packet_types_received
|
2012-05-04 21:28:25 +00:00
|
|
|
#else
|
|
|
|
#define COUNT_NAL_TYPE(data, nal) do { } while (0)
|
2015-02-19 20:12:21 +00:00
|
|
|
#define NAL_COUNTERS NULL
|
2012-05-04 21:28:25 +00:00
|
|
|
#endif
|
2015-02-19 20:12:21 +00:00
|
|
|
#define NAL_MASK 0x1f
|
2012-05-04 21:28:25 +00:00
|
|
|
|
2012-05-04 21:33:39 +00:00
|
|
|
static const uint8_t start_sequence[] = { 0, 0, 0, 1 };
|
|
|
|
|
2015-02-13 22:38:14 +00:00
|
|
|
static void parse_profile_level_id(AVFormatContext *s,
|
|
|
|
PayloadContext *h264_data,
|
2015-02-24 10:31:23 +00:00
|
|
|
const char *value)
|
2015-02-13 22:38:14 +00: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 20:18:55 +00:00
|
|
|
int ff_h264_parse_sprop_parameter_sets(AVFormatContext *s,
|
|
|
|
uint8_t **data_ptr, int *size_ptr,
|
|
|
|
const char *value)
|
2015-02-13 22:38:14 +00: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 19:19:29 +00:00
|
|
|
uint8_t *dest = av_realloc(*data_ptr,
|
2015-02-19 19:12:06 +00:00
|
|
|
packet_size + sizeof(start_sequence) +
|
2015-02-19 19:19:29 +00:00
|
|
|
*size_ptr +
|
2015-06-29 21:48:34 +00:00
|
|
|
AV_INPUT_BUFFER_PADDING_SIZE);
|
2015-02-13 22:38:14 +00:00
|
|
|
if (!dest) {
|
|
|
|
av_log(s, AV_LOG_ERROR,
|
|
|
|
"Unable to allocate memory for extradata!\n");
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2015-02-19 19:19:29 +00:00
|
|
|
*data_ptr = dest;
|
2015-02-13 22:38:14 +00:00
|
|
|
|
2015-02-19 19:19:29 +00:00
|
|
|
memcpy(dest + *size_ptr, start_sequence,
|
2015-02-13 22:38:14 +00:00
|
|
|
sizeof(start_sequence));
|
2015-02-19 19:19:29 +00:00
|
|
|
memcpy(dest + *size_ptr + sizeof(start_sequence),
|
2015-02-13 22:38:14 +00:00
|
|
|
decoded_packet, packet_size);
|
2015-02-19 19:19:29 +00:00
|
|
|
memset(dest + *size_ptr + sizeof(start_sequence) +
|
2015-06-29 21:48:34 +00:00
|
|
|
packet_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2015-02-13 22:38:14 +00:00
|
|
|
|
2015-02-19 19:19:29 +00:00
|
|
|
*size_ptr += sizeof(start_sequence) + packet_size;
|
2015-02-13 22:38:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-05 07:01:07 +00:00
|
|
|
static int sdp_parse_fmtp_config_h264(AVFormatContext *s,
|
|
|
|
AVStream *stream,
|
2012-05-05 14:42:15 +00:00
|
|
|
PayloadContext *h264_data,
|
2015-02-24 10:32:17 +00:00
|
|
|
const char *attr, const char *value)
|
2006-11-07 22:10:07 +00: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 18:42:52 +00:00
|
|
|
AVCodecParameters *par = stream->codecpar;
|
2006-11-07 22:10:07 +00:00
|
|
|
|
|
|
|
if (!strcmp(attr, "packetization-mode")) {
|
2014-07-05 07:01:07 +00:00
|
|
|
av_log(s, AV_LOG_DEBUG, "RTP Packetization Mode: %d\n", atoi(value));
|
2007-08-10 13:34:56 +00:00
|
|
|
h264_data->packetization_mode = atoi(value);
|
2006-11-07 22:10:07 +00:00
|
|
|
/*
|
2012-05-04 21:05:52 +00: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-07 22:10:07 +00:00
|
|
|
*/
|
|
|
|
if (h264_data->packetization_mode > 1)
|
2014-07-05 07:01:07 +00:00
|
|
|
av_log(s, AV_LOG_ERROR,
|
2012-05-05 17:48:08 +00:00
|
|
|
"Interleaved RTP mode is not supported yet.\n");
|
2006-11-07 22:10:07 +00:00
|
|
|
} else if (!strcmp(attr, "profile-level-id")) {
|
2015-02-13 22:38:14 +00:00
|
|
|
if (strlen(value) == 6)
|
|
|
|
parse_profile_level_id(s, h264_data, value);
|
2012-05-05 14:42:15 +00:00
|
|
|
} else if (!strcmp(attr, "sprop-parameter-sets")) {
|
2015-02-19 19:19:29 +00:00
|
|
|
int ret;
|
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 18:42:52 +00: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 19:19:29 +00: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 18:42:52 +00:00
|
|
|
par->extradata, par->extradata_size);
|
2015-02-19 19:19:29 +00:00
|
|
|
return ret;
|
2006-11-07 22:10:07 +00:00
|
|
|
}
|
2010-06-28 11:25:39 +00:00
|
|
|
return 0;
|
2006-11-07 22:10:07 +00: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 18:42:52 +00:00
|
|
|
void ff_h264_parse_framesize(AVCodecParameters *par, const char *p)
|
2015-02-23 18:59:41 +00: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 18:42:52 +00:00
|
|
|
par->width = atoi(buf1);
|
|
|
|
par->height = atoi(p + 1); // skip the -
|
2015-02-23 18:59:41 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 20:18:55 +00:00
|
|
|
int ff_h264_handle_aggregated_packet(AVFormatContext *ctx, AVPacket *pkt,
|
2015-02-19 19:40:48 +00:00
|
|
|
const uint8_t *buf, int len,
|
2015-02-22 21:25:28 +00:00
|
|
|
int skip_between, int *nal_counters,
|
2015-02-19 20:12:21 +00:00
|
|
|
int nal_mask)
|
2015-02-13 22:38:14 +00:00
|
|
|
{
|
|
|
|
int pass = 0;
|
|
|
|
int total_length = 0;
|
|
|
|
uint8_t *dst = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2015-02-19 19:49:18 +00:00
|
|
|
// first we are going to figure out the total size
|
2015-02-13 22:38:14 +00: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 20:12:21 +00:00
|
|
|
if (nal_counters)
|
|
|
|
nal_counters[(*src) & nal_mask]++;
|
2015-02-13 22:38:14 +00: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 19:28:01 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
2015-02-13 22:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// eat what we handled
|
2015-02-22 21:25:28 +00:00
|
|
|
src += nal_size + skip_between;
|
|
|
|
src_len -= nal_size + skip_between;
|
2015-02-13 22:38:14 +00: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 21:28:34 +00: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-13 22:38:14 +00:00
|
|
|
static int h264_handle_packet_fu_a(AVFormatContext *ctx, AVPacket *pkt,
|
2015-02-19 20:12:21 +00:00
|
|
|
const uint8_t *buf, int len,
|
|
|
|
int *nal_counters, int nal_mask)
|
2015-02-13 22:38:14 +00:00
|
|
|
{
|
|
|
|
uint8_t fu_indicator, fu_header, start_bit, nal_type, nal;
|
|
|
|
|
|
|
|
if (len < 3) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Too short data for FU-A H264 RTP packet\n");
|
|
|
|
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 21:28:34 +00: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-13 22:38:14 +00:00
|
|
|
}
|
|
|
|
|
2012-05-04 21:05:52 +00:00
|
|
|
// return 0 on packet, no more left, 1 on packet, 1 on partial packet
|
2012-05-05 14:42:15 +00:00
|
|
|
static int h264_handle_packet(AVFormatContext *ctx, PayloadContext *data,
|
|
|
|
AVStream *st, AVPacket *pkt, uint32_t *timestamp,
|
2012-12-10 12:38:32 +00:00
|
|
|
const uint8_t *buf, int len, uint16_t seq,
|
|
|
|
int flags)
|
2006-11-07 22:10:07 +00:00
|
|
|
{
|
2012-05-04 19:31:46 +00:00
|
|
|
uint8_t nal;
|
|
|
|
uint8_t type;
|
2012-05-05 14:42:15 +00:00
|
|
|
int result = 0;
|
2006-11-07 22:10:07 +00:00
|
|
|
|
2012-05-04 19:31:46 +00:00
|
|
|
if (!len) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Empty H264 RTP packet\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
nal = buf[0];
|
|
|
|
type = nal & 0x1f;
|
|
|
|
|
2012-05-04 21:05:52 +00:00
|
|
|
/* Simplify the case (these are all the nal types used internally by
|
|
|
|
* the h264 codec). */
|
2006-11-07 22:10:07 +00:00
|
|
|
if (type >= 1 && type <= 23)
|
2012-05-04 21:05:52 +00:00
|
|
|
type = 1;
|
2006-11-07 22:10:07 +00:00
|
|
|
switch (type) {
|
2010-12-06 12:44:09 +00:00
|
|
|
case 0: // undefined, but pass them through
|
2006-11-07 22:10:07 +00:00
|
|
|
case 1:
|
2013-12-13 00:27:26 +00:00
|
|
|
if ((result = av_new_packet(pkt, len + sizeof(start_sequence))) < 0)
|
|
|
|
return result;
|
2006-11-07 22:10:07 +00:00
|
|
|
memcpy(pkt->data, start_sequence, sizeof(start_sequence));
|
2012-05-05 14:42:15 +00:00
|
|
|
memcpy(pkt->data + sizeof(start_sequence), buf, len);
|
2012-05-04 21:28:25 +00:00
|
|
|
COUNT_NAL_TYPE(data, nal);
|
2006-11-07 22:10:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 24: // STAP-A (one packet, multiple nals)
|
|
|
|
// consume the STAP-A NAL
|
|
|
|
buf++;
|
|
|
|
len--;
|
2015-02-19 20:18:55 +00:00
|
|
|
result = ff_h264_handle_aggregated_packet(ctx, pkt, buf, len, 0,
|
|
|
|
NAL_COUNTERS, NAL_MASK);
|
2006-11-07 22:10:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 25: // STAP-B
|
|
|
|
case 26: // MTAP-16
|
|
|
|
case 27: // MTAP-24
|
|
|
|
case 29: // FU-B
|
2009-03-02 09:28:37 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
2015-02-20 18:57:17 +00:00
|
|
|
"Unhandled type (%d) (See RFC for implementation details)\n",
|
2006-11-07 22:10:07 +00:00
|
|
|
type);
|
2012-05-04 21:29:15 +00:00
|
|
|
result = AVERROR(ENOSYS);
|
2006-11-07 22:10:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 28: // FU-A (fragmented nal)
|
2015-02-19 20:12:21 +00:00
|
|
|
result = h264_handle_packet_fu_a(ctx, pkt, buf, len,
|
|
|
|
NAL_COUNTERS, NAL_MASK);
|
2006-11-07 22:10:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 30: // undefined
|
|
|
|
case 31: // undefined
|
|
|
|
default:
|
2012-05-05 17:48:08 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Undefined type (%d)\n", type);
|
2012-05-04 21:29:15 +00:00
|
|
|
result = AVERROR_INVALIDDATA;
|
2006-11-07 22:10:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-03 13:51:34 +00:00
|
|
|
pkt->stream_index = st->index;
|
|
|
|
|
2006-11-07 22:10:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-02-24 15:01:48 +00:00
|
|
|
static void h264_close_context(PayloadContext *data)
|
2006-11-07 22:10:07 +00: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 18:41:17 +00:00
|
|
|
static int parse_h264_sdp_line(AVFormatContext *s, int st_index,
|
|
|
|
PayloadContext *h264_data, const char *line)
|
2006-11-07 22:10:07 +00:00
|
|
|
{
|
2012-04-06 20:07:12 +00:00
|
|
|
AVStream *stream;
|
2006-11-07 22:10:07 +00:00
|
|
|
const char *p = line;
|
|
|
|
|
2012-04-06 20:07:12 +00:00
|
|
|
if (st_index < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
stream = s->streams[st_index];
|
2006-11-07 22:10:07 +00:00
|
|
|
|
2007-06-24 11:27:12 +00: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 18:42:52 +00:00
|
|
|
ff_h264_parse_framesize(stream->codecpar, p);
|
2007-06-24 11:27:12 +00:00
|
|
|
} else if (av_strstart(p, "fmtp:", &p)) {
|
2014-07-05 07:01:07 +00:00
|
|
|
return ff_parse_fmtp(s, stream, h264_data, p, sdp_parse_fmtp_config_h264);
|
2007-06-24 11:27:12 +00:00
|
|
|
} else if (av_strstart(p, "cliprect:", &p)) {
|
2006-11-07 22:10:07 +00:00
|
|
|
// could use this if we wanted.
|
|
|
|
}
|
|
|
|
|
2012-05-04 20:46:18 +00:00
|
|
|
return 0;
|
2006-11-07 22:10:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RTPDynamicProtocolHandler ff_h264_dynamic_handler = {
|
2009-07-27 14:29:06 +00:00
|
|
|
.enc_name = "H264",
|
2010-03-30 23:30:55 +00:00
|
|
|
.codec_type = AVMEDIA_TYPE_VIDEO,
|
2012-08-05 09:11:04 +00:00
|
|
|
.codec_id = AV_CODEC_ID_H264,
|
2015-02-23 19:51:05 +00:00
|
|
|
.need_parsing = AVSTREAM_PARSE_FULL,
|
2015-02-23 20:18:32 +00:00
|
|
|
.priv_data_size = sizeof(PayloadContext),
|
2009-07-27 14:29:06 +00:00
|
|
|
.parse_sdp_a_line = parse_h264_sdp_line,
|
2015-02-24 15:01:48 +00:00
|
|
|
.close = h264_close_context,
|
2015-02-23 20:06:41 +00:00
|
|
|
.parse_packet = h264_handle_packet,
|
2006-11-07 22:10:07 +00:00
|
|
|
};
|