annotate ffmpeg/libavformat/soxenc.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * SoX native format muxer
yading@11 3 * Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu>
yading@11 4 *
yading@11 5 * Based on libSoX sox-fmt.c
yading@11 6 * Copyright (c) 2008 robs@users.sourceforge.net
yading@11 7 *
yading@11 8 * This file is part of FFmpeg.
yading@11 9 *
yading@11 10 * FFmpeg is free software; you can redistribute it and/or
yading@11 11 * modify it under the terms of the GNU Lesser General Public
yading@11 12 * License as published by the Free Software Foundation; either
yading@11 13 * version 2.1 of the License, or (at your option) any later version.
yading@11 14 *
yading@11 15 * FFmpeg is distributed in the hope that it will be useful,
yading@11 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 18 * Lesser General Public License for more details.
yading@11 19 *
yading@11 20 * You should have received a copy of the GNU Lesser General Public
yading@11 21 * License along with FFmpeg; if not, write to the Free Software
yading@11 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 23 */
yading@11 24
yading@11 25 /**
yading@11 26 * @file
yading@11 27 * SoX native format muxer
yading@11 28 * @author Daniel Verkamp
yading@11 29 * @see http://wiki.multimedia.cx/index.php?title=SoX_native_intermediate_format
yading@11 30 */
yading@11 31
yading@11 32 #include "libavutil/intreadwrite.h"
yading@11 33 #include "libavutil/intfloat.h"
yading@11 34 #include "libavutil/dict.h"
yading@11 35 #include "avformat.h"
yading@11 36 #include "avio_internal.h"
yading@11 37 #include "rawenc.h"
yading@11 38 #include "sox.h"
yading@11 39
yading@11 40 typedef struct {
yading@11 41 int64_t header_size;
yading@11 42 } SoXContext;
yading@11 43
yading@11 44 static int sox_write_header(AVFormatContext *s)
yading@11 45 {
yading@11 46 SoXContext *sox = s->priv_data;
yading@11 47 AVIOContext *pb = s->pb;
yading@11 48 AVCodecContext *enc = s->streams[0]->codec;
yading@11 49 AVDictionaryEntry *comment;
yading@11 50 size_t comment_len = 0, comment_size;
yading@11 51
yading@11 52 comment = av_dict_get(s->metadata, "comment", NULL, 0);
yading@11 53 if (comment)
yading@11 54 comment_len = strlen(comment->value);
yading@11 55 comment_size = FFALIGN(comment_len, 8);
yading@11 56
yading@11 57 sox->header_size = SOX_FIXED_HDR + comment_size;
yading@11 58
yading@11 59 if (enc->codec_id == AV_CODEC_ID_PCM_S32LE) {
yading@11 60 ffio_wfourcc(pb, ".SoX");
yading@11 61 avio_wl32(pb, sox->header_size);
yading@11 62 avio_wl64(pb, 0); /* number of samples */
yading@11 63 avio_wl64(pb, av_double2int(enc->sample_rate));
yading@11 64 avio_wl32(pb, enc->channels);
yading@11 65 avio_wl32(pb, comment_size);
yading@11 66 } else if (enc->codec_id == AV_CODEC_ID_PCM_S32BE) {
yading@11 67 ffio_wfourcc(pb, "XoS.");
yading@11 68 avio_wb32(pb, sox->header_size);
yading@11 69 avio_wb64(pb, 0); /* number of samples */
yading@11 70 avio_wb64(pb, av_double2int(enc->sample_rate));
yading@11 71 avio_wb32(pb, enc->channels);
yading@11 72 avio_wb32(pb, comment_size);
yading@11 73 } else {
yading@11 74 av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
yading@11 75 return -1;
yading@11 76 }
yading@11 77
yading@11 78 if (comment_len)
yading@11 79 avio_write(pb, comment->value, comment_len);
yading@11 80
yading@11 81 ffio_fill(pb, 0, comment_size - comment_len);
yading@11 82
yading@11 83 avio_flush(pb);
yading@11 84
yading@11 85 return 0;
yading@11 86 }
yading@11 87
yading@11 88 static int sox_write_trailer(AVFormatContext *s)
yading@11 89 {
yading@11 90 SoXContext *sox = s->priv_data;
yading@11 91 AVIOContext *pb = s->pb;
yading@11 92 AVCodecContext *enc = s->streams[0]->codec;
yading@11 93
yading@11 94 if (s->pb->seekable) {
yading@11 95 /* update number of samples */
yading@11 96 int64_t file_size = avio_tell(pb);
yading@11 97 int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
yading@11 98 avio_seek(pb, 8, SEEK_SET);
yading@11 99 if (enc->codec_id == AV_CODEC_ID_PCM_S32LE) {
yading@11 100 avio_wl64(pb, num_samples);
yading@11 101 } else
yading@11 102 avio_wb64(pb, num_samples);
yading@11 103 avio_seek(pb, file_size, SEEK_SET);
yading@11 104
yading@11 105 avio_flush(pb);
yading@11 106 }
yading@11 107
yading@11 108 return 0;
yading@11 109 }
yading@11 110
yading@11 111 AVOutputFormat ff_sox_muxer = {
yading@11 112 .name = "sox",
yading@11 113 .long_name = NULL_IF_CONFIG_SMALL("SoX native"),
yading@11 114 .extensions = "sox",
yading@11 115 .priv_data_size = sizeof(SoXContext),
yading@11 116 .audio_codec = AV_CODEC_ID_PCM_S32LE,
yading@11 117 .video_codec = AV_CODEC_ID_NONE,
yading@11 118 .write_header = sox_write_header,
yading@11 119 .write_packet = ff_raw_write_packet,
yading@11 120 .write_trailer = sox_write_trailer,
yading@11 121 };