Browse code

Merge commit 'fd41cb43702498948ff14ba8c284fd5c15fc729d'

* commit 'fd41cb43702498948ff14ba8c284fd5c15fc729d':
avconv: improve sample format negotiation for decoder request
Opus encoder using libopus
mpegts: Drop pointless casting of hex_dump_debug arguments
avformat: const correctness for av_hex_dump / av_hex_dump_log
wmadec: Adjust debug printf argument length modifier

Conflicts:
Changelog
ffmpeg.c
libavcodec/libopusdec.c
libavcodec/version.h

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2012/10/02 21:38:01
Showing 14 changed files
... ...
@@ -4,6 +4,7 @@ releases are sorted from youngest to oldest.
4 4
 version next:
5 5
 - stream disposition information printing in ffprobe
6 6
 - filter for loudness analysis following EBU R128
7
+- Opus encoder using libopus
7 8
 
8 9
 
9 10
 version 1.0:
... ...
@@ -1768,6 +1768,7 @@ libopencore_amrwb_decoder_deps="libopencore_amrwb"
1768 1768
 libopenjpeg_decoder_deps="libopenjpeg"
1769 1769
 libopenjpeg_encoder_deps="libopenjpeg"
1770 1770
 libopus_decoder_deps="libopus"
1771
+libopus_encoder_deps="libopus"
1771 1772
 libschroedinger_decoder_deps="libschroedinger"
1772 1773
 libschroedinger_encoder_deps="libschroedinger"
1773 1774
 libspeex_decoder_deps="libspeex"
... ...
@@ -813,7 +813,7 @@ following image formats are supported:
813 813
 @item Musepack SV7           @tab     @tab  X
814 814
 @item Musepack SV8           @tab     @tab  X
815 815
 @item Nellymoser Asao        @tab  X  @tab  X
816
-@item Opus                   @tab     @tab  E
816
+@item Opus                   @tab  E  @tab  E
817 817
     @tab supported through external library libopus
818 818
 @item PCM A-law              @tab  X  @tab  X
819 819
 @item PCM mu-law             @tab  X  @tab  X
... ...
@@ -657,7 +657,10 @@ OBJS-$(CONFIG_LIBOPENCORE_AMRNB_ENCODER)  += libopencore-amr.o \
657 657
 OBJS-$(CONFIG_LIBOPENCORE_AMRWB_DECODER)  += libopencore-amr.o
658 658
 OBJS-$(CONFIG_LIBOPENJPEG_DECODER)        += libopenjpegdec.o
659 659
 OBJS-$(CONFIG_LIBOPENJPEG_ENCODER)        += libopenjpegenc.o
660
-OBJS-$(CONFIG_LIBOPUS_DECODER)            += libopusdec.o vorbis_data.o
660
+OBJS-$(CONFIG_LIBOPUS_DECODER)            += libopusdec.o libopus.o     \
661
+                                             vorbis_data.o
662
+OBJS-$(CONFIG_LIBOPUS_ENCODER)            += libopusenc.o libopus.o     \
663
+                                             vorbis_data.o audio_frame_queue.o
661 664
 OBJS-$(CONFIG_LIBSCHROEDINGER_DECODER)    += libschroedingerdec.o \
662 665
                                              libschroedinger.o
663 666
 OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER)    += libschroedingerenc.o \
... ...
@@ -436,7 +436,7 @@ void avcodec_register_all(void)
436 436
     REGISTER_ENCDEC  (LIBOPENCORE_AMRNB, libopencore_amrnb);
437 437
     REGISTER_DECODER (LIBOPENCORE_AMRWB, libopencore_amrwb);
438 438
     REGISTER_ENCDEC  (LIBOPENJPEG, libopenjpeg);
439
-    REGISTER_DECODER (LIBOPUS, libopus);
439
+    REGISTER_ENCDEC  (LIBOPUS, libopus);
440 440
     REGISTER_ENCDEC  (LIBSCHROEDINGER, libschroedinger);
441 441
     REGISTER_ENCDEC  (LIBSPEEX, libspeex);
