FFmpeg
 All Data Structures Namespaces 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 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 "rtpdec_formats.h"
30 #include "libavutil/attributes.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavcodec/get_bits.h"
33 
34 struct PayloadContext {
37  int endbyte_bits;
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 };