OSDN Git Service

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