1
--- src/ffmpeg.c 2009/12/02 11:06:28 1.28
2
+++ src/ffmpeg.c 2011/02/11 19:20:39 1.29
4
/* libSoX ffmpeg formats.
6
- * Copyright 2007 Reuben Thomas <rrt@sc3d.org>
7
+ * Copyright 2007, 2011 Reuben Thomas <rrt@sc3d.org>
9
* Based on ffplay.c and output_example.c Copyright 2003 Fabrice Bellard
10
* Note: ffplay.c is distributed under the LGPL 2.1 or later;
12
AVFormatContext *ctxt;
13
int audio_input_frame_size;
15
- uint8_t *audio_pkt_data;
19
/* open a given stream. Return 0 if OK */
23
/* NOTE: the audio packet can contain several frames */
24
- while (ffmpeg->audio_pkt_size > 0) {
25
+ while (ffmpeg->audio_pkt.size > 0) {
27
- len1 = avcodec_decode_audio2(ffmpeg->audio_st->codec,
28
- (int16_t *)audio_buf, &data_size,
29
- ffmpeg->audio_pkt_data, ffmpeg->audio_pkt_size);
30
+ len1 = avcodec_decode_audio3(ffmpeg->audio_st->codec,
31
+ (int16_t *)audio_buf, &data_size,
33
if (len1 < 0) /* if error, we skip the rest of the packet */
36
- ffmpeg->audio_pkt_data += len1;
37
- ffmpeg->audio_pkt_size -= len1;
38
+ ffmpeg->audio_pkt.data += len1;
39
+ ffmpeg->audio_pkt.size -= len1;
46
- ffmpeg->audio_pkt_data = pkt->data;
47
- ffmpeg->audio_pkt_size = pkt->size;
51
+/* On some platforms, libavcodec wants the output buffer aligned to 16
52
+ * bytes (because it uses SSE/Altivec internally). */
53
+#define ALIGN16(p) ((uint8_t *)(p) + (16 - (size_t)(p) % 16))
55
static int startread(sox_format_t * ft)
57
priv_t * ffmpeg = (priv_t *)ft->priv;
62
- ffmpeg->audio_buf = lsx_calloc(1, (size_t)AVCODEC_MAX_AUDIO_FRAME_SIZE);
63
+ ffmpeg->audio_buf = ALIGN16 (lsx_calloc(1, (size_t)AVCODEC_MAX_AUDIO_FRAME_SIZE + 16));
65
/* Signal audio stream not found */
66
ffmpeg->audio_index = -1;
68
ft->encoding.encoding = SOX_ENCODING_SIGN2;
69
ft->signal.channels = ffmpeg->audio_st->codec->channels;
70
ft->signal.length = 0; /* Currently we can't seek; no idea how to get this
71
- info from ffmpeg anyway (in time, yes, but not in
72
- samples); but ffmpeg *can* seek */
73
+ info from ffmpeg anyway (in time, yes, but not in
74
+ samples); but ffmpeg *can* seek */
79
if (ffmpeg->audio_buf_index * 2 >= ffmpeg->audio_buf_size) {
80
if ((ret = av_read_frame(ffmpeg->ctxt, pkt)) < 0 &&
81
(ret == AVERROR_EOF || url_ferror(ffmpeg->ctxt->pb)))
84
ffmpeg->audio_buf_size = audio_decode_frame(ffmpeg, ffmpeg->audio_buf, AVCODEC_MAX_AUDIO_FRAME_SIZE);
85
ffmpeg->audio_buf_index = 0;
89
/* auto detect the output format from the name. default is
91
- ffmpeg->fmt = guess_format(NULL, ft->filename, NULL);
92
+ ffmpeg->fmt = av_guess_format(NULL, ft->filename, NULL);
94
lsx_warn("ffmpeg could not deduce output format from file extension; using MPEG");
95
- ffmpeg->fmt = guess_format("mpeg", NULL, NULL);
96
+ ffmpeg->fmt = av_guess_format("mpeg", NULL, NULL);
98
lsx_fail("ffmpeg could not find suitable output format");
103
/* allocate the output media context */
104
- ffmpeg->ctxt = av_alloc_format_context();
105
+ ffmpeg->ctxt = avformat_alloc_context();
107
fprintf(stderr, "ffmpeg out of memory error");
109
@@ -408,12 +407,12 @@
110
if (ffmpeg->samples_index < ffmpeg->audio_input_frame_size) {
112
for (; nread < len && ffmpeg->samples_index < ffmpeg->audio_input_frame_size; nread++)
113
- ffmpeg->samples[ffmpeg->samples_index++] = SOX_SAMPLE_TO_SIGNED_16BIT(buf[nread], ft->clips);
114
+ ffmpeg->samples[ffmpeg->samples_index++] = SOX_SAMPLE_TO_SIGNED_16BIT(buf[nread], ft->clips);
117
/* If output frame full or no more data to read, write it out */
118
if (ffmpeg->samples_index == ffmpeg->audio_input_frame_size ||
119
- (len == 0 && ffmpeg->samples_index > 0)) {
120
+ (len == 0 && ffmpeg->samples_index > 0)) {
121
AVCodecContext *c = ffmpeg->audio_st->codec;
124
@@ -426,10 +425,10 @@
126
/* write the compressed frame to the media file */
127
if (av_write_frame(ffmpeg->ctxt, &pkt) != 0)
128
- lsx_fail("ffmpeg had error while writing audio frame");
129
+ lsx_fail("ffmpeg had error while writing audio frame");
131
/* Increment nwritten whether write succeeded or not; we have to
132
- get rid of the input! */
133
+ get rid of the input! */
134
nwritten += ffmpeg->samples_index;
135
ffmpeg->samples_index = 0;
138
"ffmpeg", /* special type to force use of ffmpeg */