@@ -70,14 +70,14 @@ static const int AUDIO_DECODER_ERROR_OTHER = -2;
70
70
/* *
71
71
* Returns the AVCodec with the specified name, or NULL if it is not available.
72
72
*/
73
- AVCodec *getCodecByName (JNIEnv *env, jstring codecName);
73
+ const AVCodec *getCodecByName (JNIEnv *env, jstring codecName);
74
74
75
75
/* *
76
76
* Allocates and opens a new AVCodecContext for the specified codec, passing the
77
77
* provided extraData as initialization data for the decoder if it is non-NULL.
78
78
* Returns the created context.
79
79
*/
80
- AVCodecContext *createContext (JNIEnv *env, AVCodec *codec, jbyteArray extraData,
80
+ AVCodecContext *createContext (JNIEnv *env, const AVCodec *codec, jbyteArray extraData,
81
81
jboolean outputFloat, jint rawSampleRate,
82
82
jint rawChannelCount);
83
83
@@ -109,7 +109,6 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
109
109
if (vm->GetEnv (reinterpret_cast <void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
110
110
return -1 ;
111
111
}
112
- avcodec_register_all ();
113
112
return JNI_VERSION_1_6;
114
113
}
115
114
@@ -128,7 +127,7 @@ LIBRARY_FUNC(jboolean, ffmpegHasDecoder, jstring codecName) {
128
127
AUDIO_DECODER_FUNC (jlong, ffmpegInitialize, jstring codecName,
129
128
jbyteArray extraData, jboolean outputFloat,
130
129
jint rawSampleRate, jint rawChannelCount) {
131
- AVCodec *codec = getCodecByName (env, codecName);
130
+ const AVCodec *codec = getCodecByName (env, codecName);
132
131
if (!codec) {
133
132
LOGE (" Codec not found." );
134
133
return 0L ;
@@ -157,20 +156,25 @@ AUDIO_DECODER_FUNC(jint, ffmpegDecode, jlong context, jobject inputData,
157
156
}
158
157
uint8_t *inputBuffer = (uint8_t *)env->GetDirectBufferAddress (inputData);
159
158
uint8_t *outputBuffer = (uint8_t *)env->GetDirectBufferAddress (outputData);
160
- AVPacket packet;
161
- av_init_packet (&packet);
162
- packet.data = inputBuffer;
163
- packet.size = inputSize;
164
- return decodePacket ((AVCodecContext *)context, &packet, outputBuffer,
159
+ AVPacket* packet = av_packet_alloc ();
160
+ if (!packet) {
161
+ LOGE (" Failed to allocate packet." );
162
+ return -1 ;
163
+ }
164
+ packet->data = inputBuffer;
165
+ packet->size = inputSize;
166
+ const int ret = decodePacket ((AVCodecContext *)context, packet, outputBuffer,
165
167
outputSize);
168
+ av_packet_free (&packet);
169
+ return ret;
166
170
}
167
171
168
172
AUDIO_DECODER_FUNC (jint, ffmpegGetChannelCount, jlong context) {
169
173
if (!context) {
170
174
LOGE (" Context must be non-NULL." );
171
175
return -1 ;
172
176
}
173
- return ((AVCodecContext *)context)->channels ;
177
+ return ((AVCodecContext *)context)->ch_layout . nb_channels ;
174
178
}
175
179
176
180
AUDIO_DECODER_FUNC (jint, ffmpegGetSampleRate, jlong context) {
@@ -193,7 +197,7 @@ AUDIO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
193
197
// Release and recreate the context if the codec is TrueHD.
194
198
// TODO: Figure out why flushing doesn't work for this codec.
195
199
releaseContext (context);
196
- AVCodec *codec = avcodec_find_decoder (codecId);
200
+ const AVCodec *codec = avcodec_find_decoder (codecId);
197
201
if (!codec) {
198
202
LOGE (" Unexpected error finding codec %d." , codecId);
199
203
return 0L ;
@@ -215,17 +219,17 @@ AUDIO_DECODER_FUNC(void, ffmpegRelease, jlong context) {
215
219
}
216
220
}
217
221
218
- AVCodec *getCodecByName (JNIEnv *env, jstring codecName) {
222
+ const AVCodec *getCodecByName (JNIEnv *env, jstring codecName) {
219
223
if (!codecName) {
220
224
return NULL ;
221
225
}
222
226
const char *codecNameChars = env->GetStringUTFChars (codecName, NULL );
223
- AVCodec *codec = avcodec_find_decoder_by_name (codecNameChars);
227
+ const AVCodec *codec = avcodec_find_decoder_by_name (codecNameChars);
224
228
env->ReleaseStringUTFChars (codecName, codecNameChars);
225
229
return codec;
226
230
}
227
231
228
- AVCodecContext *createContext (JNIEnv *env, AVCodec *codec, jbyteArray extraData,
232
+ AVCodecContext *createContext (JNIEnv *env, const AVCodec *codec, jbyteArray extraData,
229
233
jboolean outputFloat, jint rawSampleRate,
230
234
jint rawChannelCount) {
231
235
AVCodecContext *context = avcodec_alloc_context3 (codec);
@@ -250,8 +254,7 @@ AVCodecContext *createContext(JNIEnv *env, AVCodec *codec, jbyteArray extraData,
250
254
if (context->codec_id == AV_CODEC_ID_PCM_MULAW ||
251
255
context->codec_id == AV_CODEC_ID_PCM_ALAW) {
252
256
context->sample_rate = rawSampleRate;
253
- context->channels = rawChannelCount;
254
- context->channel_layout = av_get_default_channel_layout (rawChannelCount);
257
+ av_channel_layout_default (&context->ch_layout , rawChannelCount);
255
258
}
256
259
context->err_recognition = AV_EF_IGNORE_ERR;
257
260
int result = avcodec_open2 (context, codec, NULL );
@@ -293,8 +296,7 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
293
296
294
297
// Resample output.
295
298
AVSampleFormat sampleFormat = context->sample_fmt ;
296
- int channelCount = context->channels ;
297
- int channelLayout = context->channel_layout ;
299
+ int channelCount = context->ch_layout .nb_channels ;
298
300
int sampleRate = context->sample_rate ;
299
301
int sampleCount = frame->nb_samples ;
300
302
int dataSize = av_samples_get_buffer_size (NULL , channelCount, sampleCount,
@@ -304,8 +306,8 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
304
306
resampleContext = (SwrContext *)context->opaque ;
305
307
} else {
306
308
resampleContext = swr_alloc ();
307
- av_opt_set_int (resampleContext, " in_channel_layout" , channelLayout , 0 );
308
- av_opt_set_int (resampleContext, " out_channel_layout" , channelLayout , 0 );
309
+ av_opt_set_chlayout (resampleContext, " in_channel_layout" , &context-> ch_layout , 0 );
310
+ av_opt_set_chlayout (resampleContext, " out_channel_layout" , &context-> ch_layout , 0 );
309
311
av_opt_set_int (resampleContext, " in_sample_rate" , sampleRate, 0 );
310
312
av_opt_set_int (resampleContext, " out_sample_rate" , sampleRate, 0 );
311
313
av_opt_set_int (resampleContext, " in_sample_fmt" , sampleFormat, 0 );
0 commit comments