Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/s302m.c
Line
Count
Source
1
/*
2
 * SMPTE 302M decoder
3
 * Copyright (c) 2008 Laurent Aimar <fenrir@videolan.org>
4
 * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
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
#include "libavutil/channel_layout.h"
24
#include "libavutil/intreadwrite.h"
25
#include "libavutil/opt.h"
26
#include "libavutil/log.h"
27
#include "libavutil/reverse.h"
28
#include "avcodec.h"
29
#include "codec_internal.h"
30
#include "decode.h"
31
32
632k
#define AES3_HEADER_LEN 4
33
34
typedef struct S302Context {
35
    AVClass *class;
36
    int non_pcm_mode;
37
} S302Context;
38
39
static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf,
40
                                    int buf_size)
41
271k
{
42
271k
    uint32_t h;
43
271k
    int frame_size, channels, bits;
44
45
271k
    if (buf_size <= AES3_HEADER_LEN) {
46
128k
        av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
47
128k
        return AVERROR_INVALIDDATA;
48
128k
    }
49
50
    /*
51
     * AES3 header :
52
     * size:            16
53
     * number channels   2
54
     * channel_id        8
55
     * bits per samples  2
56
     * alignments        4
57
     */
58
59
143k
    h = AV_RB32(buf);
60
143k
    frame_size =  (h >> 16) & 0xffff;
61
143k
    channels   = ((h >> 14) & 0x0003) * 2 +  2;
62
143k
    bits       = ((h >>  4) & 0x0003) * 4 + 16;
63
64
143k
    if (AES3_HEADER_LEN + frame_size != buf_size || bits > 24) {
65
34.6k
        av_log(avctx, AV_LOG_ERROR, "frame has invalid header\n");
66
34.6k
        return AVERROR_INVALIDDATA;
67
34.6k
    }
68
69
    /* Set output properties */
70
108k
    avctx->bits_per_raw_sample = bits;
71
108k
    if (bits > 16)
72
68.8k
        avctx->sample_fmt = AV_SAMPLE_FMT_S32;
73
39.8k
    else
74
39.8k
        avctx->sample_fmt = AV_SAMPLE_FMT_S16;
75
76
108k
    av_channel_layout_uninit(&avctx->ch_layout);
77
108k
    switch(channels) {
78
103k
        case 2:
79
103k
            avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
80
103k
            break;
81
1.17k
        case 4:
82
1.17k
            avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD;
83
1.17k
            break;
84
1.54k
        case 6:
85
1.54k
            avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK;
86
1.54k
            break;
87
2.24k
        case 8:
88
2.24k
            av_channel_layout_from_mask(&avctx->ch_layout,
89
2.24k
                                        AV_CH_LAYOUT_5POINT1_BACK | AV_CH_LAYOUT_STEREO_DOWNMIX);
90
2.24k
            break;
91
0
        default:
92
0
            avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
93
0
            avctx->ch_layout.nb_channels = channels;
94
0
            break;
95
108k
    }
96
97
108k
    return frame_size;
98
108k
}
99
100
static int s302m_decode_frame(AVCodecContext *avctx, AVFrame *frame,
101
                              int *got_frame_ptr, AVPacket *avpkt)
102
271k
{
103
271k
    S302Context *s = avctx->priv_data;
104
271k
    const uint8_t *buf = avpkt->data;
105
271k
    int buf_size       = avpkt->size;
106
271k
    int block_size, ret, channels;
107
271k
    int i;
108
271k
    int non_pcm_data_type = -1;
109
110
271k
    int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
111
271k
    if (frame_size < 0)
112
162k
        return frame_size;
113
114
108k
    buf_size -= AES3_HEADER_LEN;
115
108k
    buf      += AES3_HEADER_LEN;
116
117
    /* get output buffer */
118
108k
    block_size = (avctx->bits_per_raw_sample + 4) / 4;
119
108k
    channels = avctx->ch_layout.nb_channels;
120
108k
    frame->nb_samples = 2 * (buf_size / block_size) / channels;
121
108k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
122
5.67k
        return ret;
123
124
102k
    avctx->bit_rate = 48000 * channels * (avctx->bits_per_raw_sample + 4) +
125
102k
                      32 * 48000 / frame->nb_samples;
126
102k
    buf_size = (frame->nb_samples * channels / 2) * block_size;
127
128
102k
    if (avctx->bits_per_raw_sample == 24) {
129
3.94k
        uint32_t *o = (uint32_t *)frame->data[0];
130
30.2k
        for (; buf_size > 6; buf_size -= 7) {
131
26.2k
            *o++ = ((unsigned)ff_reverse[buf[2]]        << 24) |
132
26.2k
                   (ff_reverse[buf[1]]        << 16) |
133
26.2k
                   (ff_reverse[buf[0]]        <<  8);
134
26.2k
            *o++ = ((unsigned)ff_reverse[buf[6] & 0xf0] << 28) |
135
26.2k
                   (ff_reverse[buf[5]]        << 20) |
136
26.2k
                   (ff_reverse[buf[4]]        << 12) |
137
26.2k
                   (ff_reverse[buf[3] & 0x0f] <<  4);
138
26.2k
            buf += 7;
139
26.2k
        }
140
3.94k
        o = (uint32_t *)frame->data[0];
141
3.94k
        if (channels == 2)
142
4.54k
            for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
143
3.60k
                if (o[i] || o[i+1] || o[i+2] || o[i+3])
144
1.67k
                    break;
145
1.93k
                if (o[i+4] == 0x96F87200U && o[i+5] == 0xA54E1F00) {
146
1.07k
                    non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
147
1.07k
                    break;
148
1.07k
                }
149
1.93k
            }
150
99.0k
    } else if (avctx->bits_per_raw_sample == 20) {
151
62.9k
        uint32_t *o = (uint32_t *)frame->data[0];
152
222k
        for (; buf_size > 5; buf_size -= 6) {
153
159k
            *o++ = ((unsigned)ff_reverse[buf[2] & 0xf0] << 28) |
154
159k
                   (ff_reverse[buf[1]]        << 20) |
155
159k
                   (ff_reverse[buf[0]]        << 12);
156
159k
            *o++ = ((unsigned)ff_reverse[buf[5] & 0xf0] << 28) |
157
159k
                   (ff_reverse[buf[4]]        << 20) |
158
159k
                   (ff_reverse[buf[3]]        << 12);
159
159k
            buf += 6;
160
159k
        }
161
62.9k
        o = (uint32_t *)frame->data[0];
162
62.9k
        if (channels == 2)
163
84.4k
            for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
164
24.5k
                if (o[i] || o[i+1] || o[i+2] || o[i+3])
165
2.37k
                    break;
166
22.2k
                if (o[i+4] == 0x6F872000U && o[i+5] == 0x54E1F000) {
167
194
                    non_pcm_data_type = (o[i+6] >> 16) & 0x1F;
168
194
                    break;
169
194
                }
170
22.2k
            }
