Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavcodec/s302menc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * SMPTE 302M encoder
3
 * Copyright (c) 2010 Google, Inc.
4
 * Copyright (c) 2013 Darryl Wallace <wallacdj@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/reverse.h"
25
#include "avcodec.h"
26
#include "codec_internal.h"
27
#include "encode.h"
28
#include "mathops.h"
29
#include "put_bits.h"
30
31
0
#define AES3_HEADER_LEN 4
32
33
typedef struct S302MEncContext {
34
    uint8_t framing_index; /* Set for even channels on multiple of 192 samples */
35
} S302MEncContext;
36
37
static av_cold int s302m_encode_init(AVCodecContext *avctx)
38
0
{
39
0
    S302MEncContext *s = avctx->priv_data;
40
41
0
    if (avctx->ch_layout.nb_channels & 1 || avctx->ch_layout.nb_channels > 8) {
42
0
        av_log(avctx, AV_LOG_ERROR,
43
0
               "Encoding %d channel(s) is not allowed. Only 2, 4, 6 and 8 channels are supported.\n",
44
0
               avctx->ch_layout.nb_channels);
45
0
        return AVERROR(EINVAL);
46
0
    }
47
48
0
    switch (avctx->sample_fmt) {
49
0
    case AV_SAMPLE_FMT_S16:
50
0
        avctx->bits_per_raw_sample = 16;
51
0
        break;
52
0
    case AV_SAMPLE_FMT_S32:
53
0
        if (avctx->bits_per_raw_sample > 20) {
54
0
            if (avctx->bits_per_raw_sample > 24)
55
0
                av_log(avctx, AV_LOG_WARNING, "encoding as 24 bits-per-sample\n");
56
0
            avctx->bits_per_raw_sample = 24;
57
0
        } else if (!avctx->bits_per_raw_sample) {
58
0
            avctx->bits_per_raw_sample = 24;
59
0
        } else if (avctx->bits_per_raw_sample <= 20) {
60
0
            avctx->bits_per_raw_sample = 20;
61
0
        }
62
0
    }
63
64
0
    avctx->frame_size = 0;
65
0
    avctx->bit_rate   = 48000 * avctx->ch_layout.nb_channels *
66
0
                       (avctx->bits_per_raw_sample + 4);
67
0
    s->framing_index  = 0;
68
69
0
    return 0;
70
0
}
71
72
static int s302m_encode2_frame(AVCodecContext *avctx, AVPacket *avpkt,
73
                               const AVFrame *frame, int *got_packet_ptr)