442 442
     REGISTER_DECODER (LIBSTAGEFRIGHT_H264, libstagefright_h264);
443 443
new file mode 100644
... ...
@@ -0,0 +1,48 @@
0
+/*
1
+ * libopus encoder/decoder common code
2
+ * Copyright (c) 2012 Nicolas George
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <opus_defines.h>
22
+
23
+#include "libavutil/common.h"
24
+#include "libavutil/error.h"
25
+#include "libopus.h"
26
+
27
+int ff_opus_error_to_averror(int err)
28
+{
29
+    switch (err) {
30
+    case OPUS_BAD_ARG:
31
+        return AVERROR(EINVAL);
32
+    case OPUS_BUFFER_TOO_SMALL:
33
+        return AVERROR_UNKNOWN;
34
+    case OPUS_INTERNAL_ERROR:
35
+        return AVERROR(EFAULT);
36
+    case OPUS_INVALID_PACKET:
37
+        return AVERROR_INVALIDDATA;
38
+    case OPUS_UNIMPLEMENTED:
39
+        return AVERROR(ENOSYS);
40
+    case OPUS_INVALID_STATE:
41
+        return AVERROR_UNKNOWN;
42
+    case OPUS_ALLOC_FAIL:
43
+        return AVERROR(ENOMEM);
44
+    default:
45
+        return AVERROR(EINVAL);
46
+    }
47
+}
0 48
new file mode 100644
... ...
@@ -0,0 +1,27 @@
0
+/*
1
+ * libopus encoder/decoder common code
2
+ * Copyright (c) 2012 Nicolas George
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#ifndef AVCODEC_LIBOPUS_H
22
+#define AVCODEC_LIBOPUS_H
23
+
24
+int ff_opus_error_to_averror(int err);
25
+
26
+#endif /* AVCODEC_LIBOPUS_H */
... ...
@@ -22,13 +22,13 @@
22 22
 #include <opus.h>
23 23
 #include <opus_multistream.h>
24 24
 
25
-#include "libavutil/common.h"
26 25
 #include "libavutil/avassert.h"
27 26
 #include "libavutil/intreadwrite.h"
28 27
 #include "avcodec.h"
29 28
 #include "internal.h"
30 29
 #include "vorbis.h"
31 30
 #include "mathops.h"
31
+#include "libopus.h"
32 32
 
