FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/intreadwrite.h"
31 #include "libavcodec/get_bits.h"
32 
33 struct PayloadContext {
37  uint32_t timestamp;
38  int newformat;
39 };
40 
42 {
43  return av_mallocz(sizeof(PayloadContext));
44 }
45 
47 {
48  if (!data)
49  return;
50  if (data->buf) {
51  uint8_t *p;
52  avio_close_dyn_buf(data->buf, &p);
53  av_free(p);
54  }
55  av_free(data);
56 }
57 
59  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
60  const uint8_t *buf, int len, uint16_t seq,
61  int flags)
62 {
63  /* Corresponding to header fields in the RFC */
64  int f, p, i, sbit, ebit, src, r;
65  int header_size, ret;
66 
67  if (data->newformat)
68  return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf, len,
69  seq, flags);
70 
71  if (data->buf && data->timestamp != *timestamp) {
72  /* Dropping old buffered, unfinished data */
73  uint8_t *p;
74  avio_close_dyn_buf(data->buf, &p);
75  av_free(p);
76  data->buf = NULL;
77  }
78 
79  if (len < 4) {
80  av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet: %d\n", len);
81  return AVERROR_INVALIDDATA;
82  }
83 
84  f = buf[0] & 0x80;
85  p = buf[0] & 0x40;
86  if (!f) {
87  /* Mode A */
88  header_size = 4;
89  i = buf[1] & 0x10;
90  r = ((buf[1] & 0x01) << 3) | ((buf[2] & 0xe0) >> 5);
91  } else if (!p) {
92  /* Mode B */
93  header_size = 8;
94  if (len < header_size) {
95  av_log(ctx, AV_LOG_ERROR,
96  "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
97  len, header_size);
98  return AVERROR_INVALIDDATA;
99  }
100  r = buf[3] & 0x03;
101  i = buf[4] & 0x80;
102  } else {
103  /* Mode C */
104  header_size = 12;
105  if (len < header_size) {
106  av_log(ctx, AV_LOG_ERROR,
107  "Too short H.263 RTP packet: %d bytes, %d header bytes\n",
108  len, header_size);
109  return AVERROR_INVALIDDATA;
110  }
111  r = buf[3] & 0x03;
112  i = buf[4] & 0x80;
113  }
114  sbit = (buf[0] >> 3) & 0x7;
115  ebit = buf[0] & 0x7;
116  src = (buf[1] & 0xe0) >> 5;
117  if (!(buf[0] & 0xf8)) { /* Reserved bits in RFC 2429/4629 are zero */
118  if ((src == 0 || src >= 6) && r) {
119  /* Invalid src for this format, and bits that should be zero
120  * according to RFC 2190 aren't zero. */
121  av_log(ctx, AV_LOG_WARNING,
122  "Interpreting H263 RTP data as RFC 2429/4629 even though "
123  "signalled with a static payload type.\n");
124  data->newformat = 1;
125  return ff_h263_handle_packet(ctx, data, st, pkt, timestamp, buf,
126  len, seq, flags);
127  }
128  }
129 
130  buf += header_size;
131  len -= header_size;
132 
133  if (!data->buf) {
134  /* Check the picture start code, only start buffering a new frame
135  * if this is correct */
136  if (len > 4 && AV_RB32(buf) >> 10 == 0x20) {
137  ret = avio_open_dyn_buf(&data->buf);
138  if (ret < 0)
139  return ret;
140  data->timestamp = *timestamp;
141  } else {
142  /* Frame not started yet, skipping */
143  return AVERROR(EAGAIN);
144  }
145  }
146 
147  if (data->endbyte_bits || sbit) {
148  if (data->endbyte_bits == sbit) {
149  data->endbyte |= buf[0] & (0xff >> sbit);
150  data->endbyte_bits = 0;
151  buf++;
152  len--;
153  avio_w8(data->buf, data->endbyte);
154  } else {
155  /* Start/end skip bits not matching - missed packets? */
156  GetBitContext gb;
157  init_get_bits(&gb, buf, len*8 - ebit);
158  skip_bits(&gb, sbit);
159  if (data->endbyte_bits) {
160  data->endbyte |= get_bits(&gb, 8 - data->endbyte_bits);
161  avio_w8(data->buf, data->endbyte);
162  }
163  while (get_bits_left(&gb) >= 8)
164  avio_w8(data->buf, get_bits(&gb, 8));
165  data->endbyte_bits = get_bits_left(&gb);
166  if (data->endbyte_bits)
167  data->endbyte = get_bits(&gb, data->endbyte_bits) <<
168  (8 - data->endbyte_bits);
169  ebit = 0;
170  len = 0;
171  }
172  }
173  if (ebit) {
174  if (len > 0)
175  avio_write(data->buf, buf, len - 1);
176  data->endbyte_bits = 8 - ebit;
177  data->endbyte = buf[len - 1] & (0xff << ebit);
178  } else {
179  avio_write(data->buf, buf, len);
180  }
181 
182  if (!(flags & RTP_FLAG_MARKER))
183  return AVERROR(EAGAIN);
184 
185  if (data->endbyte_bits)
186  avio_w8(data->buf, data->endbyte);
187  data->endbyte_bits = 0;
188 
189  ret = ff_rtp_finalize_packet(pkt, &data->buf, st->index);
190  if (ret < 0)
191  return ret;
192  if (!i)
193  pkt->flags |= AV_PKT_FLAG_KEY;
194 
195  return 0;
196 }
197 
200  .codec_id = AV_CODEC_ID_H263,
201  .parse_packet = h263_handle_packet,
202  .alloc = h263_new_context,
203  .free = h263_free_context,
204  .static_payload_id = 34,
205 };