FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/s302m.c
Date: 2022-07-06 18:02:43
Exec Total Coverage
Lines: 53 111 47.7%
Branches: 17 81 21.0%

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