FFmpeg
rtpdec_h261.c
Go to the documentation of this file.
1 /*
2  * RTP parser for H.261 payload format (RFC 4587)
3  * Copyright (c) 2014 Thomas Volkert <thomas@homer-conferencing.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavcodec/get_bits.h"
23 #include "avformat.h"
24 #include "avio_internal.h"
25 #include "rtpdec_formats.h"
26 
27 #define RTP_H261_PAYLOAD_HEADER_SIZE 4
28 
29 struct PayloadContext {
31  uint8_t endbyte;
33  uint32_t timestamp;
34 };
35 
37 {
38  /* return if context is invalid */
39  if (!pl_ctx)
40  return;
41 
42  /* free buffer if it is valid */
43  ffio_free_dyn_buf(&pl_ctx->buf);
44 }
45 
47  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
48  const uint8_t *buf, int len, uint16_t seq,
49  int flags)
50 {
51  int sbit, ebit, gobn, mbap, quant;
52  int res;
53 
54  /* drop data of previous packets in case of non-continuous (lossy) packet stream */
55  if (rtp_h261_ctx->buf && rtp_h261_ctx->timestamp != *timestamp) {
56  ffio_free_dyn_buf(&rtp_h261_ctx->buf);
57  rtp_h261_ctx->endbyte_bits = 0;
58  }
59 
60  /* sanity check for size of input packet: 1 byte payload at least */
62  av_log(ctx, AV_LOG_ERROR, "Too short RTP/H.261 packet, got %d bytes\n", len);
63  return AVERROR_INVALIDDATA;
64  }
65 
66  /*
67  * decode the H.261 payload header according to section 4.1 of RFC 4587:
68  * (uses 4 bytes between RTP header and H.261 stream per packet)
69  *
70  * 0 1 2 3
71  * 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
72  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
73  * |SBIT |EBIT |I|V| GOBN | MBAP | QUANT | HMVD | VMVD |
74  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
75  *
76  * Start bit position (SBIT): 3 bits
77  * End bit position (EBIT): 3 bits
78  * INTRA-frame encoded data (I): 1 bit
79  * Motion Vector flag (V): 1 bit
80  * GOB number (GOBN): 4 bits
81  * Macroblock address predictor (MBAP): 5 bits
82  * Quantizer (QUANT): 5 bits
83  * Horizontal motion vector data (HMVD): 5 bits
84  * Vertical motion vector data (VMVD): 5 bits
85  */
86  sbit = (buf[0] >> 5) & 0x07;
87  ebit = (buf[0] >> 2) & 0x07;
88  gobn = (buf[1] >> 4) & 0x0f;
89  mbap = ((buf[1] << 1) & 0x1e) | ((buf[2] >> 7) & 0x01);
90  quant = (buf[2] >> 2) & 0x1f;
91 
92  /* pass the H.261 payload header and continue with the actual payload */
95 
96  /* start frame buffering with new dynamic buffer */
97  if (!rtp_h261_ctx->buf) {
98  /* sanity check: a new frame starts with gobn=0, sbit=0, mbap=0, quant=0 */
99  if (!gobn && !sbit && !mbap && !quant) {
100  res = avio_open_dyn_buf(&rtp_h261_ctx->buf);
101  if (res < 0)
102  return res;
103  /* update the timestamp in the frame packet with the one from the RTP packet */
104  rtp_h261_ctx->timestamp = *timestamp;
105  } else {
106  /* frame not started yet, need more packets */
107  return AVERROR(EAGAIN);
108  }
109  }
110 
111  /* do the "byte merging" at the boundaries of two consecutive frame fragments */
112  if (rtp_h261_ctx->endbyte_bits || sbit) {
113  if (rtp_h261_ctx->endbyte_bits == sbit) {
114  rtp_h261_ctx->endbyte |= buf[0] & (0xff >> sbit);
115  rtp_h261_ctx->endbyte_bits = 0;
116  buf++;
117  len--;
118  avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
119  } else {
120  /* ebit/sbit values inconsistent, assuming packet loss */
121  GetBitContext gb;
122  res = init_get_bits(&gb, buf, len*8 - ebit);
123  if (res < 0)
124  return res;
125  skip_bits(&gb, sbit);
126  if (rtp_h261_ctx->endbyte_bits) {
127  rtp_h261_ctx->endbyte |= get_bits(&gb, 8 - rtp_h261_ctx->endbyte_bits);
128  avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
129  }
130  while (get_bits_left(&gb) >= 8)
131  avio_w8(rtp_h261_ctx->buf, get_bits(&gb, 8));
132  rtp_h261_ctx->endbyte_bits = get_bits_left(&gb);
133  if (rtp_h261_ctx->endbyte_bits)
134  rtp_h261_ctx->endbyte = get_bits(&gb, rtp_h261_ctx->endbyte_bits) <<
135  (8 - rtp_h261_ctx->endbyte_bits);
136  ebit = 0;
137  len = 0;
138  }
139  }
140  if (ebit) {
141  if (len > 0)
142  avio_write(rtp_h261_ctx->buf, buf, len - 1);
143  rtp_h261_ctx->endbyte_bits = 8 - ebit;
144  rtp_h261_ctx->endbyte = buf[len - 1] & (0xff << ebit);
145  } else {
146  avio_write(rtp_h261_ctx->buf, buf, len);
147  }
148 
149  /* RTP marker bit means: last fragment of current frame was received;
150  otherwise, an additional fragment is needed for the current frame */
151  if (!(flags & RTP_FLAG_MARKER))
152  return AVERROR(EAGAIN);
153 
154  /* write the completed last byte from the "byte merging" */
155  if (rtp_h261_ctx->endbyte_bits)
156  avio_w8(rtp_h261_ctx->buf, rtp_h261_ctx->endbyte);
157  rtp_h261_ctx->endbyte_bits = 0;
158 
159  /* close frame buffering and create resulting A/V packet */
160  res = ff_rtp_finalize_packet(pkt, &rtp_h261_ctx->buf, st->index);
161  if (res < 0)
162  return res;
163 
164  return 0;
165 }
166 
168  .enc_name = "H261",
169  .codec_type = AVMEDIA_TYPE_VIDEO,
170  .codec_id = AV_CODEC_ID_H261,
171  .need_parsing = AVSTREAM_PARSE_FULL,
172  .priv_data_size = sizeof(PayloadContext),
173  .close = h261_close_context,
175  .static_payload_id = 31,
176 };
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:839
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
rtpdec_formats.h
RTP_FLAG_MARKER
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:94
ff_h261_dynamic_handler
const RTPDynamicProtocolHandler ff_h261_dynamic_handler
Definition: rtpdec_h261.c:167
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:649
AV_CODEC_ID_H261
@ AV_CODEC_ID_H261
Definition: codec_id.h:53
ff_rtp_finalize_packet
int ff_rtp_finalize_packet(AVPacket *pkt, AVIOContext **dyn_buf, int stream_idx)
Close the dynamic buffer and make a packet from it.
Definition: rtpdec.c:1002
PayloadContext::timestamp
uint32_t timestamp
current frame timestamp
Definition: rtpdec_ac3.c:31
RTPDynamicProtocolHandler::enc_name
const char * enc_name
Definition: rtpdec.h:117
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
PayloadContext::endbyte
uint8_t endbyte
Definition: rtpdec_h261.c:31
GetBitContext
Definition: get_bits.h:61
quant
static int quant(float coef, const float Q, const float rounding)
Quantize one coefficient.
Definition: aacenc_utils.h:59
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1483
h261_close_context
static av_cold void h261_close_context(PayloadContext *pl_ctx)
Definition: rtpdec_h261.c:36
ctx
AVFormatContext * ctx
Definition: movenc.c:48
get_bits.h
AVFormatContext
Format I/O context.
Definition: avformat.h:1213
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:210
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
RTP_H261_PAYLOAD_HEADER_SIZE
#define RTP_H261_PAYLOAD_HEADER_SIZE
Definition: rtpdec_h261.c:27
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
avio_internal.h
PayloadContext::endbyte_bits
int endbyte_bits
Definition: rtpdec_h261.c:32
len
int len
Definition: vorbis_enc_data.h:426
ffio_free_dyn_buf
void ffio_free_dyn_buf(AVIOContext **s)
Free a dynamic buffer.
Definition: aviobuf.c:1556
AVStream
Stream structure.
Definition: avformat.h:948
avformat.h
PayloadContext::buf
uint8_t * buf
the temporary storage buffer
Definition: rtpdec_asf.c:183
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:956
parse_packet
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index, int flush)
Parse a packet, add all split parts to parse_queue.
Definition: demux.c:1118
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:798
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h261_handle_packet
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)
Definition: rtpdec_h261.c:46
PayloadContext
RTP/JPEG specific private data.
Definition: rdt.c:83
RTPDynamicProtocolHandler
Definition: rtpdec.h:116