soxenc.c 3.65 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * SoX native format muxer
 * Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu>
 *
 * Based on libSoX sox-fmt.c
 * Copyright (c) 2008 robs@users.sourceforge.net
 *
 * 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
 */

/**
26
 * @file
27
 * SoX native format muxer
28
 * @author Daniel Verkamp
29
 * @see http://wiki.multimedia.cx/index.php?title=SoX_native_intermediate_format
30 31 32
 */

#include "libavutil/intreadwrite.h"
33
#include "libavutil/intfloat.h"
34
#include "libavutil/dict.h"
35
#include "avformat.h"
36
#include "avio_internal.h"
37
#include "rawenc.h"
38 39 40 41 42 43 44 45 46
#include "sox.h"

typedef struct {
    int64_t header_size;
} SoXContext;

static int sox_write_header(AVFormatContext *s)
{
    SoXContext *sox = s->priv_data;
47
    AVIOContext *pb = s->pb;
48
    AVCodecContext *enc = s->streams[0]->codec;
49
    AVDictionaryEntry *comment;
50 51
    size_t comment_len = 0, comment_size;

52
    comment = av_dict_get(s->metadata, "comment", NULL, 0);
53 54
    if (comment)
        comment_len = strlen(comment->value);
Paul B Mahol's avatar
Paul B Mahol committed
55
    comment_size = FFALIGN(comment_len, 8);
56 57 58

    sox->header_size = SOX_FIXED_HDR + comment_size;

59
    if (enc->codec_id == AV_CODEC_ID_PCM_S32LE) {
60
        ffio_wfourcc(pb, ".SoX");
61 62
        avio_wl32(pb, sox->header_size);
        avio_wl64(pb, 0); /* number of samples */
63
        avio_wl64(pb, av_double2int(enc->sample_rate));
64 65
        avio_wl32(pb, enc->channels);
        avio_wl32(pb, comment_size);
66
    } else if (enc->codec_id == AV_CODEC_ID_PCM_S32BE) {
67
        ffio_wfourcc(pb, "XoS.");
68 69
        avio_wb32(pb, sox->header_size);
        avio_wb64(pb, 0); /* number of samples */
70
        avio_wb64(pb, av_double2int(enc->sample_rate));
71 72
        avio_wb32(pb, enc->channels);
        avio_wb32(pb, comment_size);
73 74 75 76 77 78
    } else {
        av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
        return -1;
    }

    if (comment_len)
79
        avio_write(pb, comment->value, comment_len);
80

Paul B Mahol's avatar
Paul B Mahol committed
81
    ffio_fill(pb, 0, comment_size - comment_len);
82

83
    avio_flush(pb);
84 85 86 87 88 89 90

    return 0;
}

static int sox_write_trailer(AVFormatContext *s)
{
    SoXContext *sox = s->priv_data;
91
    AVIOContext *pb = s->pb;
92 93
    AVCodecContext *enc = s->streams[0]->codec;

94
    if (s->pb->seekable) {
95
        /* update number of samples */
96
        int64_t file_size = avio_tell(pb);
97
        int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
98
        avio_seek(pb, 8, SEEK_SET);
99
        if (enc->codec_id == AV_CODEC_ID_PCM_S32LE) {
100
            avio_wl64(pb, num_samples);
101
        } else
102
            avio_wb64(pb, num_samples);
103
        avio_seek(pb, file_size, SEEK_SET);
104

105
        avio_flush(pb);
106 107 108 109 110
    }

    return 0;
}

111
AVOutputFormat ff_sox_muxer = {
112
    .name              = "sox",
113
    .long_name         = NULL_IF_CONFIG_SMALL("SoX native"),
114 115
    .extensions        = "sox",
    .priv_data_size    = sizeof(SoXContext),
116 117
    .audio_codec       = AV_CODEC_ID_PCM_S32LE,
    .video_codec       = AV_CODEC_ID_NONE,
118
    .write_header      = sox_write_header,
119
    .write_packet      = ff_raw_write_packet,
120
    .write_trailer     = sox_write_trailer,
121
};