• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/soxenc.c

Go to the documentation of this file.
00001 /*
00002  * SoX native format muxer
00003  * Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu>
00004  *
00005  * Based on libSoX sox-fmt.c
00006  * Copyright (c) 2008 robs@users.sourceforge.net
00007  *
00008  * This file is part of Libav.
00009  *
00010  * Libav is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * Libav is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with Libav; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00023  */
00024 
00032 #include "libavutil/intreadwrite.h"
00033 #include "libavutil/intfloat.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); /* number of samples */
00062         avio_wl64(pb, av_double2int(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); /* number of samples */
00069         avio_wb64(pb, av_double2int(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         /* update number of samples */
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 };
Generated on Thu Jan 24 2013 17:08:56 for Libav by doxygen 1.7.1