/src/ffmpeg/libavformat/rtpdec_latm.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * RTP Depacketization of MP4A-LATM, RFC 3016 |
3 | | * Copyright (c) 2010 Martin Storsjo |
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 | | #include "avio_internal.h" |
23 | | #include "rtpdec_formats.h" |
24 | | #include "internal.h" |
25 | | #include "libavutil/avstring.h" |
26 | | #include "libavutil/mem.h" |
27 | | #include "libavcodec/get_bits.h" |
28 | | |
29 | | struct PayloadContext { |
30 | | AVIOContext *dyn_buf; |
31 | | uint8_t *buf; |
32 | | int pos, len; |
33 | | uint32_t timestamp; |
34 | | }; |
35 | | |
36 | | static void latm_close_context(PayloadContext *data) |
37 | 0 | { |
38 | 0 | ffio_free_dyn_buf(&data->dyn_buf); |
39 | 0 | av_freep(&data->buf); |
40 | 0 | } |
41 | | |
42 | | static int latm_parse_packet(AVFormatContext *ctx, PayloadContext *data, |
43 | | AVStream *st, AVPacket *pkt, uint32_t *timestamp, |
44 | | const uint8_t *buf, int len, uint16_t seq, |
45 | | int flags) |
46 | 0 | { |
47 | 0 | int ret, cur_len; |
48 | |
|
49 | 0 | if (buf) { |
50 | 0 | if (!data->dyn_buf || data->timestamp != *timestamp) { |
51 | 0 | av_freep(&data->buf); |
52 | 0 | ffio_free_dyn_buf(&data->dyn_buf); |
53 | |
|
54 | 0 | data->timestamp = *timestamp; |
55 | 0 | if ((ret = avio_open_dyn_buf(&data->dyn_buf)) < 0) |
56 | 0 | return ret; |
57 | 0 | } |
58 | 0 | avio_write(data->dyn_buf, buf, len); |
59 | |
|
60 | 0 | if (!(flags & RTP_FLAG_MARKER)) |
61 | 0 | return AVERROR(EAGAIN); |
62 | 0 | av_freep(&data->buf); |
63 | 0 | data->len = avio_close_dyn_buf(data->dyn_buf, &data->buf); |
64 | 0 | data->dyn_buf = NULL; |
65 | 0 | data->pos = 0; |
66 | 0 | } |
67 | | |
68 | 0 | if (!data->buf) { |
69 | 0 | av_log(ctx, AV_LOG_ERROR, "No data available yet\n"); |
70 | 0 | return AVERROR(EIO); |
71 | 0 | } |
72 | | |
73 | 0 | cur_len = 0; |
74 | 0 | while (data->pos < data->len) { |
75 | 0 | uint8_t val = data->buf[data->pos++]; |
76 | 0 | cur_len += val; |
77 | 0 | if (val != 0xff) |
78 | 0 | break; |
79 | 0 | } |
80 | 0 | if (data->pos + cur_len > data->len) { |
81 | 0 | av_log(ctx, AV_LOG_ERROR, "Malformed LATM packet\n"); |
82 | 0 | return AVERROR(EIO); |
83 | 0 | } |
84 | | |
85 | 0 | if ((ret = av_new_packet(pkt, cur_len)) < 0) |
86 | 0 | return ret; |
87 | 0 | memcpy(pkt->data, data->buf + data->pos, cur_len); |
88 | 0 | data->pos += cur_len; |
89 | 0 | pkt->stream_index = st->index; |
90 | 0 | return data->pos < data->len; |
91 | 0 | } |
92 | | |
93 | | static int parse_fmtp_config(AVStream *st, const char *value) |
94 | 0 | { |
95 | 0 | int len = ff_hex_to_data(NULL, value), i, ret = 0; |
96 | 0 | GetBitContext gb; |
97 | 0 | uint8_t *config; |
98 | 0 | int audio_mux_version, same_time_framing, num_programs, num_layers; |
99 | | |
100 | | /* Pad this buffer, too, to avoid out of bounds reads with get_bits below */ |
101 | 0 | config = av_mallocz(len + AV_INPUT_BUFFER_PADDING_SIZE); |
102 | 0 | if (!config) |
103 | 0 | return AVERROR(ENOMEM); |
104 | 0 | ff_hex_to_data(config, value); |
105 | 0 | ret = init_get_bits(&gb, config, len*8); |
106 | 0 | if (ret < 0) |
107 | 0 | goto end; |
108 | 0 | audio_mux_version = get_bits(&gb, 1); |
109 | 0 | same_time_framing = get_bits(&gb, 1); |
110 | 0 | skip_bits(&gb, 6); /* num_sub_frames */ |
111 | 0 | num_programs = get_bits(&gb, 4); |
112 | 0 | num_layers = get_bits(&gb, 3); |
113 | 0 | if (audio_mux_version != 0 || same_time_framing != 1 || num_programs != 0 || |
114 | 0 | num_layers != 0) { |
115 | 0 | avpriv_report_missing_feature(NULL, "LATM config (%d,%d,%d,%d)", |
116 | 0 | audio_mux_version, same_time_framing, |
117 | 0 | num_programs, num_layers); |
118 | 0 | ret = AVERROR_PATCHWELCOME; |
119 | 0 | goto end; |
120 | 0 | } |
121 | 0 | ret = ff_alloc_extradata(st->codecpar, (get_bits_left(&gb) + 7)/8); |
122 | 0 | if (ret < 0) { |
123 | 0 | goto end; |
124 | 0 | } |
125 | 0 | for (i = 0; i < st->codecpar->extradata_size; i++) |
126 | 0 | st->codecpar->extradata[i] = get_bits(&gb, 8); |
127 | |
|
128 | 0 | end: |
129 | 0 | av_free(config); |
130 | 0 | return ret; |
131 | 0 | } |
132 | | |
133 | | static int parse_fmtp(AVFormatContext *s, |
134 | | AVStream *stream, PayloadContext *data, |
135 | | const char *attr, const char *value) |
136 | 0 | { |
137 | 0 | int res; |
138 | |
|
139 | 0 | if (!strcmp(attr, "config")) { |
140 | 0 | res = parse_fmtp_config(stream, value); |
141 | 0 | if (res < 0) |
142 | 0 | return res; |
143 | 0 | } else if (!strcmp(attr, "cpresent")) { |
144 | 0 | int cpresent = atoi(value); |
145 | 0 | if (cpresent != 0) |
146 | 0 | avpriv_request_sample(s, |
147 | 0 | "RTP MP4A-LATM with in-band configuration"); |
148 | 0 | } |
149 | | |
150 | 0 | return 0; |
151 | 0 | } |
152 | | |
153 | | static int latm_parse_sdp_line(AVFormatContext *s, int st_index, |
154 | | PayloadContext *data, const char *line) |
155 | 0 | { |
156 | 0 | const char *p; |
157 | |
|
158 | 0 | if (st_index < 0) |
159 | 0 | return 0; |
160 | | |
161 | 0 | if (av_strstart(line, "fmtp:", &p)) |
162 | 0 | return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp); |
163 | | |
164 | 0 | return 0; |
165 | 0 | } |
166 | | |
167 | | const RTPDynamicProtocolHandler ff_mp4a_latm_dynamic_handler = { |
168 | | .enc_name = "MP4A-LATM", |
169 | | .codec_type = AVMEDIA_TYPE_AUDIO, |
170 | | .codec_id = AV_CODEC_ID_AAC, |
171 | | .priv_data_size = sizeof(PayloadContext), |
172 | | .parse_sdp_a_line = latm_parse_sdp_line, |
173 | | .close = latm_close_context, |
174 | | .parse_packet = latm_parse_packet, |
175 | | }; |