Line | Branch | Exec | Source |
---|---|---|---|
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 { | ||
42 | int64_t header_size; | ||
43 | } SoXContext; | ||
44 | |||
45 | 1 | static int sox_write_header(AVFormatContext *s) | |
46 | { | ||
47 | 1 | SoXContext *sox = s->priv_data; | |
48 | 1 | AVIOContext *pb = s->pb; | |
49 | 1 | AVCodecParameters *par = s->streams[0]->codecpar; | |
50 | AVDictionaryEntry *comment; | ||
51 | 1 | size_t comment_len = 0, comment_size; | |
52 | |||
53 | 1 | comment = av_dict_get(s->metadata, "comment", NULL, 0); | |
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (comment) |
55 | ✗ | comment_len = strlen(comment->value); | |
56 | 1 | comment_size = FFALIGN(comment_len, 8); | |
57 | |||
58 | 1 | sox->header_size = SOX_FIXED_HDR + comment_size; | |
59 | |||
60 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (par->codec_id == AV_CODEC_ID_PCM_S32LE) { |
61 | 1 | ffio_wfourcc(pb, ".SoX"); | |
62 | 1 | avio_wl32(pb, sox->header_size); | |
63 | 1 | avio_wl64(pb, 0); /* number of samples */ | |
64 | 1 | avio_wl64(pb, av_double2int(par->sample_rate)); | |
65 | 1 | avio_wl32(pb, par->ch_layout.nb_channels); | |
66 | 1 | 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 */ | |
71 | ✗ | avio_wb64(pb, av_double2int(par->sample_rate)); | |
72 | ✗ | avio_wb32(pb, par->ch_layout.nb_channels); | |
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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (comment_len) |
80 | ✗ | avio_write(pb, comment->value, comment_len); | |
81 | |||
82 | 1 | ffio_fill(pb, 0, comment_size - comment_len); | |
83 | |||
84 | 1 | return 0; | |
85 | } | ||
86 | |||
87 | 1 | static int sox_write_trailer(AVFormatContext *s) | |
88 | { | ||
89 | 1 | SoXContext *sox = s->priv_data; | |
90 | 1 | AVIOContext *pb = s->pb; | |
91 | 1 | AVCodecParameters *par = s->streams[0]->codecpar; | |
92 | |||
93 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) { |
94 | /* update number of samples */ | ||
95 | 1 | int64_t file_size = avio_tell(pb); | |
96 | 1 | int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL; | |
97 | 1 | avio_seek(pb, 8, SEEK_SET); | |
98 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (par->codec_id == AV_CODEC_ID_PCM_S32LE) { |
99 | 1 | avio_wl64(pb, num_samples); | |
100 | } else | ||
101 | ✗ | avio_wb64(pb, num_samples); | |
102 | 1 | avio_seek(pb, file_size, SEEK_SET); | |
103 | } | ||
104 | |||
105 | 1 | return 0; | |
106 | } | ||
107 | |||
108 | const FFOutputFormat ff_sox_muxer = { | ||
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 | }; | ||
122 |