rtpdec_svq3.c
Go to the documentation of this file.
1 /*
2  * Sorenson-3 (SVQ3/SV3V) payload for RTP
3  * Copyright (c) 2010 Ronald S. Bultje
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 <string.h>
30 #include "libavutil/intreadwrite.h"
31 #include "rtp.h"
32 #include "rtpdec.h"
33 #include "rtpdec_formats.h"
34 
35 struct PayloadContext {
37  int64_t timestamp;
38 };
39 
42  AVStream *st, AVPacket *pkt,
43  uint32_t *timestamp,
44  const uint8_t *buf, int len, int flags)
45 {
46  int config_packet, start_packet, end_packet;
47 
48  if (len < 2)
49  return AVERROR_INVALIDDATA;
50 
51  config_packet = buf[0] & 0x40;
52  start_packet = buf[0] & 0x20;
53  end_packet = buf[0] & 0x10;
54  buf += 2; // ignore buf[1]
55  len -= 2;
56 
57  if (config_packet) {
58 
59  av_freep(&st->codec->extradata);
60  st->codec->extradata_size = 0;
61 
62  if (len < 2 || !(st->codec->extradata =
64  return AVERROR_INVALIDDATA;
65 
66  st->codec->extradata_size = len + 8;
67  memcpy(st->codec->extradata, "SEQH", 4);
68  AV_WB32(st->codec->extradata + 4, len);
69  memcpy(st->codec->extradata + 8, buf, len);
70 
71  /* We set codec_id to CODEC_ID_NONE initially to
72  * delay decoder initialization since extradata is
73  * carried within the RTP stream, not SDP. Here,
74  * by setting codec_id to CODEC_ID_SVQ3, we are signalling
75  * to the decoder that it is OK to initialize. */
77 
78  return AVERROR(EAGAIN);
79  }
80 
81  if (start_packet) {
82  int res;
83 
84  if (sv->pktbuf) {
85  uint8_t *tmp;
86  avio_close_dyn_buf(sv->pktbuf, &tmp);
87  av_free(tmp);
88  }
89  if ((res = avio_open_dyn_buf(&sv->pktbuf)) < 0)
90  return res;
91  sv->timestamp = *timestamp;
92  }
93 
94  if (!sv->pktbuf)
95  return AVERROR_INVALIDDATA;
96 
97  avio_write(sv->pktbuf, buf, len);
98 
99  if (end_packet) {
100  av_init_packet(pkt);
101  pkt->stream_index = st->index;
102  *timestamp = sv->timestamp;
103  pkt->size = avio_close_dyn_buf(sv->pktbuf, &pkt->data);
105  sv->pktbuf = NULL;
106  return 0;
107  }
108 
109  return AVERROR(EAGAIN);
110 }
111 
113 {
114  return av_mallocz(sizeof(PayloadContext));
115 }
116 
118 {
119  if (sv->pktbuf) {
120  uint8_t *buf;
121  avio_close_dyn_buf(sv->pktbuf, &buf);
122  av_free(buf);
123  }
124  av_free(sv);
125 }
126 
128  .enc_name = "X-SV3V-ES",
129  .codec_type = AVMEDIA_TYPE_VIDEO,
130  .codec_id = CODEC_ID_NONE, // see if (config_packet) above
131  .alloc = svq3_extradata_new,
132  .free = svq3_extradata_free,
133  .parse_packet = svq3_parse_packet,
134 };
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
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:1196
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
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:1184
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:147
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
#define AV_WB32(p, d)
Definition: intreadwrite.h:239
static void svq3_extradata_free(PayloadContext *sv)
Definition: rtpdec_svq3.c:117
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1387
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 AVERROR(e)
Definition: error.h:43
const char enc_name[50]
Definition: rtpdec.h:117
AVCodecContext * codec
codec context
Definition: avformat.h:623
AVIOContext * pktbuf
Definition: rtpdec_asf.c:154
int64_t timestamp
Definition: rtpdec_svq3.c:37
static int svq3_parse_packet(AVFormatContext *s, PayloadContext *sv, AVStream *st, AVPacket *pkt, uint32_t *timestamp, const uint8_t *buf, int len, int flags)
return 0 on packet, <0 on partial packet or error...
Definition: rtpdec_svq3.c:41
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
int extradata_size
Definition: avcodec.h:1388
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:64
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:497
RTPDynamicProtocolHandler ff_svq3_dynamic_handler
Definition: rtpdec_svq3.c:127
int len
int stream_index
Definition: avcodec.h:910
static PayloadContext * svq3_extradata_new(void)
Definition: rtpdec_svq3.c:112
enum CodecID codec_id
Definition: avcodec.h:1575