171
62.9k
    } else {
172
36.1k
        uint16_t *o = (uint16_t *)frame->data[0];
173
115k
        for (; buf_size > 4; buf_size -= 5) {
174
79.8k
            *o++ = (ff_reverse[buf[1]]        <<  8) |
175
79.8k
                    ff_reverse[buf[0]];
176
79.8k
            *o++ = (ff_reverse[buf[4] & 0xf0] << 12) |
177
79.8k
                   (ff_reverse[buf[3]]        <<  4) |
178
79.8k
                   (ff_reverse[buf[2]]        >>  4);
179
79.8k
            buf += 5;
180
79.8k
        }
181
36.1k
        o = (uint16_t *)frame->data[0];
182
36.1k
        if (channels == 2)
183
37.6k
            for (i=0; i<frame->nb_samples * 2 - 6; i+=2) {
184
4.60k
                if (o[i] || o[i+1] || o[i+2] || o[i+3])
185
2.59k
                    break;
186
2.00k
                if (o[i+4] == 0xF872U && o[i+5] == 0x4E1F) {
187
199
                    non_pcm_data_type = (o[i+6] & 0x1F);
188
199
                    break;
189
199
                }
190
2.00k
            }
191
36.1k
    }
192
193
102k
    if (non_pcm_data_type != -1) {
194
1.46k
        if (s->non_pcm_mode == 3) {
195
1.46k
            av_log(avctx, AV_LOG_ERROR,
196
1.46k
                   "S302 non PCM mode with data type %d not supported\n",
197
1.46k
                   non_pcm_data_type);
198
1.46k
            return AVERROR_PATCHWELCOME;
199
1.46k
        }
200
0
        if (s->non_pcm_mode & 1) {
201
0
            return avpkt->size;
202
0
        }
203
0
    }
204
205
101k
    avctx->sample_rate = 48000;
206
207
101k
    *got_frame_ptr = 1;
208
209
101k
    return avpkt->size;
210
102k
}
211
212
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_DECODING_PARAM
213
static const AVOption s302m_options[] = {
214
    {"non_pcm_mode", "Chooses what to do with NON-PCM", offsetof(S302Context, non_pcm_mode), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
215
    {"copy"        , "Pass NON-PCM through unchanged"     , 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
216
    {"drop"        , "Drop NON-PCM"                       , 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
217
    {"decode_copy" , "Decode if possible else passthrough", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
218
    {"decode_drop" , "Decode if possible else drop"       , 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 3, FLAGS, .unit = "non_pcm_mode"},
219
    {NULL}
220
};
221
222
static const AVClass s302m_class = {
223
    .class_name = "SMPTE 302M Decoder",
224
    .item_name  = av_default_item_name,
225
    .option     = s302m_options,
226
    .version    = LIBAVUTIL_VERSION_INT,
227
};
228
229
const FFCodec ff_s302m_decoder = {
230
    .p.name         = "s302m",
231
    CODEC_LONG_NAME("SMPTE 302M"),
232
    .p.type         = AVMEDIA_TYPE_AUDIO,
233
    .p.id           = AV_CODEC_ID_S302M,
234
    .p.priv_class   = &s302m_class,
235
    .priv_data_size = sizeof(S302Context),
236
    FF_CODEC_DECODE_CB(s302m_decode_frame),
237
    .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
238
                      AV_CODEC_CAP_DR1,
239
};