annotate soxenc.c @ 6449:3a28e6827095 libavformat

Solving memory leak and initialization problem with prev_pkt / pkt.
author bindhammer
date Tue, 31 Aug 2010 07:15:11 +0000
parents 178de7695c6c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
1 /*
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
2 * SoX native format muxer
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
3 * Copyright (c) 2009 Daniel Verkamp <daniel@drv.nu>
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
4 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
5 * Based on libSoX sox-fmt.c
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
6 * Copyright (c) 2008 robs@users.sourceforge.net
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
7 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
8 * This file is part of FFmpeg.
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
9 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
10 * FFmpeg is free software; you can redistribute it and/or
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
11 * modify it under the terms of the GNU Lesser General Public
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
12 * License as published by the Free Software Foundation; either
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
13 * version 2.1 of the License, or (at your option) any later version.
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
14 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
15 * FFmpeg is distributed in the hope that it will be useful,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
18 * Lesser General Public License for more details.
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
19 *
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
20 * You should have received a copy of the GNU Lesser General Public
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
21 * License along with FFmpeg; if not, write to the Free Software
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
23 */
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
24
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
25 /**
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
26 * SoX native format muxer
5969
178de7695c6c Remove explicit filename from Doxygen @file commands.
diego
parents: 4974
diff changeset
27 * @file
4974
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
28 * @author Daniel Verkamp
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
29 * @sa http://wiki.multimedia.cx/index.php?title=SoX_native_intermediate_format
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
30 */
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
31
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
32 #include "libavutil/intreadwrite.h"
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
33 #include "avformat.h"
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
34 #include "sox.h"
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
35
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
36 typedef struct {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
37 int64_t header_size;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
38 } SoXContext;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
39
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
40 static int sox_write_header(AVFormatContext *s)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
41 {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
42 SoXContext *sox = s->priv_data;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
43 ByteIOContext *pb = s->pb;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
44 AVCodecContext *enc = s->streams[0]->codec;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
45 AVMetadataTag *comment;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
46 size_t comment_len = 0, comment_size;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
47
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
48 comment = av_metadata_get(s->metadata, "comment", NULL, 0);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
49 if (comment)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
50 comment_len = strlen(comment->value);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
51 comment_size = (comment_len + 7) & ~7;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
52
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
53 sox->header_size = SOX_FIXED_HDR + comment_size;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
54
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
55 if (enc->codec_id == CODEC_ID_PCM_S32LE) {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
56 put_tag(pb, ".SoX");
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
57 put_le32(pb, sox->header_size);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
58 put_le64(pb, 0); /* number of samples */
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
59 put_le64(pb, av_dbl2int(enc->sample_rate));
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
60 put_le32(pb, enc->channels);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
61 put_le32(pb, comment_size);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
62 } else if (enc->codec_id == CODEC_ID_PCM_S32BE) {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
63 put_tag(pb, "XoS.");
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
64 put_be32(pb, sox->header_size);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
65 put_be64(pb, 0); /* number of samples */
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
66 put_be64(pb, av_dbl2int(enc->sample_rate));
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
67 put_be32(pb, enc->channels);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
68 put_be32(pb, comment_size);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
69 } else {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
70 av_log(s, AV_LOG_ERROR, "invalid codec; use pcm_s32le or pcm_s32be\n");
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
71 return -1;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
72 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
73
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
74 if (comment_len)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
75 put_buffer(pb, comment->value, comment_len);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
76
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
77 for ( ; comment_size > comment_len; comment_len++)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
78 put_byte(pb, 0);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
79
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
80 put_flush_packet(pb);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
81
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
82 return 0;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
83 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
84
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
85 static int sox_write_packet(AVFormatContext *s, AVPacket *pkt)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
86 {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
87 ByteIOContext *pb = s->pb;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
88 put_buffer(pb, pkt->data, pkt->size);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
89 return 0;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
90 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
91
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
92 static int sox_write_trailer(AVFormatContext *s)
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
93 {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
94 SoXContext *sox = s->priv_data;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
95 ByteIOContext *pb = s->pb;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
96 AVCodecContext *enc = s->streams[0]->codec;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
97
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
98 if (!url_is_streamed(s->pb)) {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
99 /* update number of samples */
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
100 int64_t file_size = url_ftell(pb);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
101 int64_t num_samples = (file_size - sox->header_size - 4LL) >> 2LL;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
102 url_fseek(pb, 8, SEEK_SET);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
103 if (enc->codec_id == CODEC_ID_PCM_S32LE) {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
104 put_le64(pb, num_samples);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
105 } else
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
106 put_be64(pb, num_samples);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
107 url_fseek(pb, file_size, SEEK_SET);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
108
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
109 put_flush_packet(pb);
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
110 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
111
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
112 return 0;
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
113 }
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
114
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
115 AVOutputFormat sox_muxer = {
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
116 "sox",
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
117 NULL_IF_CONFIG_SMALL("SoX native format"),
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
118 NULL,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
119 "sox",
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
120 sizeof(SoXContext),
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
121 CODEC_ID_PCM_S32LE,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
122 CODEC_ID_NONE,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
123 sox_write_header,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
124 sox_write_packet,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
125 sox_write_trailer,
636161c01391 Implement SoX native format muxer and demuxer.
stefano
parents:
diff changeset
126 };