FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pcm-dvd.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 80 145 55.2%
Branches: 27 57 47.4%

Line Branch Exec Source
1 /*
2 * LPCM codecs for PCM formats found in Video DVD streams
3 * Copyright (c) 2013 Christian Schmidt
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * LPCM codecs for PCM formats found in Video DVD streams
25 */
26
27 #include "avcodec.h"
28 #include "bytestream.h"
29 #include "internal.h"
30
31 typedef struct PCMDVDContext {
32 uint32_t last_header; // Cached header to see if parsing is needed
33 int block_size; // Size of a block of samples in bytes
34 int last_block_size; // Size of the last block of samples in bytes
35 int samples_per_block; // Number of samples per channel per block
36 int groups_per_block; // Number of 20/24-bit sample groups per block
37 int extra_sample_count; // Number of leftover samples in the buffer
38 uint8_t extra_samples[8 * 3 * 4]; // Space for leftover samples from a frame
39 // (8 channels, 3B/sample, 4 samples/block)
40 } PCMDVDContext;
41
42 4 static av_cold int pcm_dvd_decode_init(AVCodecContext *avctx)
43 {
44 4 PCMDVDContext *s = avctx->priv_data;
45
46 /* Invalid header to force parsing of the first header */
47 4 s->last_header = -1;
48
49 4 return 0;
50 }
51
52 126 static int pcm_dvd_parse_header(AVCodecContext *avctx, const uint8_t *header)
53 {
54 /* no traces of 44100 and 32000Hz in any commercial software or player */
55 static const uint32_t frequencies[4] = { 48000, 96000, 44100, 32000 };
56 126 PCMDVDContext *s = avctx->priv_data;
57 126 int header_int = (header[0] & 0xe0) | (header[1] << 8) | (header[2] << 16);
58
59 /* early exit if the header didn't change apart from the frame number */
60
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 4 times.
126 if (s->last_header == header_int)
61 122 return 0;
62 4 s->last_header = -1;
63
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (avctx->debug & FF_DEBUG_PICT_INFO)
65 av_log(avctx, AV_LOG_DEBUG, "pcm_dvd_parse_header: header = %02x%02x%02x\n",
66 header[0], header[1], header[2]);
67 /*
68 * header[0] emphasis (1), muse(1), reserved(1), frame number(5)
69 * header[1] quant (2), freq(2), reserved(1), channels(3)
70 * header[2] dynamic range control (0x80 = off)
71 */
72
73 /* Discard potentially existing leftover samples from old channel layout */
74 4 s->extra_sample_count = 0;
75
76 /* get the sample depth and derive the sample format from it */
77 4 avctx->bits_per_coded_sample = 16 + (header[1] >> 6 & 3) * 4;
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (avctx->bits_per_coded_sample == 28) {
79 av_log(avctx, AV_LOG_ERROR,
80 "PCM DVD unsupported sample depth %i\n",
81 avctx->bits_per_coded_sample);
82 return AVERROR_INVALIDDATA;
83 }
84 8 avctx->sample_fmt = avctx->bits_per_coded_sample == 16 ? AV_SAMPLE_FMT_S16
85
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 : AV_SAMPLE_FMT_S32;
86 4 avctx->bits_per_raw_sample = avctx->bits_per_coded_sample;
87
88 /* get the sample rate */
89 4 avctx->sample_rate = frequencies[header[1] >> 4 & 3];
90
91 /* get the number of channels */
92 4 avctx->channels = 1 + (header[1] & 7);
93 /* calculate the bitrate */
94 4 avctx->bit_rate = avctx->channels *
95 4 avctx->sample_rate *
96 4 avctx->bits_per_coded_sample;
97
98 /* 4 samples form a group in 20/24-bit PCM on DVD Video.
99 * A block is formed by the number of groups that are
100 * needed to complete a set of samples for each channel. */
101
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (avctx->bits_per_coded_sample == 16) {
102 2 s->samples_per_block = 1;
103 2 s->block_size = avctx->channels * 2;
104 } else {
105
1/3
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2 switch (avctx->channels) {
106 2 case 1:
107 case 2:
108 case 4:
109 /* one group has all the samples needed */
110 2 s->block_size = 4 * avctx->bits_per_coded_sample / 8;
111 2 s->samples_per_block = 4 / avctx->channels;
112 2 s->groups_per_block = 1;
113 2 break;
114 case 8:
115 /* two groups have all the samples needed */
116 s->block_size = 8 * avctx->bits_per_coded_sample / 8;
117 s->samples_per_block = 1;
118 s->groups_per_block = 2;
119 break;
120 default:
121 /* need avctx->channels groups */
122 s->block_size = 4 * avctx->channels *
123 avctx->bits_per_coded_sample / 8;
124 s->samples_per_block = 4;
125 s->groups_per_block = avctx->channels;
126 break;
127 }
128 }
129
130 4 if (avctx->debug & FF_DEBUG_PICT_INFO)
131 ff_dlog(avctx,
132 "pcm_dvd_parse_header: %d channels, %d bits per sample, %d Hz, %"PRId64" bit/s\n",
133 avctx->channels, avctx->bits_per_coded_sample,
134 avctx->sample_rate, avctx->bit_rate);
135
136 4 s->last_header = header_int;
137
138 4 return 0;
139 }
140
141 126 static void *pcm_dvd_decode_samples(AVCodecContext *avctx, const uint8_t *src,
142 void *dst, int blocks)
143 {
144 126 PCMDVDContext *s = avctx->priv_data;
145 126 int16_t *dst16 = dst;
146 126 int32_t *dst32 = dst;
147 GetByteContext gb;
148 int i;
149 uint8_t t;
150
151 126 bytestream2_init(&gb, src, blocks * s->block_size);
152
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 124 times.
✗ Branch 3 not taken.
126 switch (avctx->bits_per_coded_sample) {
153 2 case 16: {
154 #if HAVE_BIGENDIAN
155 bytestream2_get_buffer(&gb, dst16, blocks * s->block_size);
156 dst16 += blocks * s->block_size / 2;
157 #else
158 2 int samples = blocks * avctx->channels;
159 do {
160 2002 *dst16++ = bytestream2_get_be16u(&gb);
161
2/2
✓ Branch 0 taken 2000 times.
✓ Branch 1 taken 2 times.
2002 } while (--samples);
162 #endif
163 2 return dst16;
164 }
165 case 20:
166 if (avctx->channels == 1) {
167 do {
168 for (i = 2; i; i--) {
169 dst32[0] = bytestream2_get_be16u(&gb) << 16;
170 dst32[1] = bytestream2_get_be16u(&gb) << 16;
171 t = bytestream2_get_byteu(&gb);
172 *dst32++ += (t & 0xf0) << 8;
173 *dst32++ += (t & 0x0f) << 12;
174 }
175 } while (--blocks);
176 } else {
177 do {
178 for (i = s->groups_per_block; i; i--) {
179 dst32[0] = bytestream2_get_be16u(&gb) << 16;
180 dst32[1] = bytestream2_get_be16u(&gb) << 16;
181 dst32[2] = bytestream2_get_be16u(&gb) << 16;
182 dst32[3] = bytestream2_get_be16u(&gb) << 16;
183 t = bytestream2_get_byteu(&gb);
184 *dst32++ += (t & 0xf0) << 8;
185 *dst32++ += (t & 0x0f) << 12;
186 t = bytestream2_get_byteu(&gb);
187 *dst32++ += (t & 0xf0) << 8;
188 *dst32++ += (t & 0x0f) << 12;
189 }
190 } while (--blocks);
191 }
192 return dst32;
193 124 case 24:
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 124 times.
124 if (avctx->channels == 1) {
195 do {
196 for (i = 2; i; i--) {
197 dst32[0] = bytestream2_get_be16u(&gb) << 16;
198 dst32[1] = bytestream2_get_be16u(&gb) << 16;
199 *dst32++ += bytestream2_get_byteu(&gb) << 8;
200 *dst32++ += bytestream2_get_byteu(&gb) << 8;
201 }
202 } while (--blocks);
203 } else {
204 do {
205
2/2
✓ Branch 0 taken 20708 times.
✓ Branch 1 taken 20708 times.
41416 for (i = s->groups_per_block; i; i--) {
206 20708 dst32[0] = bytestream2_get_be16u(&gb) << 16;
207 20708 dst32[1] = bytestream2_get_be16u(&gb) << 16;
208 20708 dst32[2] = bytestream2_get_be16u(&gb) << 16;
209 20708 dst32[3] = bytestream2_get_be16u(&gb) << 16;
210 20708 *dst32++ += bytestream2_get_byteu(&gb) << 8;
211 20708 *dst32++ += bytestream2_get_byteu(&gb) << 8;
212 20708 *dst32++ += bytestream2_get_byteu(&gb) << 8;
213 20708 *dst32++ += bytestream2_get_byteu(&gb) << 8;
214 }
215
2/2
✓ Branch 0 taken 20584 times.
✓ Branch 1 taken 124 times.
20708 } while (--blocks);
216 }
217 124 return dst32;
218 default:
219 return NULL;
220 }
221 }
222
223 126 static int pcm_dvd_decode_frame(AVCodecContext *avctx, void *data,
224 int *got_frame_ptr, AVPacket *avpkt)
225 {
226 126 AVFrame *frame = data;
227 126 const uint8_t *src = avpkt->data;
228 126 int buf_size = avpkt->size;
229 126 PCMDVDContext *s = avctx->priv_data;
230 int retval;
231 int blocks;
232 void *dst;
233
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (buf_size < 3) {
235 av_log(avctx, AV_LOG_ERROR, "PCM packet too small\n");
236 return AVERROR_INVALIDDATA;
237 }
238
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
126 if ((retval = pcm_dvd_parse_header(avctx, src)))
240 return retval;
241
3/4
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 122 times.
126 if (s->last_block_size && s->last_block_size != s->block_size) {
242 av_log(avctx, AV_LOG_WARNING, "block_size has changed %d != %d\n", s->last_block_size, s->block_size);
243 s->extra_sample_count = 0;
244 }
245 126 s->last_block_size = s->block_size;
246 126 src += 3;
247 126 buf_size -= 3;
248
249 126 blocks = (buf_size + s->extra_sample_count) / s->block_size;
250
251 /* get output buffer */
252 126 frame->nb_samples = blocks * s->samples_per_block;
253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126 times.
126 if ((retval = ff_get_buffer(avctx, frame, 0)) < 0)
254 return retval;
255 126 dst = frame->data[0];
256
257 /* consume leftover samples from last packet */
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (s->extra_sample_count) {
259 int missing_samples = s->block_size - s->extra_sample_count;
260 if (buf_size >= missing_samples) {
261 memcpy(s->extra_samples + s->extra_sample_count, src,
262 missing_samples);
263 dst = pcm_dvd_decode_samples(avctx, s->extra_samples, dst, 1);
264 src += missing_samples;
265 buf_size -= missing_samples;
266 s->extra_sample_count = 0;
267 blocks--;
268 } else {
269 /* new packet still doesn't have enough samples */
270 memcpy(s->extra_samples + s->extra_sample_count, src, buf_size);
271 s->extra_sample_count += buf_size;
272 return avpkt->size;
273 }
274 }
275
276 /* decode remaining complete samples */
277
1/2
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
126 if (blocks) {
278 126 pcm_dvd_decode_samples(avctx, src, dst, blocks);
279 126 buf_size -= blocks * s->block_size;
280 }
281
282 /* store leftover samples */
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 126 times.
126 if (buf_size) {
284 src += blocks * s->block_size;
285 memcpy(s->extra_samples, src, buf_size);
286 s->extra_sample_count = buf_size;
287 }
288
289 126 *got_frame_ptr = 1;
290
291 126 return avpkt->size;
292 }
293
294 const AVCodec ff_pcm_dvd_decoder = {
295 .name = "pcm_dvd",
296 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 16|20|24-bit big-endian for DVD media"),
297 .type = AVMEDIA_TYPE_AUDIO,
298 .id = AV_CODEC_ID_PCM_DVD,
299 .priv_data_size = sizeof(PCMDVDContext),
300 .init = pcm_dvd_decode_init,
301 .decode = pcm_dvd_decode_frame,
302 .capabilities = AV_CODEC_CAP_CHANNEL_CONF |
303 AV_CODEC_CAP_DR1,
304 .sample_fmts = (const enum AVSampleFormat[]) {
305 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_NONE
306 },
307 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
308 };
309