FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/s302menc.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 48 93 51.6%
Branches: 16 49 32.7%

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