FFmpeg coverage


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