FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/nistspheredec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 44 77 57.1%
Functions: 2 2 100.0%
Branches: 26 64 40.6%

Line Branch Exec Source
1 /*
2 * NIST Sphere demuxer
3 * Copyright (c) 2012 Paul B Mahol
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 #include "libavutil/avstring.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "demux.h"
26 #include "internal.h"
27 #include "pcm.h"
28
29 7186 static int nist_probe(const AVProbeData *p)
30 {
31
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7185 times.
7186 if (AV_RL64(p->buf) == AV_RL64("NIST_1A\x0a"))
32 1 return AVPROBE_SCORE_MAX;
33 7185 return 0;
34 }
35
36 1 static int nist_read_header(AVFormatContext *s)
37 {
38 1 char buffer[256]= {0}, coding[32] = "pcm", format[32] = "01";
39 1 int bps = 0, be = 0;
40 1 int32_t header_size = -1;
41 AVStream *st;
42
43 1 st = avformat_new_stream(s, NULL);
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
45 return AVERROR(ENOMEM);
46
47 1 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
48
49 1 ff_get_line(s->pb, buffer, sizeof(buffer));
50 1 ff_get_line(s->pb, buffer, sizeof(buffer));
51 1 sscanf(buffer, "%"SCNd32, &header_size);
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (header_size <= 0)
53 return AVERROR_INVALIDDATA;
54
55
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 while (!avio_feof(s->pb)) {
56 6 ff_get_line(s->pb, buffer, sizeof(buffer));
57
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (avio_tell(s->pb) >= header_size)
59 return AVERROR_INVALIDDATA;
60
61
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (!memcmp(buffer, "end_head", 8)) {
62
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!st->codecpar->bits_per_coded_sample)
63 1 st->codecpar->bits_per_coded_sample = bps << 3;
64
65
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (!av_strcasecmp(coding, "pcm")) {
66 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
67 st->codecpar->codec_id = ff_get_pcm_codec_id(st->codecpar->bits_per_coded_sample,
68 0, be, 0xFFFF);
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 } else if (!av_strcasecmp(coding, "alaw")) {
70 st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW;
71
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 } else if (!av_strcasecmp(coding, "ulaw") ||
72 !av_strcasecmp(coding, "mu-law")) {
73 1 st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW;
74 } else if (!av_strncasecmp(coding, "pcm,embedded-shorten", 20)) {
75 st->codecpar->codec_id = AV_CODEC_ID_SHORTEN;
76 if (ff_alloc_extradata(st->codecpar, 1))
77 st->codecpar->extradata[0] = 1;
78 } else {
79 avpriv_request_sample(s, "coding %s", coding);
80 }
81
82 1 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
83
84 1 st->codecpar->block_align = st->codecpar->bits_per_coded_sample *
85 1 st->codecpar->ch_layout.nb_channels / 8;
86
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (avio_tell(s->pb) > header_size)
88 return AVERROR_INVALIDDATA;
89
90 1 avio_skip(s->pb, header_size - avio_tell(s->pb));
91
92 1 return 0;
93
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
5 } else if (!memcmp(buffer, "channel_count", 13)) {
94 1 sscanf(buffer, "%*s %*s %u", &st->codecpar->ch_layout.nb_channels);
95
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (st->codecpar->ch_layout.nb_channels <= 0 || st->codecpar->ch_layout.nb_channels > INT16_MAX)
96 return AVERROR_INVALIDDATA;
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } else if (!memcmp(buffer, "sample_byte_format", 18)) {
98 sscanf(buffer, "%*s %*s %31s", format);
99
100 if (!av_strcasecmp(format, "01")) {
101 be = 0;
102 } else if (!av_strcasecmp(format, "10")) {
103 be = 1;
104 } else if (!av_strcasecmp(format, "mu-law")) {
105 st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW;
106 } else if (av_strcasecmp(format, "1")) {
107 avpriv_request_sample(s, "sample byte format %s", format);
108 return AVERROR_PATCHWELCOME;
109 }
110
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 } else if (!memcmp(buffer, "sample_coding", 13)) {
111 1 sscanf(buffer, "%*s %*s %31s", coding);
112
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 } else if (!memcmp(buffer, "sample_count", 12)) {
113 1 sscanf(buffer, "%*s %*s %"SCNd64, &st->duration);
114
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 } else if (!memcmp(buffer, "sample_n_bytes", 14)) {
115 1 sscanf(buffer, "%*s %*s %d", &bps);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (bps > INT16_MAX/8U)
117 return AVERROR_INVALIDDATA;
118
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (!memcmp(buffer, "sample_rate", 11)) {
119 1 sscanf(buffer, "%*s %*s %d", &st->codecpar->sample_rate);
120 } else if (!memcmp(buffer, "sample_sig_bits", 15)) {
121 sscanf(buffer, "%*s %*s %d", &st->codecpar->bits_per_coded_sample);
122 if (st->codecpar->bits_per_coded_sample <= 0 || st->codecpar->bits_per_coded_sample > INT16_MAX)
123 return AVERROR_INVALIDDATA;
124 } else {
125 char key[32], value[32];
126 if (sscanf(buffer, "%31s %*s %31s", key, value) == 2) {
127 av_dict_set(&s->metadata, key, value, AV_DICT_APPEND);
128 } else {
129 av_log(s, AV_LOG_ERROR, "Failed to parse '%s' as metadata\n", buffer);
130 }
131 }
132 }
133
134 return AVERROR_EOF;
135 }
136
137 const FFInputFormat ff_nistsphere_demuxer = {
138 .p.name = "nistsphere",
139 .p.long_name = NULL_IF_CONFIG_SMALL("NIST SPeech HEader REsources"),
140 .p.extensions = "nist,sph",
141 .p.flags = AVFMT_GENERIC_INDEX,
142 .read_probe = nist_probe,
143 .read_header = nist_read_header,
144 .read_packet = ff_pcm_read_packet,
145 .read_seek = ff_pcm_read_seek,
146 };
147