FFmpeg
rtpdec_h263_rfc2190.c
Go to the documentation of this file.
1 /*
2  * RTP H.263 Depacketizer, RFC 2190
3  * Copyright (c) 2012 Martin Storsjo
4  * Based on the GStreamer H.263 Depayloder:
5  * Copyright 2005 Wim Taymans
6  * Copyright 2007 Edward Hervey
7  * Copyright 2007 Nokia Corporation
8  * Copyright 2007 Collabora Ltd, Philippe Kalaf
9  * Copyright 2010 Mark Nauwelaerts
10  *
11  * This file is part of FFmpeg.
12  *
13  * FFmpeg is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * FFmpeg is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with FFmpeg; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  */
27 
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "rtpdec_formats.h"
31 #include "libavutil/attributes.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavcodec/get_bits.h"
34 
35 struct PayloadContext {
37  uint8_t endbyte;
38  int endbyte_bits;
39  uint32_t timestamp;
40  int newformat;
41 };
42 
44 {
45  ffio_free_dyn_buf(&data->buf);
46 }
47 
49  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
50  const uint8_t *buf, int len, uint16_t seq,
51  int flags)
52 {
53  /* Corresponding to header fields in the RFC */
54  int f, p, i, sbit, ebit, src, r;
55  int header_size, ret;
56 
57  if (data->newformat)
58  return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
59  seq, flags);
60 
61  if (data->buf && data->timestamp != *timestamp) {
62  /* Dropping old buffered, unfinished data */
63  ffio_free_dyn_buf(&data->buf);
64  data->endbyte_bits = 0;
65  }
66 
67  if (len < 4) {
68  av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
69  return AVERROR_INVALIDDATA;
70  }
71 
72  f = buf[0] & 0x80;
73  p = buf[0] & 0x40;
74  if (!f) {
75  /* Mode A */
76  header_size = 4;
77  i = buf[1] & 0x10;
78  r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
79  } else if (!p) {
80  /* Mode B */
81  header_size = 8;
82  if (len < header_size) {
84  "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
85  len, header_size);
86  return AVERROR_INVALIDDATA;
87  }
88  r = buf[3] & 0x03;
89  i = buf[4] & 0x80;
90  } else {
91  /* Mode C */
92  header_size = 12;
93  if (len < header_size) {
95  "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
96  len, header_size);
97  return AVERROR_INVALIDDATA;
98  }
99  r = buf[3] & 0x03;
100  i = buf[4] & 0x80;
101  }
102  sbit = (buf[0] >> 3) & 0x7;
103  ebit = buf[0] & 0x7;
104  src = (buf[1] & 0xe0) >> 5;
105  if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
106  if ((src == 0 || src >= 6) && r) {
107  /* Invalid src for this format, and bits that should be zero
108  * according to RFC 2190 aren't zero. */
110  "Interpreting H.263 RTP data as RFC 2429/4629 even though "
111  "signalled with a static payload type.\n");
112  data->newformat = 1;
113  return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
114  len, seq, flags);
115  }
116  }
117 
118  buf += header_size;
119  len -= header_size;
120 
121  if (!data->buf) {
122  /* Check the picture start code, only start buffering a new frame
123  * if this is correct */
124  if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
125  ret = avio_open_dyn_buf(&data->buf);
126  if (ret < 0)
127  return ret;
128  data->timestamp = *timestamp;
129  } else {
130  /* Frame not started yet, skipping */
131  return AVERROR(EAGAIN);
132  }
133  }
134 
135  if (data->endbyte_bits || sbit) {
136  if (data->endbyte_bits == sbit) {
137  data->endbyte |= buf[0] & (0xff >> sbit);
138  data->endbyte_bits = 0;
139  buf++;
140  len--;
141  avio_w8(data->buf, data->endbyte);
142  } else {
143  /* Start/end skip bits not matching - missed packets? */
144  GetBitContext gb;
145  ret = init_get_bits(&gb, buf, len*8 - ebit);
146  if (ret < 0)
147  return ret;
148  skip_bits(&gb, sbit);
149  if (data->endbyte_bits) {
150  data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
151  avio_w8(data->buf, data->endbyte);
152  }
153  while (get_bits_left(&gb) >= 8)
154  avio_w8(data->buf, get_bits(&gb, 8));
155  data->endbyte_bits = get_bits_left(&gb);
156  if (data->endbyte_bits)
157  data->endbyte = get_bits(&gb, data->endbyte_bits) <<
158  (8 - data->endbyte_bits);
159  ebit = 0;
160  len = 0;
161  }
162  }
163  if (ebit) {
164  if (len > 0)
165  avio_write(data->buf, buf, len - 1);
166  data->endbyte_bits = 8 - ebit;
167  data->endbyte = buf[len - 1] & (0xff << ebit);
168  } else {
169  avio_write(data->buf, buf, len);
170  }
171 
172  if (!(flags & RTP_FLAG_MARKER))
173  return AVERROR(EAGAIN);
174 
175  if (data->endbyte_bits)
176  avio_w8(data->buf, data->endbyte);
177  data->endbyte_bits = 0;
178 
179  ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
180  if (ret < 0)
181  return ret;
182  if (!i)
184 
185  return 0;
186 }
187 
190  .codec_id = AV_CODEC_ID_H263,
191  .need_parsing = AVSTREAM_PARSE_FULL,
192  .parse_packet = h263_handle_packet,
193  .priv_data_size = sizeof(PayloadContext),
194  .close = h263_close_context,
195  .static_payload_id = 34,
196 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:839
r
const char * r
Definition: vf_curves.c:116
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
data
const char data[16]
Definition: mxf.c:143
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:429
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:649
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
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
ff_h263_handle_packet
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)
Definition: rtpdec_h263.c:27
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
avio_open_dyn_buf
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1483
intreadwrite.h
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
f
f
Definition: af_crystalizer.c:122
AVIOContext
Bytestream IO Context.
Definition: avio.h:162
AV_CODEC_ID_H263
@ AV_CODEC_ID_H263
Definition: codec_id.h:54
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
h263_close_context
static void h263_close_context(PayloadContext *data)
Definition: rtpdec_h263_rfc2190.c:43
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:232
attributes.h
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:380
h263_handle_packet
static int 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)
Definition: rtpdec_h263_rfc2190.c:48
ff_h263_rfc2190_dynamic_handler
const RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler
Definition: rtpdec_h263_rfc2190.c:188
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
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
ret
ret
Definition: filter_design.txt:187
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
RTPDynamicProtocolHandler::codec_type
enum AVMediaType codec_type
Definition: rtpdec.h:118
PayloadContext::newformat
int newformat
Definition: rtpdec_h263_rfc2190.c:40
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:351
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
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
PayloadContext
RTP/JPEG specific private data.
Definition: rdt.c:83
RTPDynamicProtocolHandler
Definition: rtpdec.h:116