Coverage Report

Created: 2025-11-16 07:20

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
23.5k
#define BLOCK_SIZE    18
33
1.54k
#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
964k
{
42
964k
    int offset;
43
964k
    if (AV_RB16(p->buf) != 0x8000)
44
963k
        return 0;
45
1.22k
    offset = AV_RB16(&p->buf[2]);
46
1.22k
    if (   offset < 8
47
934
        || offset > p->buf_size - 4
48
402
        || memcmp(p->buf + offset - 2, "(c)CRI", 6))
49
1.20k
        return 0;
50
27
    return AVPROBE_SCORE_MAX * 3 / 4;
51
1.22k
}
52
53
static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
54
28.0k
{
55
28.0k
    ADXDemuxerContext *c = s->priv_data;
56
28.0k
    AVCodecParameters *par = s->streams[0]->codecpar;
57
28.0k
    int ret, size;
58
59
28.0k
    if (par->codec_id == AV_CODEC_ID_AHX)
60
19.5k
        return ff_raw_read_partial_packet(s, pkt);
61
62
8.41k
    if (avio_feof(s->pb))
63
538
        return AVERROR_EOF;
64
65
7.87k
    size = BLOCK_SIZE * par->ch_layout.nb_channels;
66
67
7.87k
    pkt->pos = avio_tell(s->pb);
68
7.87k
    pkt->stream_index = 0;
69
70
7.87k
    ret = av_get_packet(s->pb, pkt, size * 128);
71
7.87k
    if (ret < 0)
72
91
        return ret;
73
7.78k
    if ((ret % size) && ret >= size) {
74
265
        size = ret - (ret % size);
75
265
        av_shrink_packet(pkt, size);
76
265
        pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
77
7.51k
    } else if (ret < size) {
78
307
        return AVERROR(EIO);
79
7.21k
    } else {
80
7.21k
        size = ret;
81
7.21k
    }
82
83
7.47k
    pkt->duration = size / (BLOCK_SIZE * par->ch_layout.nb_channels);
84
7.47k
    pkt->pts      = (pkt->pos - c->header_size) / (BLOCK_SIZE * par->ch_layout.nb_channels);
85
86
7.47k
    return 0;
87
7.78k
}
88
89
static int adx_read_header(AVFormatContext *s)
90
1.37k
{
91
1.37k
    ADXDemuxerContext *c = s->priv_data;
92
1.37k
    AVCodecParameters *par;
93
1.37k
    FFStream *sti;
94
1.37k
    int ret;
95
1.37k
    int channels;
96
97
1.37k
    AVStream *st = avformat_new_stream(s, NULL);
98
1.37k
    if (!st)
99
0
        return AVERROR(ENOMEM);
100
1.37k
    par = s->streams[0]->codecpar;
101
102
1.37k
    if (avio_rb16(s->pb) != 0x8000)
103
128
        return AVERROR_INVALIDDATA;
104
1.24k
    c->header_size = avio_rb16(s->pb) + 4;
105
1.24k
    avio_seek(s->pb, -4, SEEK_CUR);
106
107
1.24k
    if ((ret = ff_get_extradata(s, par, s->pb, c->header_size)) < 0)
108
186
        return ret;
109
110
1.06k
    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.04k
    channels = AV_RB8 (par->extradata + 7);
115
1.04k
    par->sample_rate = AV_RB32(par->extradata + 8);
116
117
1.04k
    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.03k
    if (par->sample_rate <= 0) {
123
37
        av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate);
124
37
        return AVERROR_INVALIDDATA;
125
37
    }
126
127
1.00k
    sti = ffstream(st);
128
1.00k
    par->ch_layout.nb_channels = channels;
129
1.00k
    par->codec_type  = AVMEDIA_TYPE_AUDIO;
130
1.00k
    switch (par->extradata[4]) {
131
773
    case 3:
132
773
        sti->need_parsing = AVSTREAM_PARSE_FULL_RAW;
133
773
        par->codec_id = AV_CODEC_ID_ADPCM_ADX;
134
773
        par->bit_rate    = (int64_t)par->sample_rate * par->ch_layout.nb_channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES;
135
773
        avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate);
136
773
        break;
137
179
    case 16:
138
179
        sti->need_parsing = AVSTREAM_PARSE_FULL_RAW;
139
179
        par->codec_id = AV_CODEC_ID_AHX;
140
179
        avpriv_set_pts_info(st, 64, 1, par->sample_rate);
141
179
        break;
142
50
    default:
143
50
        av_log(s, AV_LOG_ERROR, "Unsupported format: %d\n", par->extradata[4]);
144
50
        return AVERROR_INVALIDDATA;
145
1.00k
    }
146
147
952
    return 0;
148
1.00k
}
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
};