/src/ffmpeg/libavformat/nistspheredec.c
Line | Count | Source (jump to first uncovered line) |
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 | | static int nist_probe(const AVProbeData *p) |
30 | 924k | { |
31 | 924k | if (AV_RL64(p->buf) == AV_RL64("NIST_1A\x0a")) |
32 | 168 | return AVPROBE_SCORE_MAX; |
33 | 924k | return 0; |
34 | 924k | } |
35 | | |
36 | | static int nist_read_header(AVFormatContext *s) |
37 | 7.78k | { |
38 | 7.78k | char buffer[256]= {0}, coding[32] = "pcm", format[32] = "01"; |
39 | 7.78k | int bps = 0, be = 0; |
40 | 7.78k | int32_t header_size = -1; |
41 | 7.78k | AVStream *st; |
42 | | |
43 | 7.78k | st = avformat_new_stream(s, NULL); |
44 | 7.78k | if (!st) |
45 | 0 | return AVERROR(ENOMEM); |
46 | | |
47 | 7.78k | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
48 | | |
49 | 7.78k | ff_get_line(s->pb, buffer, sizeof(buffer)); |
50 | 7.78k | ff_get_line(s->pb, buffer, sizeof(buffer)); |
51 | 7.78k | sscanf(buffer, "%"SCNd32, &header_size); |
52 | 7.78k | if (header_size <= 0) |
53 | 5.13k | return AVERROR_INVALIDDATA; |
54 | | |
55 | 396k | while (!avio_feof(s->pb)) { |
56 | 396k | ff_get_line(s->pb, buffer, sizeof(buffer)); |
57 | | |
58 | 396k | if (avio_tell(s->pb) >= header_size) |
59 | 60 | return AVERROR_INVALIDDATA; |
60 | | |
61 | 396k | if (!memcmp(buffer, "end_head", 8)) { |
62 | 1.95k | if (!st->codecpar->bits_per_coded_sample) |
63 | 1.08k | st->codecpar->bits_per_coded_sample = bps << 3; |
64 | | |
65 | 1.95k | if (!av_strcasecmp(coding, "pcm")) { |
66 | 1.80k | if (st->codecpar->codec_id == AV_CODEC_ID_NONE) |
67 | 1.80k | st->codecpar->codec_id = ff_get_pcm_codec_id(st->codecpar->bits_per_coded_sample, |
68 | 1.80k | 0, be, 0xFFFF); |
69 | 1.80k | } else if (!av_strcasecmp(coding, "alaw")) { |
70 | 24 | st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW; |
71 | 122 | } else if (!av_strcasecmp(coding, "ulaw") || |
72 | 122 | !av_strcasecmp(coding, "mu-law")) { |
73 | 85 | st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; |
74 | 85 | } else if (!av_strncasecmp(coding, "pcm,embedded-shorten", 20)) { |
75 | 0 | st->codecpar->codec_id = AV_CODEC_ID_SHORTEN; |
76 | 0 | if (ff_alloc_extradata(st->codecpar, 1)) |
77 | 0 | st->codecpar->extradata[0] = 1; |
78 | 37 | } else { |
79 | 37 | avpriv_request_sample(s, "coding %s", coding); |
80 | 37 | } |
81 | | |
82 | 1.95k | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); |
83 | | |
84 | 1.95k | st->codecpar->block_align = st->codecpar->bits_per_coded_sample * |
85 | 1.95k | st->codecpar->ch_layout.nb_channels / 8; |
86 | | |
87 | 1.95k | if (avio_tell(s->pb) > header_size) |
88 | 0 | return AVERROR_INVALIDDATA; |
89 | | |
90 | 1.95k | avio_skip(s->pb, header_size - avio_tell(s->pb)); |
91 | | |
92 | 1.95k | return 0; |
93 | 394k | } else if (!memcmp(buffer, "channel_count", 13)) { |
94 | 1.50k | sscanf(buffer, "%*s %*s %u", &st->codecpar->ch_layout.nb_channels); |
95 | 1.50k | if (st->codecpar->ch_layout.nb_channels <= 0 || st->codecpar->ch_layout.nb_channels > INT16_MAX) |
96 | 54 | return AVERROR_INVALIDDATA; |
97 | 392k | } else if (!memcmp(buffer, "sample_byte_format", 18)) { |
98 | 787 | sscanf(buffer, "%*s %*s %31s", format); |
99 | | |
100 | 787 | if (!av_strcasecmp(format, "01")) { |
101 | 303 | be = 0; |
102 | 484 | } else if (!av_strcasecmp(format, "10")) { |
103 | 244 | be = 1; |
104 | 244 | } else if (!av_strcasecmp(format, "mu-law")) { |
105 | 0 | st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW; |
106 | 240 | } else if (av_strcasecmp(format, "1")) { |
107 | 19 | avpriv_request_sample(s, "sample byte format %s", format); |
108 | 19 | return AVERROR_PATCHWELCOME; |
109 | 19 | } |
110 | 391k | } else if (!memcmp(buffer, "sample_coding", 13)) { |
111 | 372 | sscanf(buffer, "%*s %*s %31s", coding); |
112 | 391k | } else if (!memcmp(buffer, "sample_count", 12)) { |
113 | 1.06k | sscanf(buffer, "%*s %*s %"SCNd64, &st->duration); |
114 | 390k | } else if (!memcmp(buffer, "sample_n_bytes", 14)) { |
115 | 616 | sscanf(buffer, "%*s %*s %d", &bps); |
116 | 616 | if (bps > INT16_MAX/8U) |
117 | 29 | return AVERROR_INVALIDDATA; |
118 | 389k | } else if (!memcmp(buffer, "sample_rate", 11)) { |
119 | 1.25k | sscanf(buffer, "%*s %*s %d", &st->codecpar->sample_rate); |
120 | 388k | } else if (!memcmp(buffer, "sample_sig_bits", 15)) { |
121 | 1.16k | sscanf(buffer, "%*s %*s %d", &st->codecpar->bits_per_coded_sample); |
122 | 1.16k | if (st->codecpar->bits_per_coded_sample <= 0 || st->codecpar->bits_per_coded_sample > INT16_MAX) |
123 | 47 | return AVERROR_INVALIDDATA; |
124 | 387k | } else { |
125 | 387k | char key[32], value[32]; |
126 | 387k | if (sscanf(buffer, "%31s %*s %31s", key, value) == 2) { |
127 | 12.7k | av_dict_set(&s->metadata, key, value, AV_DICT_APPEND); |
128 | 374k | } else { |
129 | 374k | av_log(s, AV_LOG_ERROR, "Failed to parse '%s' as metadata\n", buffer); |
130 | 374k | } |
131 | 387k | } |
132 | 396k | } |
133 | | |
134 | 489 | return AVERROR_EOF; |
135 | 2.64k | } |
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 | | }; |