33 33
 struct libopus_context {
34 34
     OpusMSDecoder *dec;
... ...
@@ -39,20 +39,6 @@ struct libopus_context {
39 39
 #endif
40 40
 };
41 41
 
42
-static int opus_error_to_averror(int err)
43
-{
44
-    switch (err) {
45
-        case OPUS_BAD_ARG:          return AVERROR(EINVAL);
46
-        case OPUS_BUFFER_TOO_SMALL: return AVERROR_BUFFER_TOO_SMALL;
47
-        case OPUS_INTERNAL_ERROR:   return AVERROR(EFAULT);
48
-        case OPUS_INVALID_PACKET:   return AVERROR_INVALIDDATA;
49
-        case OPUS_UNIMPLEMENTED:    return AVERROR(ENOSYS);
50
-        case OPUS_INVALID_STATE:    return AVERROR_EXTERNAL;
51
-        case OPUS_ALLOC_FAIL:       return AVERROR(ENOMEM);
52
-        default:                    return AVERROR(EINVAL);
53
-    }
54
-}
55
-
56 42
 #define OPUS_HEAD_SIZE 19
57 43
 
58 44
 static av_cold int libopus_decode_init(AVCodecContext *avc)
... ...
@@ -105,7 +91,7 @@ static av_cold int libopus_decode_init(AVCodecContext *avc)
105 105
     if (!opus->dec) {
106 106
         av_log(avc, AV_LOG_ERROR, "Unable to create decoder: %s\n",
107 107
                opus_strerror(ret));
108
-        return opus_error_to_averror(ret);
108
+        return ff_opus_error_to_averror(ret);
109 109
     }
110 110
 
111 111
 #ifdef OPUS_SET_GAIN
... ...
@@ -165,7 +151,7 @@ static int libopus_decode(AVCodecContext *avc, void *frame,
165 165
     if (nb_samples < 0) {
166 166
         av_log(avc, AV_LOG_ERROR, "Decoding error: %s\n",
167 167
                opus_strerror(nb_samples));
168
-        return opus_error_to_averror(nb_samples);
168
+        return ff_opus_error_to_averror(nb_samples);
169 169
     }
170 170
 
171 171
 #ifndef OPUS_SET_GAIN
172 172
new file mode 100644
... ...
@@ -0,0 +1,421 @@
0
+/*
1
+ * Opus encoder using libopus
2
+ * Copyright (c) 2012 Nathan Caldwell
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <opus.h>
22
+#include <opus_multistream.h>
23
+
24
+#include "libavutil/opt.h"
25
+#include "avcodec.h"
26
+#include "bytestream.h"
27
+#include "internal.h"
28
+#include "libopus.h"
29
+#include "vorbis.h"
30
+#include "audio_frame_queue.h"
31
+
32
+typedef struct LibopusEncOpts {
33
+    int vbr;
34
+    int application;
35
+    int packet_loss;
36
+    int complexity;
37
+    float frame_duration;
38
+    int packet_size;
39
+    int max_bandwidth;
40
+} LibopusEncOpts;
41
+
42
+typedef struct LibopusEncContext {
43
+    AVClass *class;
44
+    OpusMSEncoder *enc;
45
+    int stream_count;
46
+    uint8_t *samples;
47
+    LibopusEncOpts opts;
48
+    AudioFrameQueue afq;
49
+} LibopusEncContext;
50
+
51
+static const uint8_t opus_coupled_streams[8] = {
52
+    0, 1, 1, 2, 2, 2, 2, 3
53
+};
54
+
55
+/* Opus internal to Vorbis channel order mapping written in the header */
56
+static const uint8_t opus_vorbis_channel_map[8][8] = {
57
+    { 0 },
58
+    { 0, 1 },
59
+    { 0, 2, 1 },
60
+    { 0, 1, 2, 3 },
61
+    { 0, 4, 1, 2, 3 },
62
+    { 0, 4, 1, 2, 3, 5 },
63
+    { 0, 4, 1, 2, 3, 5, 6 },
64
+    { 0, 6, 1, 2, 3, 4, 5, 7 },
65
+};
66
+
67
+/* libav to libopus channel order mapping, passed to libopus */
68
+static const uint8_t libav_libopus_channel_map[8][8] = {
69
+    { 0 },
70
+    { 0, 1 },
71
+    { 0, 1, 2 },
72
+    { 0, 1, 2, 3 },
73
+    { 0, 1, 3, 4, 2 },
74
+    { 0, 1, 4, 5, 2, 3 },
75
+    { 0, 1, 5, 6, 2, 4, 3 },
76
+    { 0, 1, 6, 7, 4, 5, 2, 3 },
77
+};
78
+
79
+static void libopus_write_header(AVCodecContext *avctx, int stream_count,
80
+                                 int coupled_stream_count,
81
+                                 const uint8_t *channel_mapping)
82
+{
83
+    uint8_t *p   = avctx->extradata;
84
+    int channels = avctx->channels;
85
+
86
+    bytestream_put_buffer(&p, "OpusHead", 8);
87
+    bytestream_put_byte(&p, 1); /* Version */
88
+    bytestream_put_byte(&p, channels);
89
+    bytestream_put_le16(&p, avctx->delay); /* Lookahead samples at 48kHz */
90
+    bytestream_put_le32(&p, avctx->sample_rate); /* Original sample rate */
91
+    bytestream_put_le16(&p, 0); /* Gain of 0dB is recommended. */
92
+
93
+    /* Channel mapping */
94
+    if (channels > 2) {
95
+        bytestream_put_byte(&p, channels <= 8 ? 1 : 255);
96
+        bytestream_put_byte(&p, stream_count);
97
+        bytestream_put_byte(&p, coupled_stream_count);
98
+        bytestream_put_buffer(&p, channel_mapping, channels);
99
+    } else {
100
+        bytestream_put_byte(&p, 0);
101
+    }
102
+}
103
+
104
+static int libopus_configure_encoder(AVCodecContext *avctx, OpusMSEncoder *enc,
105
+                                     LibopusEncOpts *opts)
106
+{
107
+    int ret;
108
+
109
+    ret = opus_multistream_encoder_ctl(enc, OPUS_SET_BITRATE(avctx->bit_rate));
110
+    if (ret != OPUS_OK) {
111
+        av_log(avctx, AV_LOG_ERROR,
112
+               "Failed to set bitrate: %s\n", opus_strerror(ret));
113
+        return ret;
114
+    }
115
+
116
+    ret = opus_multistream_encoder_ctl(enc,
117
+                                       OPUS_SET_COMPLEXITY(opts->complexity));
118
+    if (ret != OPUS_OK)
119
+        av_log(avctx, AV_LOG_WARNING,
120
+               "Unable to set complexity: %s\n", opus_strerror(ret));
121
+
122
+    ret = opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(!!opts->vbr));
123
+    if (ret != OPUS_OK)
124
+        av_log(avctx, AV_LOG_WARNING,
125
+               "Unable to set VBR: %s\n", opus_strerror(ret));
126
+
127
+    ret = opus_multistream_encoder_ctl(enc,
128
+                                       OPUS_SET_VBR_CONSTRAINT(opts->vbr == 2));
129
+    if (ret != OPUS_OK)
130
+        av_log(avctx, AV_LOG_WARNING,
131
+               "Unable to set constrained VBR: %s\n", opus_strerror(ret));
132
+
133
+    ret = opus_multistream_encoder_ctl(enc,
134
+                                       OPUS_SET_PACKET_LOSS_PERC(opts->packet_loss));
135
+    if (ret != OPUS_OK)
136
+        av_log(avctx, AV_LOG_WARNING,
137
+               "Unable to set expected packet loss percentage: %s\n",
138
+               opus_strerror(ret));
139
+
140
+    if (avctx->cutoff) {
141
+        ret = opus_multistream_encoder_ctl(enc,
142
+                                           OPUS_SET_MAX_BANDWIDTH(opts->max_bandwidth));
143
+        if (ret != OPUS_OK)
144
+            av_log(avctx, AV_LOG_WARNING,
145
+                   "Unable to set maximum bandwidth: %s\n", opus_strerror(ret));
146
+    }
147
+
148
+    return OPUS_OK;
149
+}
150
+
151
+static int av_cold libopus_encode_init(AVCodecContext *avctx)
152
+{
153
+    LibopusEncContext *opus = avctx->priv_data;
154
+    const uint8_t *channel_mapping;
155
+    OpusMSEncoder *enc;
156
+    int ret = OPUS_OK;
157
+    int coupled_stream_count, header_size, frame_size;
158
+
159
+    coupled_stream_count = opus_coupled_streams[avctx->channels - 1];
160
+    opus->stream_count   = avctx->channels - coupled_stream_count;
161
+    channel_mapping      = libav_libopus_channel_map[avctx->channels - 1];
162
+
163
+    /* FIXME: Opus can handle up to 255 channels. However, the mapping for
164
+     * anything greater than 8 is undefined. */
165
+    if (avctx->channels > 8)
166
+        av_log(avctx, AV_LOG_WARNING,
167
+               "Channel layout undefined for %d channels.\n", avctx->channels);
168
+
169
+    if (!avctx->bit_rate) {
170
+        /* Sane default copied from opusenc */
171
+        avctx->bit_rate = 64000 * opus->stream_count +
172
+                          32000 * coupled_stream_count;
173
+        av_log(avctx, AV_LOG_WARNING,
174
+               "No bit rate set. Defaulting to %d bps.\n", avctx->bit_rate);
175
+    }
176
+
177
+    if (avctx->bit_rate < 500 || avctx->bit_rate > 256000 * avctx->channels) {
178
+        av_log(avctx, AV_LOG_ERROR, "The bit rate %d bps is unsupported. "
179
+               "Please choose a value between 500 and %d.\n", avctx->bit_rate,
180
+               256000 * avctx->channels);
181
+        return AVERROR(EINVAL);
182
+    }
183
+
184
+    frame_size = opus->opts.frame_duration * 48000 / 1000;
185
+    switch (frame_size) {
186
+    case 120:
187
+    case 240:
188
+        if (opus->opts.application != OPUS_APPLICATION_RESTRICTED_LOWDELAY)
189
+            av_log(avctx, AV_LOG_WARNING,
190
+                   "LPC mode cannot be used with a frame duration of less "
191
+                   "than 10ms. Enabling restricted low-delay mode.\n"
192
+                   "Use a longer frame duration if this is not what you want.\n");
193
+        /* Frame sizes less than 10 ms can only use MDCT mode, so switching to
194
+         * RESTRICTED_LOWDELAY avoids an unnecessary extra 2.5ms lookahead. */
195
+        opus->opts.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
196
+    case 480:
197
+    case 960:
198
+    case 1920:
199
+    case 2880:
200
+        opus->opts.packet_size =
201
+        avctx->frame_size      = frame_size * avctx->sample_rate / 48000;
202
+        break;
203
+    default:
204
+        av_log(avctx, AV_LOG_ERROR, "Invalid frame duration: %g.\n"
205
+               "Frame duration must be exactly one of: 2.5, 5, 10, 20, 40 or 60.\n",
206
+               opus->opts.frame_duration);
207
+        return AVERROR(EINVAL);
208
+    }
209
+
210
+    if (avctx->compression_level < 0 || avctx->compression_level > 10) {
211
+        av_log(avctx, AV_LOG_WARNING,
212
+               "Compression level must be in the range 0 to 10. "
213
+               "Defaulting to 10.\n");
214
+        opus->opts.complexity = 10;
215
+    } else {
216
+        opus->opts.complexity = avctx->compression_level;
217
+    }
218
+
219
+    if (avctx->cutoff) {
220
+        switch (avctx->cutoff) {
221
+        case  4000:
222
+            opus->opts.max_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
223
+            break;
224
+        case  6000:
225
+            opus->opts.max_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
226
+            break;
227
+        case  8000:
228
+            opus->opts.max_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
229
+            break;
230
+        case 12000:
231
+            opus->opts.max_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
232
+            break;
233
+        case 20000:
234
+            opus->opts.max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
235
+            break;
236
+        default:
237
+            av_log(avctx, AV_LOG_WARNING,
238
+                   "Invalid frequency cutoff: %d. Using default maximum bandwidth.\n"
239
+                   "Cutoff frequency must be exactly one of: 4000, 6000, 8000, 12000 or 20000.\n",
240
+                   avctx->cutoff);
241
+            avctx->cutoff = 0;
242
+        }
243
+    }
244
+
245
+    enc = opus_multistream_encoder_create(avctx->sample_rate, avctx->channels,
246
+                                          opus->stream_count,
247
+                                          coupled_stream_count,
248
+                                          channel_mapping,
249
+                                          opus->opts.application, &ret);
250
+    if (ret != OPUS_OK) {
251
+        av_log(avctx, AV_LOG_ERROR,
252
+               "Failed to create encoder: %s\n", opus_strerror(ret));
253
+        return ff_opus_error_to_averror(ret);
254
+    }
255
+
256
+    ret = libopus_configure_encoder(avctx, enc, &opus->opts);
257
+    if (ret != OPUS_OK) {
258
+        ret = ff_opus_error_to_averror(ret);
259
+        goto fail;
260
+    }
261
+
262
+    header_size = 19 + (avctx->channels > 2 ? 2 + avctx->channels : 0);
263
+    avctx->extradata = av_malloc(header_size + FF_INPUT_BUFFER_PADDING_SIZE);
264
+    if (!avctx->extradata) {
265
+        av_log(avctx, AV_LOG_ERROR, "Failed to allocate extradata.\n");
266
+        ret = AVERROR(ENOMEM);
267
+        goto fail;
268
+    }
269
+    avctx->extradata_size = header_size;
270
+
271
+    opus->samples = av_mallocz(frame_size * avctx->channels *
272
+                               av_get_bytes_per_sample(avctx->sample_fmt));
273
+    if (!opus->samples) {
274
+        av_log(avctx, AV_LOG_ERROR, "Failed to allocate samples buffer.\n");
275
+        ret = AVERROR(ENOMEM);
276
+        goto fail;
277
+    }
278
+
279
+    ret = opus_multistream_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&avctx->delay));
280
+    if (ret != OPUS_OK)
281
+        av_log(avctx, AV_LOG_WARNING,
282
+               "Unable to get number of lookahead samples: %s\n",
283
+               opus_strerror(ret));
284
+
285
+    libopus_write_header(avctx, opus->stream_count, coupled_stream_count,
286
+                         opus_vorbis_channel_map[avctx->channels - 1]);
287
+
288
+    ff_af_queue_init(avctx, &opus->afq);
289
+
290
+    opus->enc = enc;
291
+
292
+    return 0;
293
+
294
+fail:
295
+    opus_multistream_encoder_destroy(enc);
296
+    av_freep(&avctx->extradata);
297
+    return ret;
298
+}
299
+
300
+static int libopus_encode(AVCodecContext *avctx, AVPacket *avpkt,
301
+                          const AVFrame *frame, int *got_packet_ptr)
302
+{
303
+    LibopusEncContext *opus = avctx->priv_data;
304
+    const int sample_size   = avctx->channels *
305
+                              av_get_bytes_per_sample(avctx->sample_fmt);
306
+    uint8_t *audio;
307
+    int ret;
308
+
309
+    if (frame) {
310
+        ff_af_queue_add(&opus->afq, frame);
311
+        if (frame->nb_samples < opus->opts.packet_size) {
312
+            audio = opus->samples;
313
+            memcpy(audio, frame->data[0], frame->nb_samples * sample_size);
314
+        } else
315
+            audio = frame->data[0];
316
+    } else {
317
+        if (!opus->afq.remaining_samples)
318
+            return 0;
319
+        audio = opus->samples;
320
+        memset(audio, 0, opus->opts.packet_size * sample_size);
321
+    }
322
+
323
+    /* Maximum packet size taken from opusenc in opus-tools. 60ms packets
324
+     * consist of 3 frames in one packet. The maximum frame size is 1275
325
+     * bytes along with the largest possible packet header of 7 bytes. */
326
+    if (ret = ff_alloc_packet(avpkt, (1275 * 3 + 7) * opus->stream_count)) {
327
+        av_log(avctx, AV_LOG_ERROR, "Error getting output packet\n");
328
+        return ret;
329
+    }
330
+
331
+    if (avctx->sample_fmt == AV_SAMPLE_FMT_FLT)
332
+        ret = opus_multistream_encode_float(opus->enc, (float *)audio,
333
+                                            opus->opts.packet_size,
334
+                                            avpkt->data, avpkt->size);
335
+    else
336
+        ret = opus_multistream_encode(opus->enc, (opus_int16 *)audio,
337
+                                      opus->opts.packet_size,
338
+                                      avpkt->data, avpkt->size);
339
+
340
+    if (ret < 0) {
341
+        av_log(avctx, AV_LOG_ERROR,
342
+               "Error encoding frame: %s\n", opus_strerror(ret));
343
+        return ff_opus_error_to_averror(ret);
344
+    }
345
+
346
+    av_shrink_packet(avpkt, ret);
347
+
348
+    ff_af_queue_remove(&opus->afq, opus->opts.packet_size,
349
+                       &avpkt->pts, &avpkt->duration);
350
+
351
+    *got_packet_ptr = 1;
352
+
353
+    return 0;
354
+}
355
+
356
+static int av_cold libopus_encode_close(AVCodecContext *avctx)
357
+{
358
+    LibopusEncContext *opus = avctx->priv_data;
359
+
360
+    opus_multistream_encoder_destroy(opus->enc);
361
+
362
+    ff_af_queue_close(&opus->afq);
363
+
364
+    av_freep(&opus->samples);
365
+    av_freep(&avctx->extradata);
366
+
367
+    return 0;
368
+}
369
+
370
+#define OFFSET(x) offsetof(LibopusEncContext, opts.x)
371
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
372
+static const AVOption libopus_options[] = {
373
+    { "application",    "Intended application type",           OFFSET(application),    AV_OPT_TYPE_INT,   { .i64 = OPUS_APPLICATION_AUDIO }, OPUS_APPLICATION_VOIP, OPUS_APPLICATION_RESTRICTED_LOWDELAY, FLAGS, "application" },
374
+        { "voip",           "Favor improved speech intelligibility",   0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_VOIP },                0, 0, FLAGS, "application" },
375
+        { "audio",          "Favor faithfulness to the input",         0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_AUDIO },               0, 0, FLAGS, "application" },
376
+        { "lowdelay",       "Restrict to only the lowest delay modes", 0, AV_OPT_TYPE_CONST, { .i64 = OPUS_APPLICATION_RESTRICTED_LOWDELAY }, 0, 0, FLAGS, "application" },
377
+    { "frame_duration", "Duration of a frame in milliseconds", OFFSET(frame_duration), AV_OPT_TYPE_FLOAT, { .dbl = 10.0 }, 2.5, 60.0, FLAGS },
378
+    { "packet_loss",    "Expected packet loss percentage",     OFFSET(packet_loss),    AV_OPT_TYPE_INT,   { .i64 = 0 },    0,   100,  FLAGS },
379
+    { "vbr",            "Variable bit rate mode",              OFFSET(vbr),            AV_OPT_TYPE_INT,   { .i64 = 1 },    0,   2,    FLAGS, "vbr" },
380
+        { "off",            "Use constant bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, FLAGS, "vbr" },
381
+        { "on",             "Use variable bit rate", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, FLAGS, "vbr" },
382
+        { "constrained",    "Use constrained VBR",   0, AV_OPT_TYPE_CONST, { .i64 = 2 }, 0, 0, FLAGS, "vbr" },
383
+    { NULL },
384
+};
385
+
386
+static const AVClass libopus_class = {
387
+    .class_name = "libopus",
388
+    .item_name  = av_default_item_name,
389
+    .option     = libopus_options,
390
+    .version    = LIBAVUTIL_VERSION_INT,
391
+};
392
+
393
+static const AVCodecDefault libopus_defaults[] = {
394
+    { "b",                 "0" },
395
+    { "compression_level", "10" },
396
+    { NULL },
397
+};
398
+
399
+static const int libopus_sample_rates[] = {
400
+    48000, 24000, 16000, 12000, 8000, 0,
401
+};
402
+
403
+AVCodec ff_libopus_encoder = {
404
+    .name            = "libopus",
405
+    .type            = AVMEDIA_TYPE_AUDIO,
406
+    .id              = AV_CODEC_ID_OPUS,
407
+    .priv_data_size  = sizeof(LibopusEncContext),
408
+    .init            = libopus_encode_init,
409
+    .encode2         = libopus_encode,
410
+    .close           = libopus_encode_close,
411
+    .capabilities    = CODEC_CAP_DELAY | CODEC_CAP_SMALL_LAST_FRAME,
412
+    .sample_fmts     = (const enum AVSampleFormat[]){ AV_SAMPLE_FMT_S16,
413
+                                                      AV_SAMPLE_FMT_FLT,
414
+                                                      AV_SAMPLE_FMT_NONE },
415
+    .channel_layouts = ff_vorbis_channel_layouts,
416
+    .supported_samplerates = libopus_sample_rates,
417
+    .long_name       = NULL_IF_CONFIG_SMALL("libopus Opus"),
418
+    .priv_class      = &libopus_class,
419
+    .defaults        = libopus_defaults,
420
+};
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVCODEC_VERSION_MAJOR 54
32
-#define LIBAVCODEC_VERSION_MINOR 62
32
+#define LIBAVCODEC_VERSION_MINOR 63
33 33
 #define LIBAVCODEC_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -926,7 +926,7 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
