libavformat/nistspheredec.c
4f5440ad
 /*
  * NIST Sphere demuxer
  * Copyright (c) 2012 Paul B Mahol
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include "libavutil/avstring.h"
 #include "libavutil/intreadwrite.h"
 #include "avformat.h"
 #include "internal.h"
 #include "pcm.h"
 
 static int nist_probe(AVProbeData *p)
 {
     if (AV_RL64(p->buf) == AV_RL64("NIST_1A\x0a"))
         return AVPROBE_SCORE_MAX;
     return 0;
 }
 
 static int nist_read_header(AVFormatContext *s)
 {
0c949060
     char buffer[256], coding[32] = "pcm", format[32] = "01";
4f5440ad
     int bps = 0, be = 0;
632fdec9
     int32_t header_size = -1;
4f5440ad
     AVStream *st;
 
     st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
 
6f69f7a8
     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
4f5440ad
 
     ff_get_line(s->pb, buffer, sizeof(buffer));
     ff_get_line(s->pb, buffer, sizeof(buffer));
     sscanf(buffer, "%"SCNd32, &header_size);
     if (header_size <= 0)
         return AVERROR_INVALIDDATA;
 
d34ec64a
     while (!avio_feof(s->pb)) {
4f5440ad
         ff_get_line(s->pb, buffer, sizeof(buffer));
 
         if (avio_tell(s->pb) >= header_size)
             return AVERROR_INVALIDDATA;
 
         if (!memcmp(buffer, "end_head", 8)) {
6f69f7a8
             if (!st->codecpar->bits_per_coded_sample)
                 st->codecpar->bits_per_coded_sample = bps << 3;
4f5440ad
 
             if (!av_strcasecmp(coding, "pcm")) {
6f69f7a8
                 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
                     st->codecpar->codec_id = ff_get_pcm_codec_id(st->codecpar->bits_per_coded_sample,
a4790e18
                                                               0, be, 0xFFFF);
4f5440ad
             } else if (!av_strcasecmp(coding, "alaw")) {
6f69f7a8
                 st->codecpar->codec_id = AV_CODEC_ID_PCM_ALAW;
4f5440ad
             } else if (!av_strcasecmp(coding, "ulaw") ||
                        !av_strcasecmp(coding, "mu-law")) {
6f69f7a8
                 st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW;
0c949060
             } else if (!av_strncasecmp(coding, "pcm,embedded-shorten", 20)) {
6f69f7a8
                 st->codecpar->codec_id = AV_CODEC_ID_SHORTEN;
                 if (ff_alloc_extradata(st->codecpar, 1))
                     st->codecpar->extradata[0] = 1;
4f5440ad
             } else {
a9b42487
                 avpriv_request_sample(s, "coding %s", coding);
4f5440ad
             }
 
6f69f7a8
             avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
4f5440ad
 
6f69f7a8
             st->codecpar->block_align = st->codecpar->bits_per_coded_sample * st->codecpar->channels / 8;
4f5440ad
 
             if (avio_tell(s->pb) > header_size)
                 return AVERROR_INVALIDDATA;
 
             avio_skip(s->pb, header_size - avio_tell(s->pb));
 
             return 0;
         } else if (!memcmp(buffer, "channel_count", 13)) {
54904525
             sscanf(buffer, "%*s %*s %u", &st->codecpar->channels);
4f5440ad
         } else if (!memcmp(buffer, "sample_byte_format", 18)) {
             sscanf(buffer, "%*s %*s %31s", format);
 
             if (!av_strcasecmp(format, "01")) {
                 be = 0;
             } else if (!av_strcasecmp(format, "10")) {
                 be = 1;
a4790e18
             } else if (!av_strcasecmp(format, "mu-law")) {
6f69f7a8
                 st->codecpar->codec_id = AV_CODEC_ID_PCM_MULAW;
4f5440ad
             } else if (av_strcasecmp(format, "1")) {
a9b42487
                 avpriv_request_sample(s, "sample byte format %s", format);
4f5440ad
                 return AVERROR_PATCHWELCOME;
             }
         } else if (!memcmp(buffer, "sample_coding", 13)) {
             sscanf(buffer, "%*s %*s %31s", coding);
         } else if (!memcmp(buffer, "sample_count", 12)) {
             sscanf(buffer, "%*s %*s %"SCNd64, &st->duration);
         } else if (!memcmp(buffer, "sample_n_bytes", 14)) {
54904525
             sscanf(buffer, "%*s %*s %d", &bps);
4f5440ad
         } else if (!memcmp(buffer, "sample_rate", 11)) {
54904525
             sscanf(buffer, "%*s %*s %d", &st->codecpar->sample_rate);
4f5440ad
         } else if (!memcmp(buffer, "sample_sig_bits", 15)) {
54904525
             sscanf(buffer, "%*s %*s %d", &st->codecpar->bits_per_coded_sample);
4f5440ad
         } else {
             char key[32], value[32];
259879d3
             if (sscanf(buffer, "%31s %*s %31s", key, value) == 2) {
8fe06e7a
                 av_dict_set(&s->metadata, key, value, AV_DICT_APPEND);
             } else {
                 av_log(s, AV_LOG_ERROR, "Failed to parse '%s' as metadata\n", buffer);
             }
4f5440ad
         }
     }
 
     return AVERROR_EOF;
 }
 
 AVInputFormat ff_nistsphere_demuxer = {
     .name           = "nistsphere",
     .long_name      = NULL_IF_CONFIG_SMALL("NIST SPeech HEader REsources"),
     .read_probe     = nist_probe,
     .read_header    = nist_read_header,
     .read_packet    = ff_pcm_read_packet,
     .read_seek      = ff_pcm_read_seek,
     .extensions     = "nist,sph",
     .flags          = AVFMT_GENERIC_INDEX,
 };