]> git.sesse.net Git - ffmpeg/blob - libavformat/rtpdec_dv.c
avcodec/vc1dsp: add () to protect the arguments of the op* macros
[ffmpeg] / libavformat / rtpdec_dv.c
1 /*
2  * RTP parser for DV payload format (RFC 6469)
3  * Copyright (c) 2015 Thomas Volkert <thomas@homer-conferencing.com>
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
23 #include "libavutil/avstring.h"
24 #include "libavcodec/bytestream.h"
25
26 #include "rtpdec_formats.h"
27
28 struct PayloadContext {
29     AVIOContext *buf;
30     uint32_t    timestamp;
31     int         bundled_audio;
32 };
33
34 static av_cold PayloadContext *dv_new_context(void)
35 {
36     return av_mallocz(sizeof(PayloadContext));
37 }
38
39 static void dv_free_dyn_buffer(AVIOContext **dyn_buf)
40 {
41     uint8_t *ptr_dyn_buffer;
42     avio_close_dyn_buf(*dyn_buf, &ptr_dyn_buffer);
43     av_free(ptr_dyn_buffer);
44     *dyn_buf = NULL;
45 }
46
47 static av_cold void dv_free_context(PayloadContext *data)
48 {
49     av_free(data);
50 }
51
52 static av_cold int dv_init(AVFormatContext *ctx, int st_index,
53                            PayloadContext *data)
54 {
55     av_dlog(ctx, "dv_init() for stream %d\n", st_index);
56
57     if (st_index < 0)
58         return 0;
59
60     ctx->streams[st_index]->need_parsing = AVSTREAM_PARSE_FULL;
61
62     return 0;
63 }
64
65 static av_cold int dv_sdp_parse_fmtp_config(AVFormatContext *s,
66                                             AVStream *stream,
67                                             PayloadContext *dv_data,
68                                             char *attr, char *value)
69 {
70     /* does the DV stream include audio? */
71     if (!strcmp(attr, "audio") && !strcmp(value, "audio"))
72         dv_data->bundled_audio = 1;
73
74     /* extract the DV profile */
75     if (!strcmp(attr, "encode")) {
76         /* SD-VCR/525-60 */
77         /* SD-VCR/625-50 */
78         /* HD-VCR/1125-60 */
79         /* HD-VCR/1250-50 */
80         /* SDL-VCR/525-60 */
81         /* SDL-VCR/625-50 */
82         /* 314M-25/525-60 */
83         /* 314M-25/625-50 */
84         /* 314M-50/525-60 */
85         /* 314M-50/625-50 */
86         /* 370M/1080-60i */
87         /* 370M/1080-50i */
88         /* 370M/720-60p */
89         /* 370M/720-50p */
90         /* 306M/525-60 (for backward compatibility) */
91         /* 306M/625-50 (for backward compatibility) */
92     }
93
94     return 0;
95 }
96
97 static av_cold int dv_parse_sdp_line(AVFormatContext *ctx, int st_index,
98                                      PayloadContext *dv_data, const char *line)
99 {
100     AVStream *current_stream;
101     const char *sdp_line_ptr = line;
102
103     if (st_index < 0)
104         return 0;
105
106     current_stream = ctx->streams[st_index];
107
108     if (av_strstart(sdp_line_ptr, "fmtp:", &sdp_line_ptr)) {
109         return ff_parse_fmtp(ctx, current_stream, dv_data, sdp_line_ptr,
110                              dv_sdp_parse_fmtp_config);
111     }
112
113     return 0;
114 }
115
116 static int dv_handle_packet(AVFormatContext *ctx, PayloadContext *rtp_dv_ctx,
117                             AVStream *st, AVPacket *pkt, uint32_t *timestamp,
118                             const uint8_t *buf, int len, uint16_t seq,
119                             int flags)
120 {
121     int res = 0;
122
123     /* drop data of previous packets in case of non-continuous (lossy) packet stream */
124     if (rtp_dv_ctx->buf && rtp_dv_ctx->timestamp != *timestamp) {
125         dv_free_dyn_buffer(&rtp_dv_ctx->buf);
126     }
127
128     /* sanity check for size of input packet: 1 byte payload at least */
129     if (len < 1) {
130         av_log(ctx, AV_LOG_ERROR, "Too short RTP/DV packet, got %d bytes\n", len);
131         return AVERROR_INVALIDDATA;
132     }
133
134     /* start frame buffering with new dynamic buffer */
135     if (!rtp_dv_ctx->buf) {
136         res = avio_open_dyn_buf(&rtp_dv_ctx->buf);
137         if (res < 0)
138             return res;
139         /* update the timestamp in the frame packet with the one from the RTP packet */
140         rtp_dv_ctx->timestamp = *timestamp;
141     }
142
143     /* write the fragment to the dyn. buffer */
144     avio_write(rtp_dv_ctx->buf, buf, len);
145
146     /* RTP marker bit means: last fragment of current frame was received;
147        otherwise, an additional fragment is needed for the current frame */
148     if (!(flags & RTP_FLAG_MARKER))
149         return AVERROR(EAGAIN);
150
151     /* close frame buffering and create resulting A/V packet */
152     res = ff_rtp_finalize_packet(pkt, &rtp_dv_ctx->buf, st->index);
153     if (res < 0)
154         return res;
155
156     return 0;
157 }
158
159 RTPDynamicProtocolHandler ff_dv_dynamic_handler = {
160     .enc_name         = "DV",
161     .codec_type       = AVMEDIA_TYPE_VIDEO,
162     .codec_id         = AV_CODEC_ID_DVVIDEO,
163     .init             = dv_init,
164     .parse_sdp_a_line = dv_parse_sdp_line,
165     .alloc            = dv_new_context,
166     .free             = dv_free_context,
167     .parse_packet     = dv_handle_packet
168 };