Libav
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 Libav.
9  *
10  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
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 "sox.h"
38 
39 typedef struct SoXContext {
40  int64_t header_size;
41 } SoXContext;
42 
44 {
45  SoXContext *sox = s->priv_data;
46  AVIOContext *pb = s->pb;
47  AVCodecParameters *par = s->streams[0]->codecpar;
48  AVDictionaryEntry *comment;
49  size_t comment_len = 0, comment_size;
50 
51  comment = av_dict_get(s->metadata, "comment", NULL, 0);
52  if (comment)
53  comment_len = strlen(comment->value);
54  comment_size = (comment_len + 7) & ~7;
55 
56  sox->header_size = SOX_FIXED_HDR + comment_size;
57 
58  if (par->codec_id == AV_CODEC_ID_PCM_S32LE) {
59  ffio_wfourcc(pb, ".SoX");
60  avio_wl32(pb, sox->header_size);
61  avio_wl64(pb, 0); /* number of samples */
63  avio_wl32(pb, par->channels);
64  avio_wl32(pb, comment_size);
65  } else if (par->codec_id == AV_CODEC_ID_PCM_S32BE) {
66  ffio_wfourcc(pb, "XoS.");
67  avio_wb32(pb, sox->header_size);
68  avio_wb64(pb, 0); /* number of samples */
70  avio_wb32(pb, par->channels);
71  avio_wb32(pb, comment_size);
72  } else {
73  av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
74  return -1;
75  }
76 
77  if (comment_len)
78  avio_write(pb, comment->value, comment_len);
79 
80  for ( ; comment_size > comment_len; comment_len++)
81  avio_w8(pb, 0);
82 
83  avio_flush(pb);
84 
85  return 0;
86 }
87 
89 {
90  AVIOContext *pb = s->pb;
91  avio_write(pb, pkt->data, pkt->size);
92  return 0;
93 }
94 
96 {
97  SoXContext *sox = s->priv_data;
98  AVIOContext *pb = s->pb;
99  AVCodecParameters *par = s->streams[0]->codecpar;
100 
101  if (s->pb->seekable) {
102  /* update number of samples */
103  int64_t file_size = avio_tell(pb);
104  int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
105  avio_seek(pb, 8, SEEK_SET);
106  if (par->codec_id == AV_CODEC_ID_PCM_S32LE) {
107  avio_wl64(pb, num_samples);
108  } else
109  avio_wb64(pb, num_samples);
110  avio_seek(pb, file_size, SEEK_SET);
111 
112  avio_flush(pb);
113  }
114 
115  return 0;
116 }
117 
119  .name = "sox",
120  .long_name = NULL_IF_CONFIG_SMALL("SoX native"),
121  .extensions = "sox",
122  .priv_data_size = sizeof(SoXContext),
123  .audio_codec = AV_CODEC_ID_PCM_S32LE,
124  .video_codec = AV_CODEC_ID_NONE,
129 };
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:392
Bytestream IO Context.
Definition: avio.h:104
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
Format I/O context.
Definition: avformat.h:940
Public dictionary API.
int64_t header_size
Definition: soxenc.c:40
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:316
static int sox_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: soxenc.c:88
AVOutputFormat ff_sox_muxer
Definition: soxenc.c:118
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:38
uint8_t * data
Definition: avcodec.h:1346
static int flags
Definition: log.c:50
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:221
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:66
static int write_trailer(AVFormatContext *s)
Definition: assenc.c:64
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:386
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:236
const char * name
Definition: avformat.h:450
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:420
NULL
Definition: eval.c:55
AVIOContext * pb
I/O context.
Definition: avformat.h:982
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:200
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
Definition: avconv.c:278
int sample_rate
Audio only.
Definition: avcodec.h:3564
Main libavformat public API header.
static int sox_write_header(AVFormatContext *s)
Definition: soxenc.c:43
char * value
Definition: dict.h:74
void * priv_data
Format private data.
Definition: avformat.h:968
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:373
int channels
Audio only.
Definition: avcodec.h:3560
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:324
AVCodecParameters * codecpar
Definition: avformat.h:831
static int sox_write_trailer(AVFormatContext *s)
Definition: soxenc.c:95
#define SOX_FIXED_HDR
Size of fixed header without magic.
Definition: sox.h:25
This structure stores compressed data.
Definition: avcodec.h:1323