soxenc.c
Go to the documentation of this file.
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  * @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 #include "sox.h"
39 
40 typedef struct {
41  int64_t header_size;
42 } SoXContext;
43 
45 {
46  SoXContext *sox = s->priv_data;
47  AVIOContext *pb = s->pb;
48  AVCodecContext *enc = s->streams[0]->codec;
49  AVDictionaryEntry *comment;
50  size_t comment_len = 0, comment_size;
51 
52  comment = av_dict_get(s->metadata, "comment", NULL, 0);
53  if (comment)
54  comment_len = strlen(comment->value);
55  comment_size = FFALIGN(comment_len, 8);
56 
57  sox->header_size = SOX_FIXED_HDR + comment_size;
58 
59  if (enc->codec_id == AV_CODEC_ID_PCM_S32LE) {
60  ffio_wfourcc(pb, ".SoX");
61  avio_wl32(pb, sox->header_size);
62  avio_wl64(pb, 0); /* number of samples */
64  avio_wl32(pb, enc->channels);
65  avio_wl32(pb, comment_size);
66  } else if (enc->codec_id == AV_CODEC_ID_PCM_S32BE) {
67  ffio_wfourcc(pb, "XoS.");
68  avio_wb32(pb, sox->header_size);
69  avio_wb64(pb, 0); /* number of samples */
71  avio_wb32(pb, enc->channels);
72  avio_wb32(pb, comment_size);
73  } else {
74  av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
75  return -1;
76  }
77 
78  if (comment_len)
79  avio_write(pb, comment->value, comment_len);
80 
81  ffio_fill(pb, 0, comment_size - comment_len);
82 
83  avio_flush(pb);
84 
85  return 0;
86 }
87 
89 {
90  SoXContext *sox = s->priv_data;
91  AVIOContext *pb = s->pb;
92  AVCodecContext *enc = s->streams[0]->codec;
93 
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  return 0;
109 }
110 
112  .name = "sox",
113  .long_name = NULL_IF_CONFIG_SMALL("SoX native"),
114  .extensions = "sox",
115  .priv_data_size = sizeof(SoXContext),
116  .audio_codec = AV_CODEC_ID_PCM_S32LE,
117  .video_codec = AV_CODEC_ID_NONE,
121 };
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:361
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
static int write_packet(AVFormatContext *s, AVPacket *pkt)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:39
#define FFALIGN(x, a)
Definition: common.h:63
Format I/O context.
Definition: avformat.h:944
Public dictionary API.
int64_t header_size
Definition: soxenc.c:41
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:291
AVOutputFormat ff_sox_muxer
Definition: soxenc.c:111
AVStream ** streams
Definition: avformat.h:992
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:50
static int write_trailer(AVFormatContext *s)
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:355
AVDictionary * metadata
Definition: avformat.h:1092
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
void ffio_fill(AVIOContext *s, int b, int count)
Definition: aviobuf.c:159
const char * name
Definition: avformat.h:378
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
NULL
Definition: eval.c:55
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
main external API structure.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
Main libavformat public API header.
static int sox_write_header(AVFormatContext *s)
Definition: soxenc.c:44
char * value
Definition: dict.h:82
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:470
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:299
static int sox_write_trailer(AVFormatContext *s)
Definition: soxenc.c:88
#define SOX_FIXED_HDR
Size of fixed header without magic.
Definition: sox.h:25