|
|
@ -19,11 +19,11 @@ |
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "libavcodec/get_bits.h" |
|
|
|
#include "avformat.h" |
|
|
|
#include "avformat.h" |
|
|
|
#include "rtpdec_formats.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 { |
|
|
|
struct PayloadContext { |
|
|
|
AVIOContext *buf; |
|
|
|
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, |
|
|
|
static av_cold int h261_init(AVFormatContext *ctx, int st_index, |
|
|
|
PayloadContext *data) |
|
|
|
PayloadContext *data) |
|
|
|
{ |
|
|
|
{ |
|
|
|
//av_log(ctx, AV_LOG_DEBUG, "h261_init() for stream %d\n", st_index);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (st_index < 0) |
|
|
|
if (st_index < 0) |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
|
@ -73,16 +71,18 @@ static av_cold int h261_init(AVFormatContext *ctx, int st_index, |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data, |
|
|
|
static int h261_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_h261_ctx, |
|
|
|
AVStream *st, AVPacket *pkt, uint32_t *timestamp, |
|
|
|
AVStream *st, AVPacket *pkt, uint32_t *timestamp, |
|
|
|
const uint8_t *buf, int len, uint16_t seq, int flags) |
|
|
|
const uint8_t *buf, int len, uint16_t seq, |
|
|
|
|
|
|
|
int flags) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int sbit, ebit, gobn, mbap, quant; |
|
|
|
int sbit, ebit, gobn, mbap, quant; |
|
|
|
int res; |
|
|
|
int res; |
|
|
|
|
|
|
|
|
|
|
|
/* drop data of previous packets in case of non-continuous (loss) packet stream */ |
|
|
|
/* drop data of previous packets in case of non-continuous (lossy) packet stream */ |
|
|
|
if (data->buf && data->timestamp != *timestamp) { |
|
|
|
if (rtp_h261_ctx->buf && rtp_h261_ctx->timestamp != *timestamp) { |
|
|
|
h261_free_dyn_buffer(&data->buf); |
|
|
|
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 */ |
|
|
|
/* 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: |
|
|
|
* 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) |
|
|
|
* (uses 4 bytes between RTP header and H.261 stream per packet) |
|
|
|
|
|
|
|
* |
|
|
|
0 1 2 3 |
|
|
|
* 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 |
|
|
|
* 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 | |
|
|
|
* |SBIT |EBIT |I|V| GOBN | MBAP | QUANT | HMVD | VMVD | |
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
|
|
|
|
|
|
|
* |
|
|
|
Start bit position (SBIT): 3 bits |
|
|
|
* Start bit position (SBIT): 3 bits |
|
|
|
End bit position (EBIT): 3 bits |
|
|
|
* End bit position (EBIT): 3 bits |
|
|
|
INTRA-frame encoded data (I): 1 bit |
|
|
|
* INTRA-frame encoded data (I): 1 bit |
|
|
|
Motion Vector flag (V): 1 bit |
|
|
|
* Motion Vector flag (V): 1 bit |
|
|
|
GOB number (GOBN): 4 bits |
|
|
|
* GOB number (GOBN): 4 bits |
|
|
|
Macroblock address predictor (MBAP): 5 bits |
|
|
|
* Macroblock address predictor (MBAP): 5 bits |
|
|
|
Quantizer (QUANT): 5 bits |
|
|
|
* Quantizer (QUANT): 5 bits |
|
|
|
Horizontal motion vector data (HMVD): 5 bits |
|
|
|
* Horizontal motion vector data (HMVD): 5 bits |
|
|
|
Vertical motion vector data (VMVD): 5 bits |
|
|
|
* Vertical motion vector data (VMVD): 5 bits |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
sbit = (buf[0] >> 5) & 0x07; |
|
|
|
sbit = (buf[0] >> 5) & 0x07; |
|
|
|
ebit = (buf[0] >> 2) & 0x07; |
|
|
|
ebit = (buf[0] >> 2) & 0x07; |
|
|
|
gobn = (buf[1] >> 4) & 0x0f; |
|
|
|
gobn = (buf[1] >> 4) & 0x0f; |
|
|
@ -123,14 +122,14 @@ int ff_h261_handle_packet(AVFormatContext *ctx, PayloadContext *data, |
|
|
|
len -= RTP_H261_PAYLOAD_HEADER_SIZE; |
|
|
|
len -= RTP_H261_PAYLOAD_HEADER_SIZE; |
|
|
|
|
|
|
|
|
|
|
|
/* start frame buffering with new dynamic buffer */ |
|
|
|
/* start frame buffering with new dynamic buffer */ |
|
|
|
if (!data->buf) { |
|
|
|
if (!rtp_h261_ctx->buf) { |
|
|
|
/* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, uqnat=0 */ |
|
|
|
/* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, quant=0 */ |
|
|
|
if (!gobn && !sbit && !mbap && !quant){ |
|
|
|
if (!gobn && !sbit && !mbap && !quant) { |
|
|
|
res = avio_open_dyn_buf(&data->buf); |
|
|
|
res = avio_open_dyn_buf(&rtp_h261_ctx->buf); |
|
|
|
if (res < 0) |
|
|
|
if (res < 0) |
|
|
|
return res; |
|
|
|
return res; |
|
|
|
/* update the timestamp in the frame packet with the one from the RTP packet */ |
|
|
|
/* update the timestamp in the frame packet with the one from the RTP packet */ |
|
|
|
data->timestamp = *timestamp; |
|
|
|
rtp_h261_ctx->timestamp = *timestamp; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
/* frame not started yet, need more packets */ |
|
|
|
/* frame not started yet, need more packets */ |
|
|
|
return AVERROR(EAGAIN); |
|
|
|
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 */ |
|
|
|
/* do the "byte merging" at the boundaries of two consecutive frame fragments */ |
|
|
|
if (data->endbyte_bits || sbit) { |
|
|
|
if (rtp_h261_ctx->endbyte_bits || sbit) { |
|
|
|
if (data->endbyte_bits == sbit) { |
|
|
|
if (rtp_h261_ctx->endbyte_bits == sbit) { |
|
|
|
data->endbyte |= buf[0] & (0xff >> sbit); |
|
|
|
rtp_h261_ctx->endbyte |= buf[0] & (0xff >> sbit); |
|
|
|
data->endbyte_bits = 0; |
|
|
|
rtp_h261_ctx->endbyte_bits = 0; |
|
|
|
buf++; |
|
|
|
buf++; |
|
|
|
len--; |
|
|
|
len--; |
|
|
|
avio_w8(data->buf, data->endbyte); |
|
|
|
avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
/* ebit/sbit values inconsistent, assuming packet loss */ |
|
|
|
/* ebit/sbit values inconsistent, assuming packet loss */ |
|
|
|
GetBitContext gb; |
|
|
|
GetBitContext gb; |
|
|
|
init_get_bits(&gb, buf, len*8 - ebit); |
|
|
|
init_get_bits(&gb, buf, len*8 - ebit); |
|
|
|
skip_bits(&gb, sbit); |
|
|
|
skip_bits(&gb, sbit); |
|
|
|
if (data->endbyte_bits) { |
|
|
|
if (rtp_h261_ctx->endbyte_bits) { |
|
|
|
data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits); |
|
|
|
rtp_h261_ctx->endbyte |= get_bits(&gb, 8 - rtp_h261_ctx->endbyte_bits); |
|
|
|
avio_w8(data->buf, data->endbyte); |
|
|
|
avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte); |
|
|
|
} |
|
|
|
} |
|
|
|
while (get_bits_left(&gb) >= 8) |
|
|
|
while (get_bits_left(&gb) >= 8) |
|
|
|
avio_w8(data->buf, get_bits(&gb, 8)); |
|
|
|
avio_w8(rtp_h261_ctx->buf, get_bits(&gb, 8)); |
|
|
|
data->endbyte_bits = get_bits_left(&gb); |
|
|
|
rtp_h261_ctx->endbyte_bits = get_bits_left(&gb); |
|
|
|
if (data->endbyte_bits) |
|
|
|
if (rtp_h261_ctx->endbyte_bits) |
|
|
|
data->endbyte = get_bits(&gb, data->endbyte_bits) << |
|
|
|
rtp_h261_ctx->endbyte = get_bits(&gb, rtp_h261_ctx->endbyte_bits) << |
|
|
|
(8 - data->endbyte_bits); |
|
|
|
(8 - rtp_h261_ctx->endbyte_bits); |
|
|
|
ebit = 0; |
|
|
|
ebit = 0; |
|
|
|
len = 0; |
|
|
|
len = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (ebit) { |
|
|
|
if (ebit) { |
|
|
|
if (len > 0) |
|
|
|
if (len > 0) |
|
|
|
avio_write(data->buf, buf, len - 1); |
|
|
|
avio_write(rtp_h261_ctx->buf, buf, len - 1); |
|
|
|
data->endbyte_bits = 8 - ebit; |
|
|
|
rtp_h261_ctx->endbyte_bits = 8 - ebit; |
|
|
|
data->endbyte = buf[len - 1] & (0xff << ebit); |
|
|
|
rtp_h261_ctx->endbyte = buf[len - 1] & (0xff << ebit); |
|
|
|
} else { |
|
|
|
} 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;
|
|
|
|
/* 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); |
|
|
|
return AVERROR(EAGAIN); |
|
|
|
|
|
|
|
|
|
|
|
/* write the completed last byte from the "byte merging" */ |
|
|
|
/* write the completed last byte from the "byte merging" */ |
|
|
|
if (data->endbyte_bits) |
|
|
|
if (rtp_h261_ctx->endbyte_bits) |
|
|
|
avio_w8(data->buf, data->endbyte); |
|
|
|
avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte); |
|
|
|
data->endbyte_bits = 0; |
|
|
|
rtp_h261_ctx->endbyte_bits = 0; |
|
|
|
|
|
|
|
|
|
|
|
/* close frame buffering and create resulting A/V packet */ |
|
|
|
/* 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) |
|
|
|
if (res < 0) |
|
|
|
return res; |
|
|
|
return res; |
|
|
|
|
|
|
|
|
|
|
@ -196,8 +195,8 @@ RTPDynamicProtocolHandler ff_h261_dynamic_handler = { |
|
|
|
.codec_type = AVMEDIA_TYPE_VIDEO, |
|
|
|
.codec_type = AVMEDIA_TYPE_VIDEO, |
|
|
|
.codec_id = AV_CODEC_ID_H261, |
|
|
|
.codec_id = AV_CODEC_ID_H261, |
|
|
|
.init = h261_init, |
|
|
|
.init = h261_init, |
|
|
|
.parse_packet = ff_h261_handle_packet, |
|
|
|
|
|
|
|
.alloc = h261_new_context, |
|
|
|
.alloc = h261_new_context, |
|
|
|
.free = h261_free_context, |
|
|
|
.free = h261_free_context, |
|
|
|
|
|
|
|
.parse_packet = h261_handle_packet, |
|
|
|
.static_payload_id = 31, |
|
|
|
.static_payload_id = 31, |
|
|
|
}; |
|
|
|
}; |
|
|
|