rtpdec_h263.c
Go to the documentation of this file.
1 /*
2  * RTP H.263 Depacketizer, RFC 4629
3  * Copyright (c) 2010 Martin Storsjo
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 "avformat.h"
23 #include "rtpdec_formats.h"
24 #include "libavutil/intreadwrite.h"
25 
26 static int h263_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
27 {
28  if (st_index < 0)
29  return 0;
30  ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
31  return 0;
32 }
33 
35  AVStream *st, AVPacket *pkt, uint32_t *timestamp,
36  const uint8_t *buf, int len, uint16_t seq, int flags)
37 {
38  uint8_t *ptr;
39  uint16_t header;
40  int startcode, vrc, picture_header;
41 
42  if (len < 2) {
43  av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n");
44  return AVERROR_INVALIDDATA;
45  }
46 
47  /* Decode the 16 bit H.263+ payload header, as described in section
48  * 5.1 of RFC 4629. The fields of this header are:
49  * - 5 reserved bits, should be ignored.
50  * - One bit (P, startcode), indicating a picture start, picture segment
51  * start or video sequence end. If set, two zero bytes should be
52  * prepended to the payload.
53  * - One bit (V, vrc), indicating the presence of an 8 bit Video
54  * Redundancy Coding field after this 16 bit header.
55  * - 6 bits (PLEN, picture_header), the length (in bytes) of an extra
56  * picture header, following the VRC field.
57  * - 3 bits (PEBIT), the number of bits to ignore of the last byte
58  * of the extra picture header. (Not used at the moment.)
59  */
60  header = AV_RB16(buf);
61  startcode = (header & 0x0400) >> 9;
62  vrc = header & 0x0200;
63  picture_header = (header & 0x01f8) >> 3;
64  buf += 2;
65  len -= 2;
66 
67  if (vrc) {
68  /* Skip VRC header if present, not used at the moment. */
69  buf += 1;
70  len -= 1;
71  }
72  if (picture_header) {
73  /* Skip extra picture header if present, not used at the moment. */
74  buf += picture_header;
75  len -= picture_header;
76  }
77 
78  if (len < 0) {
79  av_log(ctx, AV_LOG_ERROR, "Too short H.263 RTP packet\n");
80  return AVERROR_INVALIDDATA;
81  }
82 
83  if (av_new_packet(pkt, len + startcode)) {
84  av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
85  return AVERROR(ENOMEM);
86  }
87  pkt->stream_index = st->index;
88  ptr = pkt->data;
89 
90  if (startcode) {
91  *ptr++ = 0;
92  *ptr++ = 0;
93  }
94  memcpy(ptr, buf, len);
95 
96  return 0;
97 }
98 
100  .enc_name = "H263-1998",
101  .codec_type = AVMEDIA_TYPE_VIDEO,
102  .codec_id = AV_CODEC_ID_H263,
103  .init = h263_init,
104  .parse_packet = ff_h263_handle_packet,
105 };
106 
108  .enc_name = "H263-2000",
109  .codec_type = AVMEDIA_TYPE_VIDEO,
110  .codec_id = AV_CODEC_ID_H263,
111  .init = h263_init,
112  .parse_packet = ff_h263_handle_packet,
113 };
RTPDynamicProtocolHandler ff_h263_2000_dynamic_handler
Definition: rtpdec_h263.c:107
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
RTP/JPEG specific private data.
Definition: rdt.c:83
int index
stream index in AVFormatContext
Definition: avformat.h:644
Format I/O context.
Definition: avformat.h:944
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
enum AVStreamParseType need_parsing
Definition: avformat.h:811
AVStream ** streams
Definition: avformat.h:992
uint8_t * data
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
RTPDynamicProtocolHandler ff_h263_1998_dynamic_handler
Definition: rtpdec_h263.c:99
#define AV_RB16
Spectrum Plot time data
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
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:34
Stream structure.
Definition: avformat.h:643
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
const char enc_name[50]
Definition: rtpdec.h:120
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
static int flags
Definition: cpu.c:23
full parsing and repack
Definition: avformat.h:582
Main libavformat public API header.
int len
static int h263_init(AVFormatContext *ctx, int st_index, PayloadContext *data)
Definition: rtpdec_h263.c:26
This structure stores compressed data.