diff --git a/Changelog b/Changelog index 59a26c2f49..517145d76c 100644 --- a/Changelog +++ b/Changelog @@ -34,7 +34,7 @@ version 2.4: - ICY metadata are now requested by default with the HTTP protocol - support for using metadata in stream specifiers in fftools - LZMA compression support in TIFF decoder -- support for H.261 RTP payload format (RFC 4587) +- H.261 RTP payload format (RFC 4587) depacketizer and experimental packetizer - HEVC/H.265 RTP payload format (draft v6) depacketizer - added codecview filter to visualize information exported by some codecs - Matroska 3D support thorugh side data diff --git a/libavformat/rtpdec_formats.h b/libavformat/rtpdec_formats.h index 803410e164..87e316fac5 100644 --- a/libavformat/rtpdec_formats.h +++ b/libavformat/rtpdec_formats.h @@ -31,10 +31,6 @@ */ int ff_wms_parse_sdp_a_line(AVFormatContext *s, const char *p); -int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data, - AVStream *st, AVPacket *pkt, uint32_t *timestamp, - const uint8_t *buf, int len, uint16_t seq, int flags); - int ff_h263_handle_packet(AVFormatContext *ctx, PayloadContext *data, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, uint16_t seq, int flags); diff --git a/libavformat/rtpdec_h261.c b/libavformat/rtpdec_h261.c index 43244bb43d..9f3488931b 100644 --- a/libavformat/rtpdec_h261.c +++ b/libavformat/rtpdec_h261.c @@ -19,11 +19,11 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavcodec/get_bits.h" #include "avformat.h" #include "rtpdec_formats.h" -#include "libavcodec/get_bits.h" -#define RTP_H261_PAYLOAD_HEADER_SIZE 4 +#define RTP_H261_PAYLOAD_HEADER_SIZE 4 struct PayloadContext { AVIOContext *buf; @@ -63,8 +63,6 @@ static av_cold void h261_free_context(PayloadContext *pl_ctx) static av_cold int h261_init(AVFormatContext *ctx, int st_index, PayloadContext *data) { - //av_log(ctx, AV_LOG_DEBUG, "h261_init() for stream %d\n", st_index); - if (st_index < 0) return 0; @@ -73,16 +71,18 @@ static av_cold int h261_init(AVFormatContext *ctx, int st_index, return 0; } -int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data, - AVStream *st, AVPacket *pkt, uint32_t *timestamp, - const uint8_t *buf, int len, uint16_t seq, int flags) +static int h261_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_h261_ctx, + AVStream *st, AVPacket *pkt, uint32_t *timestamp, + const uint8_t *buf, int len, uint16_t seq, + int flags) { int sbit, ebit, gobn, mbap, quant; int res; - /* drop data of previous packets in case of non-continuous (loss) packet stream */ - if (data->buf && data->timestamp != *timestamp) { - h261_free_dyn_buffer(&data->buf); + /* drop data of previous packets in case of non-continuous (lossy) packet stream */ + if (rtp_h261_ctx->buf && rtp_h261_ctx->timestamp != *timestamp) { + h261_free_dyn_buffer(&rtp_h261_ctx->buf); + rtp_h261_ctx->endbyte_bits = 0; } /* sanity check for size of input packet: 1 byte payload at least */ @@ -92,26 +92,25 @@ int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data, } /* - decode the H.261 payload header according to section 4.1 of RFC 4587: - (uses 4 bytes between RTP header and H.261 stream per packet) - - 0 1 2 3 - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - |SBIT |EBIT |I|V| GOBN | MBAP | QUANT | HMVD | VMVD | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - - Start bit position (SBIT): 3 bits - End bit position (EBIT): 3 bits - INTRA-frame encoded data (I): 1 bit - Motion Vector flag (V): 1 bit - GOB number (GOBN): 4 bits - Macroblock address predictor (MBAP): 5 bits - Quantizer (QUANT): 5 bits - Horizontal motion vector data (HMVD): 5 bits - Vertical motion vector data (VMVD): 5 bits - - */ + * decode the H.261 payload header according to section 4.1 of RFC 4587: + * (uses 4 bytes between RTP header and H.261 stream per packet) + * + * 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |SBIT |EBIT |I|V| GOBN | MBAP | QUANT | HMVD | VMVD | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * Start bit position (SBIT): 3 bits + * End bit position (EBIT): 3 bits + * INTRA-frame encoded data (I): 1 bit + * Motion Vector flag (V): 1 bit + * GOB number (GOBN): 4 bits + * Macroblock address predictor (MBAP): 5 bits + * Quantizer (QUANT): 5 bits + * Horizontal motion vector data (HMVD): 5 bits + * Vertical motion vector data (VMVD): 5 bits + */ sbit = (buf[0] >> 5) & 0x07; ebit = (buf[0] >> 2) & 0x07; gobn = (buf[1] >> 4) & 0x0f; @@ -123,14 +122,14 @@ int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data, len -= RTP_H261_PAYLOAD_HEADER_SIZE; /* start frame buffering with new dynamic buffer */ - if (!data->buf) { - /* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, uqnat=0 */ - if (!gobn && !sbit && !mbap && !quant){ - res = avio_open_dyn_buf(&data->buf); + if (!rtp_h261_ctx->buf) { + /* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, quant=0 */ + if (!gobn && !sbit && !mbap && !quant) { + res = avio_open_dyn_buf(&rtp_h261_ctx->buf); if (res < 0) return res; /* update the timestamp in the frame packet with the one from the RTP packet */ - data->timestamp = *timestamp; + rtp_h261_ctx->timestamp = *timestamp; } else { /* frame not started yet, need more packets */ return AVERROR(EAGAIN); @@ -138,39 +137,39 @@ int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data, } /* do the "byte merging" at the boundaries of two consecutive frame fragments */ - if (data->endbyte_bits || sbit) { - if (data->endbyte_bits == sbit) { - data->endbyte |= buf[0] & (0xff >> sbit); - data->endbyte_bits = 0; + if (rtp_h261_ctx->endbyte_bits || sbit) { + if (rtp_h261_ctx->endbyte_bits == sbit) { + rtp_h261_ctx->endbyte |= buf[0] & (0xff >> sbit); + rtp_h261_ctx->endbyte_bits = 0; buf++; len--; - avio_w8(data->buf, data->endbyte); + avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte); } else { /* ebit/sbit values inconsistent, assuming packet loss */ GetBitContext gb; init_get_bits(&gb, buf, len*8 - ebit); skip_bits(&gb, sbit); - if (data->endbyte_bits) { - data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits); - avio_w8(data->buf, data->endbyte); + if (rtp_h261_ctx->endbyte_bits) { + rtp_h261_ctx->endbyte |= get_bits(&gb, 8 - rtp_h261_ctx->endbyte_bits); + avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte); } while (get_bits_left(&gb) >= 8) - avio_w8(data->buf, get_bits(&gb, 8)); - data->endbyte_bits = get_bits_left(&gb); - if (data->endbyte_bits) - data->endbyte = get_bits(&gb, data->endbyte_bits) << - (8 - data->endbyte_bits); + avio_w8(rtp_h261_ctx->buf, get_bits(&gb, 8)); + rtp_h261_ctx->endbyte_bits = get_bits_left(&gb); + if (rtp_h261_ctx->endbyte_bits) + rtp_h261_ctx->endbyte = get_bits(&gb, rtp_h261_ctx->endbyte_bits) << + (8 - rtp_h261_ctx->endbyte_bits); ebit = 0; - len = 0; + len = 0; } } if (ebit) { if (len > 0) - avio_write(data->buf, buf, len - 1); - data->endbyte_bits = 8 - ebit; - data->endbyte = buf[len - 1] & (0xff << ebit); + avio_write(rtp_h261_ctx->buf, buf, len - 1); + rtp_h261_ctx->endbyte_bits = 8 - ebit; + rtp_h261_ctx->endbyte = buf[len - 1] & (0xff << ebit); } else { - avio_write(data->buf, buf, len); + avio_write(rtp_h261_ctx->buf, buf, len); } /* RTP marker bit means: last fragment of current frame was received; @@ -179,12 +178,12 @@ int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data, return AVERROR(EAGAIN); /* write the completed last byte from the "byte merging" */ - if (data->endbyte_bits) - avio_w8(data->buf, data->endbyte); - data->endbyte_bits = 0; + if (rtp_h261_ctx->endbyte_bits) + avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte); + rtp_h261_ctx->endbyte_bits = 0; /* close frame buffering and create resulting A/V packet */ - res = ff_rtp_finalize_packet(pkt, &data->buf, st->index); + res = ff_rtp_finalize_packet(pkt, &rtp_h261_ctx->buf, st->index); if (res < 0) return res; @@ -196,8 +195,8 @@ RTPDynamicProtocolHandler ff_h261_dynamic_handler = { .codec_type = AVMEDIA_TYPE_VIDEO, .codec_id = AV_CODEC_ID_H261, .init = h261_init, - .parse_packet = ff_h261_handle_packet, .alloc = h261_new_context, .free = h261_free_context, + .parse_packet = h261_handle_packet, .static_payload_id = 31, }; diff --git a/libavformat/rtpenc.c b/libavformat/rtpenc.c index ef8b33c7cd..c4c4d3e416 100644 --- a/libavformat/rtpenc.c +++ b/libavformat/rtpenc.c @@ -89,7 +89,7 @@ static int is_supported(enum AVCodecID id) static int rtp_write_header(AVFormatContext *s1) { RTPMuxContext *s = s1->priv_data; - int n; + int n, ret = AVERROR(EINVAL); AVStream *st; if (s1->nb_streams != 1) { @@ -195,6 +195,17 @@ static int rtp_write_header(AVFormatContext *s1) s->max_payload_size = n * TS_PACKET_SIZE; s->buf_ptr = s->buf; break; + case AV_CODEC_ID_H261: + if (s1->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) { + av_log(s, AV_LOG_ERROR, + "Packetizing H261 is experimental and produces incorrect " + "packetization for cases where GOBs don't fit into packets " + "(even though most receivers may handle it just fine). " + "Please set -f_strict experimental in order to enable it.\n"); + ret = AVERROR_EXPERIMENTAL; + goto fail; + } + break; case AV_CODEC_ID_H264: /* check for H.264 MP4 syntax */ if (st->codec->extradata_size > 4 && st->codec->extradata[0] == 1) { @@ -277,7 +288,7 @@ defaultcase: fail: av_freep(&s->buf); - return AVERROR(EINVAL); + return ret; } /* send an rtcp sender report packet */ diff --git a/libavformat/rtpenc_h261.c b/libavformat/rtpenc_h261.c index 6f63703f0a..9930b71473 100644 --- a/libavformat/rtpenc_h261.c +++ b/libavformat/rtpenc_h261.c @@ -22,36 +22,81 @@ #include "avformat.h" #include "rtpenc.h" -void ff_rtp_send_h261(AVFormatContext *s1, const uint8_t *frame_buf, int frame_size) +#define RTP_H261_HEADER_SIZE 4 + +static const uint8_t *find_resync_marker_reverse(const uint8_t *restrict start, + const uint8_t *restrict end) +{ + const uint8_t *p = end - 1; + start += 1; /* Make sure we never return the original start. */ + for (; p > start; p--) { + if (p[0] == 0 && p[1] == 1) + return p; + } + return end; +} + +void ff_rtp_send_h261(AVFormatContext *ctx, const uint8_t *frame_buf, int frame_size) { - RTPMuxContext *rtp_ctx = s1->priv_data; - int processed_frame_size; + int cur_frame_size; int last_packet_of_frame; - uint8_t *tmp_buf_ptr; + RTPMuxContext *rtp_ctx = ctx->priv_data; /* use the default 90 KHz time stamp */ rtp_ctx->timestamp = rtp_ctx->cur_timestamp; /* continue as long as not all frame data is processed */ while (frame_size > 0) { - tmp_buf_ptr = rtp_ctx->buf; - *tmp_buf_ptr++ = 1; /* V=1 */ - *tmp_buf_ptr++ = 0; - *tmp_buf_ptr++ = 0; - *tmp_buf_ptr++ = 0; - - processed_frame_size = FFMIN(rtp_ctx->max_payload_size - 4, frame_size); + /* + * encode the H.261 payload header according to section 4.1 of RFC 4587: + * (uses 4 bytes between RTP header and H.261 stream per packet) + * + * 0 1 2 3 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * |SBIT |EBIT |I|V| GOBN | MBAP | QUANT | HMVD | VMVD | + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + * + * Start bit position (SBIT): 3 bits + * End bit position (EBIT): 3 bits + * INTRA-frame encoded data (I): 1 bit + * Motion Vector flag (V): 1 bit + * GOB number (GOBN): 4 bits + * Macroblock address predictor (MBAP): 5 bits + * Quantizer (QUANT): 5 bits + * Horizontal motion vector data (HMVD): 5 bits + * Vertical motion vector data (VMVD): 5 bits + */ + rtp_ctx->buf[0] = 1; /* sbit=0, ebit=0, i=0, v=1 */ + rtp_ctx->buf[1] = 0; /* gobn=0, mbap=0 */ + rtp_ctx->buf[2] = 0; /* quant=0, hmvd=5 */ + rtp_ctx->buf[3] = 0; /* vmvd=0 */ + if (frame_size < 2 || frame_buf[0] != 0 || frame_buf[1] != 1) { + /* A full, correct fix for this would be to make the H261 encoder + * support inserting extra GOB headers (triggered by setting e.g. + * "-ps 1"), and including information about macroblock boundaries + * (such as for h263_rfc2190). */ + av_log(ctx, AV_LOG_WARNING, + "RTP/H261 packet not cut at a GOB boundary, not signaled correctly\n"); + } - //XXX: parse the h.261 bitstream and improve frame splitting here + cur_frame_size = FFMIN(rtp_ctx->max_payload_size - RTP_H261_HEADER_SIZE, frame_size); - last_packet_of_frame = (processed_frame_size == frame_size); + /* look for a better place to split the frame into packets */ + if (cur_frame_size < frame_size) { + const uint8_t *packet_end = find_resync_marker_reverse(frame_buf, + frame_buf + cur_frame_size); + cur_frame_size = packet_end - frame_buf; + } - memcpy(tmp_buf_ptr, frame_buf, processed_frame_size); - tmp_buf_ptr += processed_frame_size; + /* calculate the "marker" bit for the RTP header */ + last_packet_of_frame = cur_frame_size == frame_size; - ff_rtp_send_data(s1, rtp_ctx->buf, tmp_buf_ptr - rtp_ctx->buf, last_packet_of_frame); + /* complete and send RTP packet */ + memcpy(&rtp_ctx->buf[RTP_H261_HEADER_SIZE], frame_buf, cur_frame_size); + ff_rtp_send_data(ctx, rtp_ctx->buf, RTP_H261_HEADER_SIZE + cur_frame_size, last_packet_of_frame); - frame_buf += processed_frame_size; - frame_size -= processed_frame_size; + frame_buf += cur_frame_size; + frame_size -= cur_frame_size; } } diff --git a/libavformat/sdp.c b/libavformat/sdp.c index fadabbe5ae..4d621c7ffc 100644 --- a/libavformat/sdp.c +++ b/libavformat/sdp.c @@ -504,9 +504,10 @@ static char *sdp_write_media_attributes(char *buff, int size, AVCodecContext *c, /* only QCIF and CIF are specified as supported in RFC 4587 */ if (c->width == 176 && c->height == 144) pic_fmt = "QCIF=1"; - if (c->width == 352 && c->height == 288) + else if (c->width == 352 && c->height == 288) pic_fmt = "CIF=1"; - av_strlcatf(buff, size, "a=rtpmap:%d H261/90000\r\n", payload_type); + if (payload_type >= RTP_PT_PRIVATE) + av_strlcatf(buff, size, "a=rtpmap:%d H261/90000\r\n", payload_type); if (pic_fmt) av_strlcatf(buff, size, "a=fmtp:%d %s\r\n", payload_type, pic_fmt); break; diff --git a/libavformat/version.h b/libavformat/version.h index d39dd5d783..d3ce0bf8c5 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -31,7 +31,7 @@ #define LIBAVFORMAT_VERSION_MAJOR 56 #define LIBAVFORMAT_VERSION_MINOR 15 -#define LIBAVFORMAT_VERSION_MICRO 106 +#define LIBAVFORMAT_VERSION_MICRO 107 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \