Libav
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 Libav.
12  *
13  * Libav 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  * Libav 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 Libav; 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 "rtpdec_formats.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavcodec/get_bits.h"
33 
34 struct PayloadContext {
38  uint32_t timestamp;
39  int newformat;
40 };
41 
43 {
44  return av_mallocz(sizeof(PayloadContext));
45 }
46 
48 {
49  if (!data)
50  return;
51  if (data->buf) {
52  uint8_t *p;
53  avio_close_dyn_buf(data->buf, &p);
54  av_free(p);
55  }
56  av_free(data);
57 }
58 
59 static av_cold int h263_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
60 {
61  if (st_index < 0)
62  return 0;
63  ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
64  return 0;
65 }
66 
68  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
69  const uint8_t *buf, int len, uint16_t seq,
70  int flags)
71 {
72  /* Corresponding to header fields in the RFC */
73  int f, p, i, sbit, ebit, src, r;
74  int header_size, ret;
75 
76  if (data->newformat)
77  return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
78  seq, flags);
79 
80  if (data->buf && data->timestamp != *timestamp) {
81  /* Dropping old buffered, unfinished data */
82  uint8_t *p;
83  avio_close_dyn_buf(data->buf, &p);
84  av_free(p);
85  data->buf = NULL;
86  data->endbyte_bits = 0;
87  }
88 
89  if (len < 4) {
90  av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
91  return AVERROR_INVALIDDATA;
92  }
93 
94  f = buf[0] & 0x80;
95  p = buf[0] & 0x40;
96  if (!f) {
97  /* Mode A */
98  header_size = 4;
99  i = buf[1] & 0x10;
100  r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
101  } else if (!p) {
102  /* Mode B */
103  header_size = 8;
104  if (len < header_size) {
105  av_log(ctx, AV_LOG_ERROR,
106  "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
107  len, header_size);
108  return AVERROR_INVALIDDATA;
109  }
110  r = buf[3] & 0x03;
111  i = buf[4] & 0x80;
112  } else {
113  /* Mode C */
114  header_size = 12;
115  if (len < header_size) {
116  av_log(ctx, AV_LOG_ERROR,
117  "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
118  len, header_size);
119  return AVERROR_INVALIDDATA;
120  }
121  r = buf[3] & 0x03;
122  i = buf[4] & 0x80;
123  }
124  sbit = (buf[0] >> 3) & 0x7;
125  ebit = buf[0] & 0x7;
126  src = (buf[1] & 0xe0) >> 5;
127  if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
128  if ((src == 0 || src >= 6) && r) {
129  /* Invalid src for this format, and bits that should be zero
130  * according to RFC 2190 aren't zero. */
131  av_log(ctx, AV_LOG_WARNING,
132  "Interpreting H263 RTP data as RFC 2429/4629 even though "
133  "signalled with a static payload type.\n");
134  data->newformat = 1;
135  return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
136  len, seq, flags);
137  }
138  }
139 
140  buf += header_size;
141  len -= header_size;
142 
143  if (!data->buf) {
144  /* Check the picture start code, only start buffering a new frame
145  * if this is correct */
146  if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
147  ret = avio_open_dyn_buf(&data->buf);
148  if (ret < 0)
149  return ret;
150  data->timestamp = *timestamp;
151  } else {
152  /* Frame not started yet, skipping */
153  return AVERROR(EAGAIN);
154  }
155  }
156 
157  if (data->endbyte_bits || sbit) {
158  if (data->endbyte_bits == sbit) {
159  data->endbyte |= buf[0] & (0xff >> sbit);
160  data->endbyte_bits = 0;
161  buf++;
162  len--;
163  avio_w8(data->buf, data->endbyte);
164  } else {
165  /* Start/end skip bits not matching - missed packets? */
166  GetBitContext gb;
167  init_get_bits(&gb, buf, len*8 - ebit);
168  skip_bits(&gb, sbit);
169  if (data->endbyte_bits) {
170  data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
171  avio_w8(data->buf, data->endbyte);
172  }
173  while (get_bits_left(&gb) >= 8)
174  avio_w8(data->buf, get_bits(&gb, 8));
175  data->endbyte_bits = get_bits_left(&gb);
176  if (data->endbyte_bits)
177  data->endbyte = get_bits(&gb, data->endbyte_bits) <<
178  (8 - data->endbyte_bits);
179  ebit = 0;
180  len = 0;
181  }
182  }
183  if (ebit) {
184  if (len > 0)
185  avio_write(data->buf, buf, len - 1);
186  data->endbyte_bits = 8 - ebit;
187  data->endbyte = buf[len - 1] & (0xff << ebit);
188  } else {
189  avio_write(data->buf, buf, len);
190  }
191 
192  if (!(flags & RTP_FLAG_MARKER))
193  return AVERROR(EAGAIN);
194 
195  if (data->endbyte_bits)
196  avio_w8(data->buf, data->endbyte);
197  data->endbyte_bits = 0;
198 
199  ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
200  if (ret < 0)
201  return ret;
202  if (!i)
203  pkt->flags |= AV_PKT_FLAG_KEY;
204 
205  return 0;
206 }
207 
210  .codec_id = AV_CODEC_ID_H263,
211  .init = h263_init,
212  .parse_packet = h263_handle_packet,
213  .alloc = h263_new_context,
214  .free = h263_free_context,
215  .static_payload_id = 34,
216 };
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)
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
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:36
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:966
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static void h263_free_context(PayloadContext *data)
RTP/JPEG specific private data.
Definition: rdt.c:83
int index
stream index in AVFormatContext
Definition: avformat.h:700
uint8_t * buf
the temporary storage buffer
Definition: rtpdec_asf.c:170
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:954
enum AVMediaType codec_type
Definition: rtpdec.h:117
Macro definitions for various function/variable attributes.
Format I/O context.
Definition: avformat.h:922
uint8_t
#define av_cold
Definition: attributes.h:66
#define AV_RB32
Definition: intreadwrite.h:130
enum AVStreamParseType need_parsing
Definition: avformat.h:867
static PayloadContext * h263_new_context(void)
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
const char data[16]
Definition: mxf.c:70
static int flags
Definition: log.c:44
bitstream reader API header.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
#define r
Definition: input.c:51
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:555
uint32_t timestamp
current frame timestamp
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:93
#define AVERROR(e)
Definition: error.h:43
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
AVIOContext * buf
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
RTPDynamicProtocolHandler ff_h263_rfc2190_dynamic_handler
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:144
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
full parsing and repack
Definition: avformat.h:653
Main libavformat public API header.
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:867
int len
static av_cold int h263_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
This structure stores compressed data.
Definition: avcodec.h:950
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205