926 926
         samples += s->nb_channels * s->frame_len;
927 927
     }
928 928
 
929
-    av_dlog(s->avctx, "%d %d %d %d outbytes:%d eaten:%d\n",
929
+    av_dlog(s->avctx, "%d %d %d %d outbytes:%td eaten:%d\n",
930 930
             s->frame_len_bits, s->block_len_bits, s->frame_len, s->block_len,
931 931
             (int8_t *)samples - (int8_t *)data, s->block_align);
932 932
 
... ...
@@ -1813,7 +1813,7 @@ int av_get_output_timestamp(struct AVFormatContext *s, int stream,
1813 1813
  *
1814 1814
  * @see av_hex_dump_log, av_pkt_dump2, av_pkt_dump_log2
1815 1815
  */
1816
-void av_hex_dump(FILE *f, uint8_t *buf, int size);
1816
+void av_hex_dump(FILE *f, const uint8_t *buf, int size);
1817 1817
 
1818 1818
 /**
1819 1819
  * Send a nice hexadecimal dump of a buffer to the log.
... ...
@@ -1827,7 +1827,7 @@ void av_hex_dump(FILE *f, uint8_t *buf, int size);
1827 1827
  *
1828 1828
  * @see av_hex_dump, av_pkt_dump2, av_pkt_dump_log2
1829 1829
  */
1830
-void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size);
1830
+void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size);
1831 1831
 
