Statistics
| Branch: | Revision:

ffmpeg / libavformat / soxenc.c @ f59d8ff8

History | View | Annotate | Download (3.58 KB)

1
/*
2
 * SoX native format muxer
3
 * Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu>
4
 *
5
 * Based on libSoX sox-fmt.c
6
 * Copyright (c) 2008 robs@users.sourceforge.net
7
 *
8
 * This file is part of FFmpeg.
9
 *
10
 * FFmpeg is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU Lesser General Public
12
 * License as published by the Free Software Foundation; either
13
 * version 2.1 of the License, or (at your option) any later version.
14
 *
15
 * FFmpeg is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * Lesser General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Lesser General Public
21
 * License along with FFmpeg; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
 */
24

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

    
32
#include "libavutil/intreadwrite.h"
33
#include "avformat.h"
34
#include "avio_internal.h"
35
#include "sox.h"
36

    
37
typedef struct {
38
    int64_t header_size;
39
} SoXContext;
40

    
41
static int sox_write_header(AVFormatContext *s)
42
{
43
    SoXContext *sox = s->priv_data;
44
    AVIOContext *pb = s->pb;
45
    AVCodecContext *enc = s->streams[0]->codec;
46
    AVMetadataTag *comment;
47
    size_t comment_len = 0, comment_size;
48

    
49
    comment = av_metadata_get(s->metadata, "comment", NULL, 0);
50
    if (comment)
51
        comment_len = strlen(comment->value);
52
    comment_size = (comment_len + 7) & ~7;
53

    
54
    sox->header_size = SOX_FIXED_HDR + comment_size;
55

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

    
75
    if (comment_len)
76
        avio_write(pb, comment->value, comment_len);
77

    
78
    for ( ; comment_size > comment_len; comment_len++)
79
        avio_w8(pb, 0);
80

    
81
    put_flush_packet(pb);
82

    
83
    return 0;
84
}
85

    
86
static int sox_write_packet(AVFormatContext *s, AVPacket *pkt)
87
{
88
    AVIOContext *pb = s->pb;
89
    avio_write(pb, pkt->data, pkt->size);
90
    return 0;
91
}
92

    
93
static int sox_write_trailer(AVFormatContext *s)
94
{
95
    SoXContext *sox = s->priv_data;
96
    AVIOContext *pb = s->pb;
97
    AVCodecContext *enc = s->streams[0]->codec;
98

    
99
    if (!url_is_streamed(s->pb)) {
100
        /* update number of samples */
101
        int64_t file_size = url_ftell(pb);
102
        int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
103
        avio_seek(pb, 8, SEEK_SET);
104
        if (enc->codec_id == CODEC_ID_PCM_S32LE) {
105
            avio_wl64(pb, num_samples);
106
        } else
107
            avio_wb64(pb, num_samples);
108
        avio_seek(pb, file_size, SEEK_SET);
109

    
110
        put_flush_packet(pb);
111
    }
112

    
113
    return 0;
114
}
115

    
116
AVOutputFormat ff_sox_muxer = {
117
    "sox",
118
    NULL_IF_CONFIG_SMALL("SoX native format"),
119
    NULL,
120
    "sox",
121
    sizeof(SoXContext),
122
    CODEC_ID_PCM_S32LE,
123
    CODEC_ID_NONE,
124
    sox_write_header,
125
    sox_write_packet,
126
    sox_write_trailer,
127
};