2 * RTP parser for DV payload format (RFC 6469)
3 * Copyright (c) 2015 Thomas Volkert <thomas@homer-conferencing.com>
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/avstring.h"
24 #include "libavcodec/bytestream.h"
26 #include "rtpdec_formats.h"
28 struct PayloadContext {
34 static av_cold PayloadContext *dv_new_context(void)
36 return av_mallocz(sizeof(PayloadContext));
39 static void dv_free_dyn_buffer(AVIOContext **dyn_buf)
41 uint8_t *ptr_dyn_buffer;
42 avio_close_dyn_buf(*dyn_buf, &ptr_dyn_buffer);
43 av_free(ptr_dyn_buffer);
47 static av_cold void dv_free_context(PayloadContext *data)
49 dv_free_dyn_buffer(&data->buf);
53 static av_cold int dv_sdp_parse_fmtp_config(AVFormatContext *s,
55 PayloadContext *dv_data,
56 char *attr, char *value)
58 /* does the DV stream include audio? */
59 if (!strcmp(attr, "audio") && !strcmp(value, "bundled"))
60 dv_data->bundled_audio = 1;
62 /* extract the DV profile */
63 if (!strcmp(attr, "encode")) {
78 /* 306M/525-60 (for backward compatibility) */
79 /* 306M/625-50 (for backward compatibility) */
85 static av_cold int dv_parse_sdp_line(AVFormatContext *ctx, int st_index,
86 PayloadContext *dv_data, const char *line)
88 AVStream *current_stream;
89 const char *sdp_line_ptr = line;
94 current_stream = ctx->streams[st_index];
96 if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
97 return ff_parse_fmtp(ctx, current_stream, dv_data, sdp_line_ptr,
98 dv_sdp_parse_fmtp_config);
104 static int dv_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_dv_ctx,
105 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
106 const uint8_t *buf, int len, uint16_t seq,
111 /* drop data of previous packets in case of non-continuous (lossy) packet stream */
112 if (rtp_dv_ctx->buf && rtp_dv_ctx->timestamp != *timestamp) {
113 dv_free_dyn_buffer(&rtp_dv_ctx->buf);
116 /* sanity check for size of input packet: 1 byte payload at least */
118 av_log(ctx, AV_LOG_ERROR, "Too short RTP/DV packet, got %d bytes\n", len);
119 return AVERROR_INVALIDDATA;
122 /* start frame buffering with new dynamic buffer */
123 if (!rtp_dv_ctx->buf) {
124 res = avio_open_dyn_buf(&rtp_dv_ctx->buf);
127 /* update the timestamp in the frame packet with the one from the RTP packet */
128 rtp_dv_ctx->timestamp = *timestamp;
131 /* write the fragment to the dyn. buffer */
132 avio_write(rtp_dv_ctx->buf, buf, len);
134 /* RTP marker bit means: last fragment of current frame was received;
135 otherwise, an additional fragment is needed for the current frame */
136 if (!(flags & RTP_FLAG_MARKER))
137 return AVERROR(EAGAIN);
139 /* close frame buffering and create resulting A/V packet */
140 res = ff_rtp_finalize_packet(pkt, &rtp_dv_ctx->buf, st->index);
147 RTPDynamicProtocolHandler ff_dv_dynamic_handler = {
149 .codec_type = AVMEDIA_TYPE_VIDEO,
150 .codec_id = AV_CODEC_ID_DVVIDEO,
151 .need_parsing = AVSTREAM_PARSE_FULL,
152 .parse_sdp_a_line = dv_parse_sdp_line,
153 .alloc = dv_new_context,
154 .free = dv_free_context,
155 .parse_packet = dv_handle_packet,