Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/codec2.c
Line
Count
Source
1
/*
2
 * codec2 muxer and demuxers
3
 * Copyright (c) 2017 Tomas Härdin
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 "config_components.h"
23
24
#include "libavcodec/codec2utils.h"
25
#include "libavutil/channel_layout.h"
26
#include "libavutil/intreadwrite.h"
27
#include "libavutil/opt.h"
28
#include "avio_internal.h"
29
#include "avformat.h"
30
#include "demux.h"
31
#include "internal.h"
32
#include "mux.h"
33
#include "rawenc.h"
34
#include "pcm.h"
35
36
711
#define CODEC2_HEADER_SIZE 7
37
965k
#define CODEC2_MAGIC       0xC0DEC2
38
39
//the lowest version we should ever run across is 0.8
40
//we may run across later versions as the format evolves
41
1.96k
#define EXPECTED_CODEC2_MAJOR_VERSION 0
42
423
#define EXPECTED_CODEC2_MINOR_VERSION 8
43
44
typedef struct {
45
    const AVClass *class;
46
    int mode;
47
    int frames_per_packet;
48
} Codec2Context;
49
50
static int codec2_probe(const AVProbeData *p)
51
964k
{
52
    //must start wih C0 DE C2
53
964k
    if (AV_RB24(p->buf) != CODEC2_MAGIC) {
54
964k
        return 0;
55
964k
    }
56
57
    //no .c2 files prior to 0.8
58
    //be strict about major version while we're at it
59
623
    if (p->buf[3] != EXPECTED_CODEC2_MAJOR_VERSION ||
60
423
        p->buf[4] <  EXPECTED_CODEC2_MINOR_VERSION) {
61
403
        return 0;
62
403
    }
63
64
    //32 bits of identification -> low score
65
220
    return AVPROBE_SCORE_EXTENSION + 1;
66
623
}
67
68
//Mimics codec2_samples_per_frame()
69
static int codec2_mode_frame_size(AVFormatContext *s, int mode)
70
1.42k
{
71
1.42k
    int frame_size_table[CODEC2_MODE_MAX+1] = {
72
1.42k
        160,    // 3200
73
1.42k
        160,    // 2400
74
1.42k
        320,    // 1600
75
1.42k
        320,    // 1400
76
1.42k
        320,    // 1300
77
1.42k
        320,    // 1200
78
1.42k
        320,    // 700
79
1.42k
        320,    // 700B
80
1.42k
        320,    // 700C
81
1.42k
    };
82
83
1.42k
    if (mode < 0 || mode > CODEC2_MODE_MAX) {
84
20
        av_log(s, AV_LOG_ERROR, "unknown codec2 mode %i, can't find frame_size\n", mode);
85
20
        return 0;
86
1.40k
    } else {
87
1.40k
        return frame_size_table[mode];
88
1.40k
    }
89
1.42k
}
90
91
//Mimics (codec2_bits_per_frame()+7)/8
92
static int codec2_mode_block_align(AVFormatContext *s, int mode)
93
1.42k
{
94
1.42k
    int block_align_table[CODEC2_MODE_MAX+1] = {
95
1.42k
        8,      // 3200
96
1.42k
        6,      // 2400
97
1.42k
        8,      // 1600
98
1.42k
        7,      // 1400
99
1.42k
        7,      // 1300
100
1.42k
        6,      // 1200
101
1.42k
        4,      // 700
102
1.42k
        4,      // 700B
103
1.42k
        4,      // 700C
104
1.42k
    };
105
106
1.42k
    if (mode < 0 || mode > CODEC2_MODE_MAX) {
107
20
        av_log(s, AV_LOG_ERROR, "unknown codec2 mode %i, can't find block_align\n", mode);
108
20
        return 0;
109
1.40k
    } else {
110
1.40k
        return block_align_table[mode];
111
1.40k
    }
112
1.42k
}
113
114
//Computes bitrate from mode, with frames rounded up to the nearest octet.
115
//So 700 bit/s (28 bits/frame) becomes 800 bits/s (32 bits/frame).
116
static int codec2_mode_bit_rate(AVFormatContext *s, int mode)
117
711
{
118
711
    int frame_size  = codec2_mode_frame_size(s, mode);
119
711
    int block_align = codec2_mode_block_align(s, mode);
120
121
711
    if (frame_size <= 0 || block_align <= 0) {
122
10
        return 0;
123
10
    }
124
125
701
    return 8 * 8000 * block_align / frame_size;
126
711
}
127
128
static int codec2_read_header_common(AVFormatContext *s, AVStream *st)
129
711
{
130
711
    int mode = codec2_mode_from_extradata(st->codecpar->extradata);
131
132
711
    st->codecpar->codec_type        = AVMEDIA_TYPE_AUDIO;
133
711
    st->codecpar->codec_id          = AV_CODEC_ID_CODEC2;
134
711
    st->codecpar->sample_rate       = 8000;
135
711
    st->codecpar->format            = AV_SAMPLE_FMT_S16;
136
711
    st->codecpar->ch_layout         = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
137
711
    st->codecpar->bit_rate          = codec2_mode_bit_rate(s, mode);
138
711
    st->codecpar->frame_size        = codec2_mode_frame_size(s, mode);
139
711
    st->codecpar->block_align       = codec2_mode_block_align(s, mode);
140
141
711
    if (st->codecpar->bit_rate <= 0 ||
142
701
        st->codecpar->frame_size <= 0 ||
143
701
        st->codecpar->block_align <= 0) {
144
10
        return AVERROR_INVALIDDATA;
145
10
    }
146
147
701
    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
148
149
701
    return 0;
150
711
}
151
152
static int codec2_read_header(AVFormatContext *s)
153
955
{
154
955
    AVStream *st = avformat_new_stream(s, NULL);
155
955
    int ret, version;
156
157
955
    if (!st) {
158
0
        return AVERROR(ENOMEM);
159
0
    }
160
161
955
    if (avio_rb24(s->pb) != CODEC2_MAGIC) {
162
199
        av_log(s, AV_LOG_ERROR, "not a .c2 file\n");
163
199
        return AVERROR_INVALIDDATA;
164
199
    }
165
166
756
    ret = ff_alloc_extradata(st->codecpar, CODEC2_EXTRADATA_SIZE);
167
756
    if (ret) {
168
0
        return ret;
169
0
    }
170
171
756
    ret = ffio_read_size(s->pb, st->codecpar->extradata, CODEC2_EXTRADATA_SIZE);
172
756
    if (ret < 0) {
173
35
        return ret;
174
35
    }
175
176
721
    version = AV_RB16(st->codecpar->extradata);
177
721
    if ((version >> 8) != EXPECTED_CODEC2_MAJOR_VERSION) {
178
10
        avpriv_report_missing_feature(s, "Major version %i", version >> 8);
179
10
        return AVERROR_PATCHWELCOME;
180
10
    }
181
182
711
    ffformatcontext(s)->data_offset = CODEC2_HEADER_SIZE;
183
184
711
    return codec2_read_header_common(s, st);
185
721
}
186
187
static int codec2_read_packet(AVFormatContext *s, AVPacket *pkt)
188
309k
{
189
309k
    Codec2Context *c2 = s->priv_data;
190
309k
    AVStream *st = s->streams[0];
191
309k
    int ret, size, n, block_align, frame_size;
192
193
309k
    block_align = st->codecpar->block_align;
194
309k
    frame_size  = st->codecpar->frame_size;
195
196
309k
    if (block_align <= 0 || frame_size <= 0 || c2->frames_per_packet <= 0) {
197
0
        return AVERROR(EINVAL);
198
0
    }
199
200
    //try to read desired number of frames, compute n from to actual number of bytes read
201
309k
    size = c2->frames_per_packet * block_align;
202
309k
    ret = av_get_packet(s->pb, pkt, size);
203
309k
    if (ret < 0) {
204
841
        return ret;
205
841
    }
206
207
    //only set duration - compute_pkt_fields() and ff_pcm_read_seek() takes care of everything else
208
    //tested by spamming the seek functionality in ffplay
209
308k
    n = ret / block_align;
210
308k
    pkt->duration = n * frame_size;
211
212
308k
    return ret;
213
309k
}
214
215
static int codec2_write_header(AVFormatContext *s)
216
0
{
217
0
    AVStream *st = s->streams[0];
218
0
219
0
    if (st->codecpar->extradata_size != CODEC2_EXTRADATA_SIZE) {
220
0
        av_log(s, AV_LOG_ERROR, ".c2 files require exactly %i bytes of extradata (got %i)\n",
221
0
               CODEC2_EXTRADATA_SIZE, st->codecpar->extradata_size);
222
0
        return AVERROR(EINVAL);
223
0
    }
224
0
225
0
    avio_wb24(s->pb, CODEC2_MAGIC);
226
0
    avio_write(s->pb, st->codecpar->extradata, CODEC2_EXTRADATA_SIZE);
227
0
228
0
    return 0;
229
0
}
230
231
static int codec2raw_read_header(AVFormatContext *s)
232
109
{
233
109
    Codec2Context *c2 = s->priv_data;
234
109
    AVStream *st;
235
109
    int ret;
236
237
109
    if (c2->mode < 0) {
238
        //FIXME: using a default value of -1 for mandatory options is an incredibly ugly hack
239
109
        av_log(s, AV_LOG_ERROR, "-mode must be set in order to make sense of raw codec2 files\n");
240
109
        return AVERROR(EINVAL);
241
109
    }
242
243
0
    st = avformat_new_stream(s, NULL);
244
0
    if (!st) {
245
0
        return AVERROR(ENOMEM);
246
0
    }
247
248
0
    ret = ff_alloc_extradata(st->codecpar, CODEC2_EXTRADATA_SIZE);
249
0
    if (ret) {
250
0
        return ret;
251
0
    }
252
253
0
    codec2_make_extradata(st->codecpar->extradata, c2->mode);
254
255
0
    return codec2_read_header_common(s, st);
256
0
}
257
258
//transcoding report2074.c2 to wav went from 7.391s to 5.322s with -frames_per_packet 1000 compared to default, same sha1sum
259
#define FRAMES_PER_PACKET \
260
    { "frames_per_packet", "Number of frames to read at a time. Higher = faster decoding, lower granularity", \
261
      offsetof(Codec2Context, frames_per_packet), AV_OPT_TYPE_INT, {.i64 = 1}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM}
262
263
static const AVOption codec2_options[] = {
264
    FRAMES_PER_PACKET,
265
    { NULL },
266
};
267
268
static const AVOption codec2raw_options[] = {
269
    CODEC2_AVOPTIONS("codec2 mode [mandatory]", Codec2Context, -1, -1, AV_OPT_FLAG_DECODING_PARAM),
270
    FRAMES_PER_PACKET,
271
    { NULL },
272
};
273
274
static const AVClass codec2_demux_class = {
275
    .class_name = "codec2 demuxer",
276
    .item_name  = av_default_item_name,
277
    .option     = codec2_options,
278
    .version    = LIBAVUTIL_VERSION_INT,
279
    .category   = AV_CLASS_CATEGORY_DEMUXER,
280
};
281
282
static const AVClass codec2raw_demux_class = {
283
    .class_name = "codec2raw demuxer",
284
    .item_name  = av_default_item_name,
285
    .option     = codec2raw_options,
286
    .version    = LIBAVUTIL_VERSION_INT,
287
    .category   = AV_CLASS_CATEGORY_DEMUXER,
288
};
289
290
#if CONFIG_CODEC2_DEMUXER
291
const FFInputFormat ff_codec2_demuxer = {
292
    .p.name         = "codec2",
293
    .p.long_name    = NULL_IF_CONFIG_SMALL("codec2 .c2 demuxer"),
294
    .p.extensions   = "c2",
295
    .p.flags        = AVFMT_GENERIC_INDEX,
296
    .p.priv_class   = &codec2_demux_class,
297
    .priv_data_size = sizeof(Codec2Context),
298
    .read_probe     = codec2_probe,
299
    .read_header    = codec2_read_header,
300
    .read_packet    = codec2_read_packet,
301
    .read_seek      = ff_pcm_read_seek,
302
    .raw_codec_id   = AV_CODEC_ID_CODEC2,
303
};
304
#endif
305
306
#if CONFIG_CODEC2_MUXER
307
const FFOutputFormat ff_codec2_muxer = {
308
    .p.name         = "codec2",
309
    .p.long_name    = NULL_IF_CONFIG_SMALL("codec2 .c2 muxer"),
310
    .p.extensions   = "c2",
311
    .p.audio_codec  = AV_CODEC_ID_CODEC2,
312
    .p.video_codec  = AV_CODEC_ID_NONE,
313
    .p.subtitle_codec = AV_CODEC_ID_NONE,
314
    .p.flags        = AVFMT_NOTIMESTAMPS,
315
    .flags_internal   = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
316
                        FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
317
    .write_header   = codec2_write_header,
318
    .write_packet   = ff_raw_write_packet,
319
};
320
#endif
321
322
#if CONFIG_CODEC2RAW_DEMUXER
323
const FFInputFormat ff_codec2raw_demuxer = {
324
    .p.name         = "codec2raw",
325
    .p.long_name    = NULL_IF_CONFIG_SMALL("raw codec2 demuxer"),
326
    .p.flags        = AVFMT_GENERIC_INDEX,
327
    .p.priv_class   = &codec2raw_demux_class,
328
    .priv_data_size = sizeof(Codec2Context),
329
    .read_header    = codec2raw_read_header,
330
    .read_packet    = codec2_read_packet,
331
    .read_seek      = ff_pcm_read_seek,
332
    .raw_codec_id   = AV_CODEC_ID_CODEC2,
333
};
334
#endif