FFmpeg
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 "mux.h"
38 #include "rawenc.h"
39 #include "sox.h"
40 
41 typedef struct SoXContext {
43 } SoXContext;
44 
46 {
47  SoXContext *sox = s->priv_data;
48  AVIOContext *pb = s->pb;
49  AVCodecParameters *par = s->streams[0]->codecpar;
51  size_t comment_len = 0, comment_size;
52 
53  comment = av_dict_get(s->metadata, "comment", NULL, 0);
54  if (comment)
55  comment_len = strlen(comment->value);
56  comment_size = FFALIGN(comment_len, 8);
57 
58  sox->header_size = SOX_FIXED_HDR + comment_size;
59 
60  if (par->codec_id == AV_CODEC_ID_PCM_S32LE) {
61  ffio_wfourcc(pb, ".SoX");
62  avio_wl32(pb, sox->header_size);
63  avio_wl64(pb, 0); /* number of samples */
66  avio_wl32(pb, comment_size);
67  } else if (par->codec_id == AV_CODEC_ID_PCM_S32BE) {
68  ffio_wfourcc(pb, "XoS.");
69  avio_wb32(pb, sox->header_size);
70  avio_wb64(pb, 0); /* number of samples */
73  avio_wb32(pb, comment_size);
74  } else {
75  av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
76  return AVERROR(EINVAL);
77  }
78 
79  if (comment_len)
80  avio_write(pb, comment->value, comment_len);
81 
82  ffio_fill(pb, 0, comment_size - comment_len);
83 
84  return 0;
85 }
86 
88 {
89  SoXContext *sox = s->priv_data;
90  AVIOContext *pb = s->pb;
91  AVCodecParameters *par = s->streams[0]->codecpar;
92 
93  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
94  /* update number of samples */
95  int64_t file_size = avio_tell(pb);
96  int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
97  avio_seek(pb, 8, SEEK_SET);
98  if (par->codec_id == AV_CODEC_ID_PCM_S32LE) {
99  avio_wl64(pb, num_samples);
100  } else
101  avio_wb64(pb, num_samples);
102  avio_seek(pb, file_size, SEEK_SET);
103  }
104 
105  return 0;
106 }
107 
109  .p.name = "sox",
110  .p.long_name = NULL_IF_CONFIG_SMALL("SoX (Sound eXchange) native"),
111  .p.extensions = "sox",
112  .priv_data_size = sizeof(SoXContext),
113  .p.audio_codec = AV_CODEC_ID_PCM_S32LE,
114  .p.video_codec = AV_CODEC_ID_NONE,
115  .p.subtitle_codec = AV_CODEC_ID_NONE,
116  .write_header = sox_write_header,
117  .write_packet = ff_raw_write_packet,
118  .write_trailer = sox_write_trailer,
119  .p.flags = AVFMT_NOTIMESTAMPS,
120  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH,
121 };
AVOutputFormat::name
const char * name
Definition: avformat.h:510
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ffio_wfourcc
static av_always_inline void ffio_wfourcc(AVIOContext *pb, const uint8_t *s)
Definition: avio_internal.h:124
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:47
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
int64_t
long long int64_t
Definition: coverity.c:34
sox.h
avio_wl64
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:424
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
intfloat.h
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
sox_write_trailer
static int sox_write_trailer(AVFormatContext *s)
Definition: soxenc.c:87
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_dict_get
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:62
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_raw_write_packet
int ff_raw_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: rawenc.c:31
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
ff_sox_muxer
const FFOutputFormat ff_sox_muxer
Definition: soxenc.c:108
NULL
#define NULL
Definition: coverity.c:32
FFOutputFormat
Definition: mux.h:61
ffio_fill
void ffio_fill(AVIOContext *s, int b, int64_t count)
Definition: aviobuf.c:186
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:200
avio_wb32
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:364
avio_wl32
void avio_wl32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:356
rawenc.h
av_double2int
static av_always_inline uint64_t av_double2int(double f)
Reinterpret a double as a 64-bit integer.
Definition: intfloat.h:70
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
avio_internal.h
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition: mux.h:50
AV_CODEC_ID_PCM_S32BE
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:337
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
comment
static int FUNC() comment(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawComment *current)
Definition: cbs_jpeg_syntax_template.c:174
SoXContext::header_size
int64_t header_size
Definition: soxenc.c:42
avformat.h
dict.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
avio_wb64
void avio_wb64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:430
AV_CODEC_ID_PCM_S32LE
@ AV_CODEC_ID_PCM_S32LE
Definition: codec_id.h:336
AVDictionaryEntry
Definition: dict.h:89
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
SOX_FIXED_HDR
#define SOX_FIXED_HDR
Size of fixed header without magic.
Definition: sox.h:25
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
SoXContext
Definition: soxenc.c:41
sox_write_header
static int sox_write_header(AVFormatContext *s)
Definition: soxenc.c:45
mux.h