rtpdec_vp8.c
Go to the documentation of this file.
1 /*
2  * RTP VP8 Depacketizer
3  * Copyright (c) 2010 Josh Allmann
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
29 #include "libavcodec/bytestream.h"
30 
31 #include "rtpdec_formats.h"
32 
33 struct PayloadContext {
35  uint32_t timestamp;
37 };
38 
39 static void prepare_packet(AVPacket *pkt, PayloadContext *vp8, int stream)
40 {
41  av_init_packet(pkt);
42  pkt->stream_index = stream;
43  pkt->flags = vp8->is_keyframe ? AV_PKT_FLAG_KEY : 0;
44  pkt->size = avio_close_dyn_buf(vp8->data, &pkt->data);
46  vp8->data = NULL;
47 }
48 
50  PayloadContext *vp8,
51  AVStream *st,
52  AVPacket *pkt,
53  uint32_t *timestamp,
54  const uint8_t *buf,
55  int len, int flags)
56 {
57  int start_packet, end_packet, has_au, ret = AVERROR(EAGAIN);
58 
59  if (!buf) {
60  // only called when vp8_handle_packet returns 1
61  if (!vp8->data) {
62  av_log(ctx, AV_LOG_ERROR, "Invalid VP8 data passed\n");
63  return AVERROR_INVALIDDATA;
64  }
65  prepare_packet(pkt, vp8, st->index);
66  *timestamp = vp8->timestamp;
67  return 0;
68  }
69 
70  start_packet = *buf & 1;
71  end_packet = flags & RTP_FLAG_MARKER;
72  has_au = *buf & 2;
73  buf++;
74  len--;
75 
76  if (start_packet) {
77  int res;
78  uint32_t ts = *timestamp;
79  if (vp8->data) {
80  // missing end marker; return old frame anyway. untested
81  prepare_packet(pkt, vp8, st->index);
82  *timestamp = vp8->timestamp; // reset timestamp from old frame
83 
84  // if current frame fits into one rtp packet, need to hold
85  // that for the next av_get_packet call
86  ret = end_packet ? 1 : 0;
87  }
88  if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
89  return res;
90  vp8->is_keyframe = *buf & 1;
91  vp8->timestamp = ts;
92  }
93 
94  if (!vp8->data || vp8->timestamp != *timestamp && ret == AVERROR(EAGAIN)) {
96  "Received no start marker; dropping frame\n");
97  return AVERROR(EAGAIN);
98  }
99 
100  // cycle through VP8AU headers if needed
101  // not tested with actual VP8AUs
102  while (len) {
103  int au_len = len;
104  if (has_au && len > 2) {
105  au_len = AV_RB16(buf);
106  buf += 2;
107  len -= 2;
108  if (buf + au_len > buf + len) {
109  av_log(ctx, AV_LOG_ERROR, "Invalid VP8AU length\n");
110  return AVERROR_INVALIDDATA;
111  }
112  }
113 
114  avio_write(vp8->data, buf, au_len);
115  buf += au_len;
116  len -= au_len;
117  }
118 
119  if (ret != AVERROR(EAGAIN)) // did we miss a end marker?
120  return ret;
121 
122  if (end_packet) {
123  prepare_packet(pkt, vp8, st->index);
124  return 0;
125  }
126 
127  return AVERROR(EAGAIN);
128 }
129 
131 {
132  av_log(NULL, AV_LOG_ERROR, "RTP VP8 payload implementation is incompatible "
133  "with the latest spec drafts.\n");
134  return av_mallocz(sizeof(PayloadContext));
135 }
136 
138 {
139  if (vp8->data) {
140  uint8_t *tmp;
141  avio_close_dyn_buf(vp8->data, &tmp);
142  av_free(tmp);
143  }
144  av_free(vp8);
145 }
146 
148  .enc_name = "VP8",
149  .codec_type = AVMEDIA_TYPE_VIDEO,
150  .codec_id = CODEC_ID_VP8,
151  .alloc = vp8_new_context,
152  .free = vp8_free_context,
153  .parse_packet = vp8_handle_packet,
154 };
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:154
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_WB24 AV_RB16
Definition: bytestream.h:89
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1196
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:117
RTP/H264 specific private data.
Definition: rdt.c:83
int index
stream index in AVFormatContext
Definition: avformat.h:621
int size
Definition: avcodec.h:909
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
RTPDynamicProtocolHandler ff_vp8_dynamic_handler
Definition: rtpdec_vp8.c:147
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1184
Format I/O context.
Definition: avformat.h:863
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:46
uint8_t * data
Definition: avcodec.h:908
static int flags
Definition: log.c:34
void av_destruct_packet(AVPacket *pkt)
Default packet destructor.
Definition: avpacket.c:33
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:190
void(* destruct)(struct AVPacket *)
Definition: avcodec.h:931
uint32_t timestamp
timestamp of next-to-be-returned packet
Definition: rtpdec_latm.c:31
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:137
#define RTP_FLAG_MARKER
RTP marker bit was set for this packet.
Definition: rtpdec.h:94
#define AVERROR(e)
Definition: error.h:43
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
const char enc_name[50]
Definition: rtpdec.h:117
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
static void vp8_free_context(PayloadContext *vp8)
Definition: rtpdec_vp8.c:137
static int vp8_handle_packet(AVFormatContext *ctx, PayloadContext *vp8, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, int flags)
Definition: rtpdec_vp8.c:49
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
AVIOContext * data
Definition: rtpdec_vp8.c:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static PayloadContext * vp8_new_context(void)
Definition: rtpdec_vp8.c:130
int len
static void prepare_packet(AVPacket *pkt, PayloadContext *vp8, int stream)
Definition: rtpdec_vp8.c:39
int stream_index
Definition: avcodec.h:910