FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/rtpdec_latm.c
Date: 2026-09-26 05:01:43
Exec Total Coverage
Lines: 0 82 0.0%
Functions: 0 5 0.0%
Branches: 0 50 0.0%

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