74
0
{
75
0
    S302MEncContext *s = avctx->priv_data;
76
0
    const int nb_channels = avctx->ch_layout.nb_channels;
77
0
    const int buf_size = AES3_HEADER_LEN +
78
0
                        (frame->nb_samples * nb_channels *
79
0
                        (avctx->bits_per_raw_sample + 4)) / 8;
80
0
    int ret, c, channels;
81
0
    uint8_t *o;
82
0
    PutBitContext pb;
83
84
0
    if (buf_size - AES3_HEADER_LEN > UINT16_MAX) {
85
0
        av_log(avctx, AV_LOG_ERROR, "number of samples in frame too big\n");
86
0
        return AVERROR(EINVAL);
87
0
    }
88
89
0
    if ((ret = ff_get_encode_buffer(avctx, avpkt, buf_size, 0)) < 0)
90
0
        return ret;
91
92
0
    o = avpkt->data;
93
0
    init_put_bits(&pb, o, buf_size);
94
0
    put_bits(&pb, 16, buf_size - AES3_HEADER_LEN);
95
0
    put_bits(&pb, 2, (nb_channels - 2) >> 1);   // number of channels
96
0
    put_bits(&pb, 8, 0);                            // channel ID
97
0
    put_bits(&pb, 2, (avctx->bits_per_raw_sample - 16) / 4); // bits per samples (0 = 16bit, 1 = 20bit, 2 = 24bit)
98
0
    put_bits(&pb, 4, 0);                            // alignments
99
0
    flush_put_bits(&pb);
100
0
    o += AES3_HEADER_LEN;
101
102
0
    if (avctx->bits_per_raw_sample == 24) {
103
0
        const uint32_t *samples = (uint32_t *)frame->data[0];
104
105
0
        for (c = 0; c < frame->nb_samples; c++) {
106
0
            uint8_t vucf = s->framing_index == 0 ? 0x10: 0;
107
108
0
            for (channels = 0; channels < nb_channels; channels += 2) {
109
0
                o[0] = ff_reverse[(samples[0] & 0x0000FF00) >> 8];
110
0
                o[1] = ff_reverse[(samples[0] & 0x00FF0000) >> 16];
111
0
                o[2] = ff_reverse[(samples[0] & 0xFF000000) >> 24];
112
0
                o[3] = ff_reverse[(samples[1] & 0x00000F00) >> 4] | vucf;
113
0
                o[4] = ff_reverse[(samples[1] & 0x000FF000) >> 12];
114
0
                o[5] = ff_reverse[(samples[1] & 0x0FF00000) >> 20];
115
0
                o[6] = ff_reverse[(samples[1] & 0xF0000000) >> 28];
116
0
                o += 7;
117
0
                samples += 2;
118
0
            }
119
120
0
            s->framing_index++;
121
0
            if (s->framing_index >= 192)
122
0
                s->framing_index = 0;
123
0
        }
124
0
    } else if (avctx->bits_per_raw_sample == 20) {
125
0
        const uint32_t *samples = (uint32_t *)frame->data[0];
126
127
0
        for (c = 0; c < frame->nb_samples; c++) {
128
0
            uint8_t vucf = s->framing_index == 0 ? 0x80: 0;
129
130
0
            for (channels = 0; channels < nb_channels; channels += 2) {
131
0
                o[0] = ff_reverse[ (samples[0] & 0x000FF000) >> 12];
132
0
                o[1] = ff_reverse[ (samples[0] & 0x0FF00000) >> 20];
133
0
                o[2] = ff_reverse[((samples[0] & 0xF0000000) >> 28) | vucf];
134
0
                o[3] = ff_reverse[ (samples[1] & 0x000FF000) >> 12];
135
0
                o[4] = ff_reverse[ (samples[1] & 0x0FF00000) >> 20];
136
0
                o[5] = ff_reverse[ (samples[1] & 0xF0000000) >> 28];
137
0
                o += 6;
138
0
                samples += 2;
139
0
            }
140
141
0
            s->framing_index++;
142
0
            if (s->framing_index >= 192)
143
0
                s->framing_index = 0;
144
0
        }
145
0
    } else if (avctx->bits_per_raw_sample == 16) {
146
0
        const uint16_t *samples = (uint16_t *)frame->data[0];
147
148
0
        for (c = 0; c < frame->nb_samples; c++) {
149
0
            uint8_t vucf = s->framing_index == 0 ? 0x10 : 0;
150
151
0
            for (channels = 0; channels < nb_channels; channels += 2) {
152
0
                o[0] = ff_reverse[ samples[0] & 0xFF];
153
0
                o[1] = ff_reverse[(samples[0] & 0xFF00) >>  8];
154
0
                o[2] = ff_reverse[(samples[1] & 0x0F)   <<  4] | vucf;
155
0
                o[3] = ff_reverse[(samples[1] & 0x0FF0) >>  4];
156
0
                o[4] = ff_reverse[(samples[1] & 0xF000) >> 12];
157
0
                o += 5;
158
0
                samples += 2;
159
160
0
            }
161
162
0
            s->framing_index++;
163
0
            if (s->framing_index >= 192)
164
0
                s->framing_index = 0;
165
0
        }
166
0
    }
167
168
0
    *got_packet_ptr = 1;
169
170
0
    return 0;
171
0
}
172
173
const FFCodec ff_s302m_encoder = {
174
    .p.name                = "s302m",
175
    CODEC_LONG_NAME("SMPTE 302M"),
176
    .p.type                = AVMEDIA_TYPE_AUDIO,
177
    .p.id                  = AV_CODEC_ID_S302M,
178
    .p.capabilities        = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_EXPERIMENTAL |
179
                             AV_CODEC_CAP_VARIABLE_FRAME_SIZE             |
180
                             AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
181
    .priv_data_size        = sizeof(S302MEncContext),
182
    .init                  = s302m_encode_init,
183
    FF_CODEC_ENCODE_CB(s302m_encode2_frame),
184
    CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16),
185
    CODEC_SAMPLERATES(48000),
186
};