1832 1832
 /**
1833 1833
  * Send a nice dump of a packet to the specified file stream.
... ...
@@ -1414,7 +1414,7 @@ static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
1414 1414
     int i;
1415 1415
 
1416 1416
     av_dlog(ts->stream, "PMT: len %i\n", section_len);
1417
-    hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
1417
+    hex_dump_debug(ts->stream, section, section_len);
1418 1418
 
1419 1419
     p_end = section + section_len - 4;
1420 1420
     p = section;
... ...
@@ -1553,7 +1553,7 @@ static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
1553 1553
     AVProgram *program;
1554 1554
 
1555 1555
     av_dlog(ts->stream, "PAT:\n");
1556
-    hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
1556
+    hex_dump_debug(ts->stream, section, section_len);
1557 1557
 
1558 1558
     p_end = section + section_len - 4;
1559 1559
     p = section;
... ...
@@ -1601,7 +1601,7 @@ static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len
1601 1601
     char *name, *provider_name;
1602 1602
 
1603 1603
     av_dlog(ts->stream, "SDT:\n");
1604
-    hex_dump_debug(ts->stream, (uint8_t *)section, section_len);
1604
+    hex_dump_debug(ts->stream, section, section_len);
1605 1605
 
1606 1606
     p_end = section + section_len - 4;
1607 1607
     p = section;
... ...
@@ -4146,7 +4146,8 @@ int av_get_frame_filename(char *buf, int buf_size,
4146 4146
     return -1;
4147 4147
 }
4148 4148
 
4149
-static void hex_dump_internal(void *avcl, FILE *f, int level, uint8_t *buf, int size)
4149
+static void hex_dump_internal(void *avcl, FILE *f, int level,
4150
+                              const uint8_t *buf, int size)
4150 4151
 {
4151 4152
     int len, i, j, c;
4152 4153
 #undef fprintf
... ...
@@ -4175,12 +4176,12 @@ static void hex_dump_internal(void *avcl, FILE *f, int level, uint8_t *buf, int
4175 4175
 #undef PRINT
4176 4176
 }
4177 4177
 
4178
-void av_hex_dump(FILE *f, uint8_t *buf, int size)
4178
+void av_hex_dump(FILE *f, const uint8_t *buf, int size)
4179 4179
 {
4180 4180
     hex_dump_internal(NULL, f, 0, buf, size);
4181 4181
 }
4182 4182
 
4183
-void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size)
4183
+void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
4184 4184
 {
4185 4185
     hex_dump_internal(avcl, NULL, level, buf, size);
4186 4186
 }