2014-08-23 22:25:39 +03:00
|
|
|
/*
|
|
|
|
* RTP parser for H.261 payload format (RFC 4587)
|
|
|
|
* Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2014-12-06 20:54:07 +02:00
|
|
|
#include "libavcodec/get_bits.h"
|
2014-08-23 22:25:39 +03:00
|
|
|
#include "avformat.h"
|
2015-02-24 13:37:03 +02:00
|
|
|
#include "avio_internal.h"
|
2014-08-23 22:25:39 +03:00
|
|
|
#include "rtpdec_formats.h"
|
|
|
|
|
2014-12-06 20:54:07 +02:00
|
|
|
#define RTP_H261_PAYLOAD_HEADER_SIZE 4
|
2014-08-23 22:25:39 +03:00
|
|
|
|
|
|
|
struct PayloadContext {
|
|
|
|
AVIOContext *buf;
|
|
|
|
uint8_t endbyte;
|
|
|
|
int endbyte_bits;
|
|
|
|
uint32_t timestamp;
|
|
|
|
};
|
|
|
|
|
2015-02-24 17:01:48 +02:00
|
|
|
static av_cold void h261_close_context(PayloadContext *pl_ctx)
|
2014-08-23 22:25:39 +03:00
|
|
|
{
|
|
|
|
/* return if context is invalid */
|
|
|
|
if (!pl_ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* free buffer if it is valid */
|
2015-02-24 13:37:03 +02:00
|
|
|
ffio_free_dyn_buf(&pl_ctx->buf);
|
2014-08-23 22:25:39 +03:00
|
|
|
}
|
|
|
|
|
2014-12-06 20:54:07 +02:00
|
|
|
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)
|
2014-08-23 22:25:39 +03:00
|
|
|
{
|
|
|
|
int sbit, ebit, gobn, mbap, quant;
|
|
|
|
int res;
|
|
|
|
|
2014-12-06 20:54:07 +02:00
|
|
|
/* drop data of previous packets in case of non-continuous (lossy) packet stream */
|
|
|
|
if (rtp_h261_ctx->buf && rtp_h261_ctx->timestamp != *timestamp) {
|
2015-02-24 13:37:03 +02:00
|
|
|
ffio_free_dyn_buf(&rtp_h261_ctx->buf);
|
2014-12-06 20:54:07 +02:00
|
|
|
rtp_h261_ctx->endbyte_bits = 0;
|
2014-08-23 22:25:39 +03:00
|
|
|
}
|
|
|
|
|
2014-11-01 14:01:53 +02:00
|
|
|
/* sanity check for size of input packet: 1 byte payload at least */
|
|
|
|
if (len < RTP_H261_PAYLOAD_HEADER_SIZE + 1) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Too short RTP/H.261 packet, got %d bytes\n", len);
|
2014-08-23 22:25:39 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-12-06 20:54:07 +02:00
|
|
|
* 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
|
|
|
|
*/
|
2014-08-23 22:25:39 +03:00
|
|
|
sbit = (buf[0] >> 5) & 0x07;
|
|
|
|
ebit = (buf[0] >> 2) & 0x07;
|
|
|
|
gobn = (buf[1] >> 4) & 0x0f;
|
2014-08-26 15:23:00 +03:00
|
|
|
mbap = ((buf[1] << 1) & 0x1e) | ((buf[2] >> 7) & 0x01);
|
|
|
|
quant = (buf[2] >> 2) & 0x1f;
|
2014-08-23 22:25:39 +03:00
|
|
|
|
|
|
|
/* pass the H.261 payload header and continue with the actual payload */
|
|
|
|
buf += RTP_H261_PAYLOAD_HEADER_SIZE;
|
|
|
|
len -= RTP_H261_PAYLOAD_HEADER_SIZE;
|
|
|
|
|
|
|
|
/* start frame buffering with new dynamic buffer */
|
2014-12-06 20:54:07 +02:00
|
|
|
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);
|
2014-08-23 22:25:39 +03:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
/* update the timestamp in the frame packet with the one from the RTP packet */
|
2014-12-06 20:54:07 +02:00
|
|
|
rtp_h261_ctx->timestamp = *timestamp;
|
2014-08-23 22:25:39 +03:00
|
|
|
} else {
|
|
|
|
/* frame not started yet, need more packets */
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do the "byte merging" at the boundaries of two consecutive frame fragments */
|
2014-12-06 20:54:07 +02:00
|
|
|
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;
|
2014-08-23 22:25:39 +03:00
|
|
|
buf++;
|
|
|
|
len--;
|
2014-12-06 20:54:07 +02:00
|
|
|
avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
|
2014-08-23 22:25:39 +03:00
|
|
|
} else {
|
|
|
|
/* ebit/sbit values inconsistent, assuming packet loss */
|
|
|
|
GetBitContext gb;
|
|
|
|
init_get_bits(&gb, buf, len*8 - ebit);
|
|
|
|
skip_bits(&gb, sbit);
|
2014-12-06 20:54:07 +02:00
|
|
|
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);
|
2014-08-23 22:25:39 +03:00
|
|
|
}
|
|
|
|
while (get_bits_left(&gb) >= 8)
|
2014-12-06 20:54:07 +02:00
|
|
|
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);
|
2014-08-23 22:25:39 +03:00
|
|
|
ebit = 0;
|
2014-12-06 20:54:07 +02:00
|
|
|
len = 0;
|
2014-08-23 22:25:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ebit) {
|
|
|
|
if (len > 0)
|
2014-12-06 20:54:07 +02:00
|
|
|
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);
|
2014-08-23 22:25:39 +03:00
|
|
|
} else {
|
2014-12-06 20:54:07 +02:00
|
|
|
avio_write(rtp_h261_ctx->buf, buf, len);
|
2014-08-23 22:25:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* RTP marker bit means: last fragment of current frame was received;
|
|
|
|
otherwise, an additional fragment is needed for the current frame */
|
|
|
|
if (!(flags & RTP_FLAG_MARKER))
|
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
|
|
|
|
/* write the completed last byte from the "byte merging" */
|
2014-12-06 20:54:07 +02:00
|
|
|
if (rtp_h261_ctx->endbyte_bits)
|
|
|
|
avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
|
|
|
|
rtp_h261_ctx->endbyte_bits = 0;
|
2014-08-23 22:25:39 +03:00
|
|
|
|
|
|
|
/* close frame buffering and create resulting A/V packet */
|
2014-12-06 20:54:07 +02:00
|
|
|
res = ff_rtp_finalize_packet(pkt, &rtp_h261_ctx->buf, st->index);
|
2014-08-23 22:25:39 +03:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:21:06 +02:00
|
|
|
const RTPDynamicProtocolHandler ff_h261_dynamic_handler = {
|
2014-08-23 22:25:39 +03:00
|
|
|
.enc_name = "H261",
|
|
|
|
.codec_type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.codec_id = AV_CODEC_ID_H261,
|
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),
|
2015-02-24 17:01:48 +02:00
|
|
|
.close = h261_close_context,
|
2014-12-06 20:54:07 +02:00
|
|
|
.parse_packet = h261_handle_packet,
|
2014-08-23 22:25:39 +03:00
|
|
|
.static_payload_id = 31,
|
|
|
|
};
|