Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/adxdec.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2011 Justin Ruggles
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22
 * @file
23
 * CRI ADX demuxer
24
 */
25
26
#include "libavutil/intreadwrite.h"
27
#include "avformat.h"
28
#include "demux.h"
29
#include "internal.h"
30
#include "rawdec.h"
31
32
37.3k
#define BLOCK_SIZE    18
33
1.95k
#define BLOCK_SAMPLES 32
34
35
typedef struct ADXDemuxerContext {
36
    FFRawDemuxerContext rawctx;
37
    int header_size;
38
} ADXDemuxerContext;
39
40
static int adx_probe(const AVProbeData *p)
41
958k
{
42
958k
    int offset;
43
958k
    if (AV_RB16(p->buf) != 0x8000)
44
957k
        return 0;
45
1.19k
    offset = AV_RB16(&p->buf[2]);
46
1.19k
    if (   offset < 8
47
916
        || offset > p->buf_size - 4
48
417
        || memcmp(p->buf + offset - 2, "(c)CRI", 6))
49
1.17k
        return 0;
50
20
    return AVPROBE_SCORE_MAX * 3 / 4;
51
1.19k
}
52
53
static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
54
86.1k
{
55
86.1k
    ADXDemuxerContext *c = s->priv_data;
56
86.1k
    AVCodecParameters *par = s->streams[0]->codecpar;
57
86.1k
    int ret, size;
58
59
86.1k
    if (par->codec_id == AV_CODEC_ID_AHX)
60
73.1k
        return ff_raw_read_partial_packet(s, pkt);
61
62
13.0k
    if (avio_feof(s->pb))
63
578
        return AVERROR_EOF;
64
65
12.5k
    size = BLOCK_SIZE * par->ch_layout.nb_channels;
66
67
12.5k
    pkt->pos = avio_tell(s->pb);
68
12.5k
    pkt->stream_index = 0;
69
70
12.5k
    ret = av_get_packet(s->pb, pkt, size * 128);
71
12.5k
    if (ret < 0)
72
161
        return ret;
73
12.3k
    if ((ret % size) && ret >= size) {
74
255
        size = ret - (ret % size);
75
255
        av_shrink_packet(pkt, size);
76
255
        pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
77
12.0k
    } else if (ret < size) {
78
432
        return AVERROR_INVALIDDATA;
79
11.6k
    } else {
80
11.6k
        size = ret;
81
11.6k
    }
82
83
11.9k
    pkt->duration = size / (BLOCK_SIZE * par->ch_layout.nb_channels);
84
11.9k
    pkt->pts      = (pkt->pos - c->header_size) / (BLOCK_SIZE * par->ch_layout.nb_channels);
85
86
11.9k
    return 0;
87
12.3k
}
88
89
static int adx_read_header(AVFormatContext *s)
90
1.56k
{
91
1.56k
    ADXDemuxerContext *c = s->priv_data;
92
1.56k
    AVCodecParameters *par;
93
1.56k
    FFStream *sti;
94
1.56k
    int ret;
95
1.56k
    int channels;
96
97
1.56k
    AVStream *st = avformat_new_stream(s, NULL);
98
1.56k
    if (!st)
99
0
        return AVERROR(ENOMEM);
100
1.56k
    par = s->streams[0]->codecpar;
101
102
1.56k
    if (avio_rb16(s->pb) != 0x8000)
103
106
        return AVERROR_INVALIDDATA;
104
1.46k
    c->header_size = avio_rb16(s->pb) + 4;
105
1.46k
    avio_seek(s->pb, -4, SEEK_CUR);
106
107
1.46k
    if ((ret = ff_get_extradata(s, par, s->pb, c->header_size)) < 0)
108
193
        return ret;
109
110
1.26k
    if (par->extradata_size < 12) {
111
16
        av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
112
16
        return AVERROR_INVALIDDATA;
113
16
    }
114
1.25k
    channels = AV_RB8 (par->extradata + 7);
115
1.25k
    par->sample_rate = AV_RB32(par->extradata + 8);
116
117
1.25k
    if (channels <= 0) {
118
5
        av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", channels);
119
5
        return AVERROR_INVALIDDATA;
120
5
    }
121
122
1.24k
    if (par->sample_rate <= 0) {
123
39
        av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate);
124
39
        return AVERROR_INVALIDDATA;
125
39
    }
126
127
1.20k
    sti = ffstream(st);
128
1.20k
    par->ch_layout.nb_channels = channels;
129
1.20k
    par->codec_type  = AVMEDIA_TYPE_AUDIO;
130
1.20k
    switch (par->extradata[4]) {
131
978
    case 3:
132
978
        sti->need_parsing = AVSTREAM_PARSE_FULL_RAW;
133
978
        par->codec_id = AV_CODEC_ID_ADPCM_ADX;
134
978
        par->bit_rate    = (int64_t)par->sample_rate * par->ch_layout.nb_channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES;
135
978
        avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate);
136
978
        break;
137
215
    case 16:
138
215
        sti->need_parsing = AVSTREAM_PARSE_FULL_RAW;
139
215
        par->codec_id = AV_CODEC_ID_AHX;
140
215
        avpriv_set_pts_info(st, 64, 1, par->sample_rate);
141
215
        break;
142
15
    default:
143
15
        av_log(s, AV_LOG_ERROR, "Unsupported format: %d\n", par->extradata[4]);
144
15
        return AVERROR_INVALIDDATA;
145
1.20k
    }
146
147
1.19k
    return 0;
148
1.20k
}
149
150
const FFInputFormat ff_adx_demuxer = {
151
    .p.name         = "adx",
152
    .p.long_name    = NULL_IF_CONFIG_SMALL("CRI ADX"),
153
    .p.extensions   = "adx",
154
    .p.priv_class   = &ff_raw_demuxer_class,
155
    .p.flags        = AVFMT_GENERIC_INDEX,
156
    .read_probe     = adx_probe,
157
    .priv_data_size = sizeof(ADXDemuxerContext),
158
    .read_header    = adx_read_header,
159
    .read_packet    = adx_read_packet,
160
};