00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00032 #include "libavutil/intreadwrite.h"
00033 #include "libavutil/intfloat_readwrite.h"
00034 #include "libavutil/dict.h"
00035 #include "avformat.h"
00036 #include "avio_internal.h"
00037 #include "sox.h"
00038
00039 typedef struct {
00040 int64_t header_size;
00041 } SoXContext;
00042
00043 static int sox_write_header(AVFormatContext *s)
00044 {
00045 SoXContext *sox = s->priv_data;
00046 AVIOContext *pb = s->pb;
00047 AVCodecContext *enc = s->streams[0]->codec;
00048 AVDictionaryEntry *comment;
00049 size_t comment_len = 0, comment_size;
00050
00051 comment = av_dict_get(s->metadata, "comment", NULL, 0);
00052 if (comment)
00053 comment_len = strlen(comment->value);
00054 comment_size = (comment_len + 7) & ~7;
00055
00056 sox->header_size = SOX_FIXED_HDR + comment_size;
00057
00058 if (enc->codec_id == CODEC_ID_PCM_S32LE) {
00059 ffio_wfourcc(pb, ".SoX");
00060 avio_wl32(pb, sox->header_size);
00061 avio_wl64(pb, 0);
00062 avio_wl64(pb, av_dbl2int(enc->sample_rate));
00063 avio_wl32(pb, enc->channels);
00064 avio_wl32(pb, comment_size);
00065 } else if (enc->codec_id == CODEC_ID_PCM_S32BE) {
00066 ffio_wfourcc(pb, "XoS.");
00067 avio_wb32(pb, sox->header_size);
00068 avio_wb64(pb, 0);
00069 avio_wb64(pb, av_dbl2int(enc->sample_rate));
00070 avio_wb32(pb, enc->channels);
00071 avio_wb32(pb, comment_size);
00072 } else {
00073 av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
00074 return -1;
00075 }
00076
00077 if (comment_len)
00078 avio_write(pb, comment->value, comment_len);
00079
00080 for ( ; comment_size > comment_len; comment_len++)
00081 avio_w8(pb, 0);
00082
00083 avio_flush(pb);
00084
00085 return 0;
00086 }
00087
00088 static int sox_write_packet(AVFormatContext *s, AVPacket *pkt)
00089 {
00090 AVIOContext *pb = s->pb;
00091 avio_write(pb, pkt->data, pkt->size);
00092 return 0;
00093 }
00094
00095 static int sox_write_trailer(AVFormatContext *s)
00096 {
00097 SoXContext *sox = s->priv_data;
00098 AVIOContext *pb = s->pb;
00099 AVCodecContext *enc = s->streams[0]->codec;
00100
00101 if (s->pb->seekable) {
00102
00103 int64_t file_size = avio_tell(pb);
00104 int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
00105 avio_seek(pb, 8, SEEK_SET);
00106 if (enc->codec_id == CODEC_ID_PCM_S32LE) {
00107 avio_wl64(pb, num_samples);
00108 } else
00109 avio_wb64(pb, num_samples);
00110 avio_seek(pb, file_size, SEEK_SET);
00111
00112 avio_flush(pb);
00113 }
00114
00115 return 0;
00116 }
00117
00118 AVOutputFormat ff_sox_muxer = {
00119 .name = "sox",
00120 .long_name = NULL_IF_CONFIG_SMALL("SoX native format"),
00121 .extensions = "sox",
00122 .priv_data_size = sizeof(SoXContext),
00123 .audio_codec = CODEC_ID_PCM_S32LE,
00124 .video_codec = CODEC_ID_NONE,
00125 .write_header = sox_write_header,
00126 .write_packet = sox_write_packet,
00127 .write_trailer = sox_write_trailer,
00128 };