FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rtpdec_mpeg4.c
Date: 2024-04-20 14:10:07
Exec Total Coverage
Lines: 0 144 0.0%
Functions: 0 6 0.0%
Branches: 0 90 0.0%

Line Branch Exec Source
1 /*
2 * Common code for the RTP depacketization of MPEG-4 formats.
3 * Copyright (c) 2010 Fabrice Bellard
4 * Romain Degez
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * @brief MPEG-4 / RTP Code
26 * @author Fabrice Bellard
27 * @author Romain Degez
28 */
29
30 #include "rtpdec_formats.h"
31 #include "internal.h"
32 #include "libavutil/attributes.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/mem.h"
35 #include "libavcodec/get_bits.h"
36
37 #define MAX_AAC_HBR_FRAME_SIZE 8191
38
39 /** Structure listing useful vars to parse RTP packet payload */
40 struct PayloadContext {
41 int sizelength;
42 int indexlength;
43 int indexdeltalength;
44 int profile_level_id;
45 int streamtype;
46 int objecttype;
47 char *mode;
48
49 /** mpeg 4 AU headers */
50 struct AUHeaders {
51 int size;
52 int index;
53 int cts_flag;
54 int cts;
55 int dts_flag;
56 int dts;
57 int rap_flag;
58 int streamstate;
59 } *au_headers;
60 int au_headers_allocated;
61 int nb_au_headers;
62 int au_headers_length_bytes;
63 int cur_au_index;
64
65 uint8_t buf[FFMAX(RTP_MAX_PACKET_LENGTH, MAX_AAC_HBR_FRAME_SIZE)];
66 int buf_pos, buf_size;
67 uint32_t timestamp;
68 };
69
70 typedef struct AttrNameMap {
71 const char *str;
72 uint16_t type;
73 uint32_t offset;
74
75 /** Range for integer values */
76 struct Range {
77 int min;
78 int max;
79 } range;
80 } AttrNameMap;
81
82 /* All known fmtp parameters and the corresponding RTPAttrTypeEnum */
83 #define ATTR_NAME_TYPE_INT 0
84 #define ATTR_NAME_TYPE_STR 1
85 static const AttrNameMap attr_names[] = {
86 { "SizeLength", ATTR_NAME_TYPE_INT,
87 offsetof(PayloadContext, sizelength),
88 {0, 32} }, // SizeLength number of bits used to encode AU-size integer value
89 { "IndexLength", ATTR_NAME_TYPE_INT,
90 offsetof(PayloadContext, indexlength),
91 {0, 32} }, // IndexLength number of bits used to encode AU-Index integer value
92 { "IndexDeltaLength", ATTR_NAME_TYPE_INT,
93 offsetof(PayloadContext, indexdeltalength),
94 {0, 32} }, // IndexDeltaLength number of bits to encode AU-Index-delta integer value
95 { "profile-level-id", ATTR_NAME_TYPE_INT,
96 offsetof(PayloadContext, profile_level_id),
97 {INT32_MIN, INT32_MAX} }, // It differs depending on StreamType
98 { "StreamType", ATTR_NAME_TYPE_INT,
99 offsetof(PayloadContext, streamtype),
100 {0x00, 0x3F} }, // Values from ISO/IEC 14496-1, 'StreamType Values' table
101 { "mode", ATTR_NAME_TYPE_STR,
102 offsetof(PayloadContext, mode),
103 {0} },
104 { NULL, -1, -1, {0} },
105 };
106
107 static void close_context(PayloadContext *data)
108 {
109 av_freep(&data->au_headers);
110 av_freep(&data->mode);
111 }
112
113 static int parse_fmtp_config(AVCodecParameters *par, const char *value)
114 {
115 /* decode the hexa encoded parameter */
116 int len = ff_hex_to_data(NULL, value), ret;
117
118 if ((ret = ff_alloc_extradata(par, len)) < 0)
119 return ret;
120 ff_hex_to_data(par->extradata, value);
121 return 0;
122 }
123
124 static int rtp_parse_mp4_au(PayloadContext *data, const uint8_t *buf, int len)
125 {
126 int au_headers_length, au_header_size, i;
127 GetBitContext getbitcontext;
128 int ret;
129
130 if (len < 2)
131 return AVERROR_INVALIDDATA;
132
133 /* decode the first 2 bytes where the AUHeader sections are stored
134 length in bits */
135 au_headers_length = AV_RB16(buf);
136
137 if (au_headers_length > RTP_MAX_PACKET_LENGTH)
138 return -1;
139
140 data->au_headers_length_bytes = (au_headers_length + 7) / 8;
141
142 /* skip AU headers length section (2 bytes) */
143 buf += 2;
144 len -= 2;
145
146 if (len < data->au_headers_length_bytes)
147 return AVERROR_INVALIDDATA;
148
149 ret = init_get_bits(&getbitcontext, buf, data->au_headers_length_bytes * 8);
150 if (ret < 0)
151 return ret;
152
153 /* XXX: Wrong if optional additional sections are present (cts, dts etc...) */
154 au_header_size = data->sizelength + data->indexlength;
155 if (au_header_size <= 0 || (au_headers_length % au_header_size != 0))
156 return -1;
157
158 data->nb_au_headers = au_headers_length / au_header_size;
159 if (!data->au_headers || data->au_headers_allocated < data->nb_au_headers) {
160 av_free(data->au_headers);
161 data->au_headers = av_malloc(sizeof(struct AUHeaders) * data->nb_au_headers);
162 if (!data->au_headers)
163 return AVERROR(ENOMEM);
164 data->au_headers_allocated = data->nb_au_headers;
165 }
166
167 for (i = 0; i < data->nb_au_headers; ++i) {
168 data->au_headers[i].size = get_bits_long(&getbitcontext, data->sizelength);
169 data->au_headers[i].index = get_bits_long(&getbitcontext, data->indexlength);
170 }
171
172 return 0;
173 }
174
175
176 /* Follows RFC 3640 */
177 static int aac_parse_packet(AVFormatContext *ctx, PayloadContext *data,
178 AVStream *st, AVPacket *pkt, uint32_t *timestamp,
179 const uint8_t *buf, int len, uint16_t seq,
180 int flags)
181 {
182 int ret;
183
184
185 if (!buf) {
186 if (data->cur_au_index > data->nb_au_headers) {
187 av_log(ctx, AV_LOG_ERROR, "Invalid parser state\n");
188 return AVERROR_INVALIDDATA;
189 }
190 if (data->buf_size - data->buf_pos < data->au_headers[data->cur_au_index].size) {
191 av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
192 return AVERROR_INVALIDDATA;
193 }
194 if ((ret = av_new_packet(pkt, data->au_headers[data->cur_au_index].size)) < 0) {
195 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
196 return ret;
197 }
198 memcpy(pkt->data, &data->buf[data->buf_pos], data->au_headers[data->cur_au_index].size);
199 data->buf_pos += data->au_headers[data->cur_au_index].size;
200 pkt->stream_index = st->index;
201 data->cur_au_index++;
202
203 if (data->cur_au_index == data->nb_au_headers) {
204 data->buf_pos = 0;
205 return 0;
206 }
207
208 return 1;
209 }
210
211 if (rtp_parse_mp4_au(data, buf, len)) {
212 av_log(ctx, AV_LOG_ERROR, "Error parsing AU headers\n");
213 return -1;
214 }
215
216 buf += data->au_headers_length_bytes + 2;
217 len -= data->au_headers_length_bytes + 2;
218 if (data->nb_au_headers == 1 && len < data->au_headers[0].size) {
219 /* Packet is fragmented */
220
221 if (!data->buf_pos) {
222 if (data->au_headers[0].size > MAX_AAC_HBR_FRAME_SIZE) {
223 av_log(ctx, AV_LOG_ERROR, "Invalid AU size\n");
224 return AVERROR_INVALIDDATA;
225 }
226
227 data->buf_size = data->au_headers[0].size;
228 data->timestamp = *timestamp;
229 }
230
231 if (data->timestamp != *timestamp ||
232 data->au_headers[0].size != data->buf_size ||
233 data->buf_pos + len > MAX_AAC_HBR_FRAME_SIZE) {
234 data->buf_pos = 0;
235 data->buf_size = 0;
236 av_log(ctx, AV_LOG_ERROR, "Invalid packet received\n");
237 return AVERROR_INVALIDDATA;
238 }
239
240 memcpy(&data->buf[data->buf_pos], buf, len);
241 data->buf_pos += len;
242
243 if (!(flags & RTP_FLAG_MARKER))
244 return AVERROR(EAGAIN);
245
246 if (data->buf_pos != data->buf_size) {
247 data->buf_pos = 0;
248 av_log(ctx, AV_LOG_ERROR, "Missed some packets, discarding frame\n");
249 return AVERROR_INVALIDDATA;
250 }
251
252 data->buf_pos = 0;
253 ret = av_new_packet(pkt, data->buf_size);
254 if (ret < 0) {
255 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
256 return ret;
257 }
258 pkt->stream_index = st->index;
259
260 memcpy(pkt->data, data->buf, data->buf_size);
261
262 return 0;
263 }
264
265 if (len < data->au_headers[0].size) {
266 av_log(ctx, AV_LOG_ERROR, "First AU larger than packet size\n");
267 return AVERROR_INVALIDDATA;
268 }
269 if ((ret = av_new_packet(pkt, data->au_headers[0].size)) < 0) {
270 av_log(ctx, AV_LOG_ERROR, "Out of memory\n");
271 return ret;
272 }
273 memcpy(pkt->data, buf, data->au_headers[0].size);
274 len -= data->au_headers[0].size;
275 buf += data->au_headers[0].size;
276 pkt->stream_index = st->index;
277
278 if (len > 0 && data->nb_au_headers > 1) {
279 data->buf_size = FFMIN(len, sizeof(data->buf));
280 memcpy(data->buf, buf, data->buf_size);
281 data->cur_au_index = 1;
282 data->buf_pos = 0;
283 return 1;
284 }
285
286 return 0;
287 }
288
289 static int parse_fmtp(AVFormatContext *s,
290 AVStream *stream, PayloadContext *data,
291 const char *attr, const char *value)
292 {
293 AVCodecParameters *par = stream->codecpar;
294 int res, i;
295
296 if (!strcmp(attr, "config")) {
297 res = parse_fmtp_config(par, value);
298
299 if (res < 0)
300 return res;
301 }
302
303 if (par->codec_id == AV_CODEC_ID_AAC) {
304 /* Looking for a known attribute */
305 for (i = 0; attr_names[i].str; ++i) {
306 if (!av_strcasecmp(attr, attr_names[i].str)) {
307 if (attr_names[i].type == ATTR_NAME_TYPE_INT) {
308 char *end_ptr = NULL;
309 long long int val = strtoll(value, &end_ptr, 10);
310 if (end_ptr == value || end_ptr[0] != '\0') {
311 av_log(s, AV_LOG_ERROR,
312 "The %s field value is not a valid number: %s\n",
313 attr, value);
314 return AVERROR_INVALIDDATA;
315 }
316 if (val < attr_names[i].range.min ||
317 val > attr_names[i].range.max) {
318 av_log(s, AV_LOG_ERROR,
319 "fmtp field %s should be in range [%d,%d] (provided value: %lld)",
320 attr, attr_names[i].range.min, attr_names[i].range.max, val);
321 return AVERROR_INVALIDDATA;
322 }
323
324 *(int *)((char *)data+
325 attr_names[i].offset) = (int) val;
326 } else if (attr_names[i].type == ATTR_NAME_TYPE_STR) {
327 char *val = av_strdup(value);
328 if (!val)
329 return AVERROR(ENOMEM);
330 *(char **)((char *)data+
331 attr_names[i].offset) = val;
332 }
333 }
334 }
335 }
336 return 0;
337 }
338
339 static int parse_sdp_line(AVFormatContext *s, int st_index,
340 PayloadContext *data, const char *line)
341 {
342 const char *p;
343
344 if (st_index < 0)
345 return 0;
346
347 if (av_strstart(line, "fmtp:", &p))
348 return ff_parse_fmtp(s, s->streams[st_index], data, p, parse_fmtp);
349
350 return 0;
351 }
352
353 const RTPDynamicProtocolHandler ff_mp4v_es_dynamic_handler = {
354 .enc_name = "MP4V-ES",
355 .codec_type = AVMEDIA_TYPE_VIDEO,
356 .codec_id = AV_CODEC_ID_MPEG4,
357 .need_parsing = AVSTREAM_PARSE_FULL,
358 .priv_data_size = sizeof(PayloadContext),
359 .parse_sdp_a_line = parse_sdp_line,
360 };
361
362 const RTPDynamicProtocolHandler ff_mpeg4_generic_dynamic_handler = {
363 .enc_name = "mpeg4-generic",
364 .codec_type = AVMEDIA_TYPE_AUDIO,
365 .codec_id = AV_CODEC_ID_AAC,
366 .priv_data_size = sizeof(PayloadContext),
367 .parse_sdp_a_line = parse_sdp_line,
368 .close = close_context,
369 .parse_packet = aac_parse_packet,
370 };
371