@@ -74,14 +74,14 @@ static jmethodID growOutputBufferMethod;
74
74
/* *
75
75
* Returns the AVCodec with the specified name, or NULL if it is not available.
76
76
*/
77
- AVCodec *getCodecByName (JNIEnv *env, jstring codecName);
77
+ const AVCodec *getCodecByName (JNIEnv *env, jstring codecName);
78
78
79
79
/* *
80
80
* Allocates and opens a new AVCodecContext for the specified codec, passing the
81
81
* provided extraData as initialization data for the decoder if it is non-NULL.
82
82
* Returns the created context.
83
83
*/
84
- AVCodecContext *createContext (JNIEnv *env, AVCodec *codec, jbyteArray extraData,
84
+ AVCodecContext *createContext (JNIEnv *env, const AVCodec *codec, jbyteArray extraData,
85
85
jboolean outputFloat, jint rawSampleRate,
86
86
jint rawChannelCount);
87
87
@@ -137,7 +137,6 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
137
137
LOGE (" JNI_OnLoad: GetMethodID failed" );
138
138
return -1 ;
139
139
}
140
- avcodec_register_all ();
141
140
return JNI_VERSION_1_6;
142
141
}
143
142
@@ -156,7 +155,7 @@ LIBRARY_FUNC(jboolean, ffmpegHasDecoder, jstring codecName) {
156
155
AUDIO_DECODER_FUNC (jlong, ffmpegInitialize, jstring codecName,
157
156
jbyteArray extraData, jboolean outputFloat,
158
157
jint rawSampleRate, jint rawChannelCount) {
159
- AVCodec *codec = getCodecByName (env, codecName);
158
+ const AVCodec *codec = getCodecByName (env, codecName);
160
159
if (!codec) {
161
160
LOGE (" Codec not found." );
162
161
return 0L ;
@@ -186,13 +185,17 @@ AUDIO_DECODER_FUNC(jint, ffmpegDecode, jlong context, jobject inputData,
186
185
}
187
186
uint8_t *inputBuffer = (uint8_t *)env->GetDirectBufferAddress (inputData);
188
187
uint8_t *outputBuffer = (uint8_t *)env->GetDirectBufferAddress (outputData);
189
- AVPacket packet;
190
- av_init_packet (&packet);
191
- packet.data = inputBuffer;
192
- packet.size = inputSize;
193
- return decodePacket ((AVCodecContext *)context, &packet, outputBuffer,
194
- outputSize,
195
- GrowOutputBufferCallback{env, thiz, decoderOutputBuffer});
188
+ AVPacket* packet = av_packet_alloc ();
189
+ if (!packet) {
190
+ LOGE (" Failed to allocate packet." );
191
+ return -1 ;
192
+ }
193
+ packet->data = inputBuffer;
194
+ packet->size = inputSize;
195
+ const int ret = decodePacket ((AVCodecContext *)context, packet, outputBuffer,
196
+ outputSize, GrowOutputBufferCallback{env, thiz, decoderOutputBuffer});
197
+ av_packet_free (&packet);
198
+ return ret;
196
199
}
197
200
198
201
uint8_t *GrowOutputBufferCallback::operator ()(int requiredSize) const {
@@ -211,7 +214,7 @@ AUDIO_DECODER_FUNC(jint, ffmpegGetChannelCount, jlong context) {
211
214
LOGE (" Context must be non-NULL." );
212
215
return -1 ;
213
216
}
214
- return ((AVCodecContext *)context)->channels ;
217
+ return ((AVCodecContext *)context)->ch_layout . nb_channels ;
215
218
}
216
219
217
220
AUDIO_DECODER_FUNC (jint, ffmpegGetSampleRate, jlong context) {
@@ -234,7 +237,7 @@ AUDIO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
234
237
// Release and recreate the context if the codec is TrueHD.
235
238
// TODO: Figure out why flushing doesn't work for this codec.
236
239
releaseContext (context);
237
- AVCodec *codec = avcodec_find_decoder (codecId);
240
+ const AVCodec *codec = avcodec_find_decoder (codecId);
238
241
if (!codec) {
239
242
LOGE (" Unexpected error finding codec %d." , codecId);
240
243
return 0L ;
@@ -256,17 +259,17 @@ AUDIO_DECODER_FUNC(void, ffmpegRelease, jlong context) {
256
259
}
257
260
}
258
261
259
- AVCodec *getCodecByName (JNIEnv *env, jstring codecName) {
262
+ const AVCodec *getCodecByName (JNIEnv *env, jstring codecName) {
260
263
if (!codecName) {
261
264
return NULL ;
262
265
}
263
266
const char *codecNameChars = env->GetStringUTFChars (codecName, NULL );
264
- AVCodec *codec = avcodec_find_decoder_by_name (codecNameChars);
267
+ const AVCodec *codec = avcodec_find_decoder_by_name (codecNameChars);
265
268
env->ReleaseStringUTFChars (codecName, codecNameChars);
266
269
return codec;
267
270
}
268
271
269
- AVCodecContext *createContext (JNIEnv *env, AVCodec *codec, jbyteArray extraData,
272
+ AVCodecContext *createContext (JNIEnv *env, const AVCodec *codec, jbyteArray extraData,
270
273
jboolean outputFloat, jint rawSampleRate,
271
274
jint rawChannelCount) {
272
275
AVCodecContext *context = avcodec_alloc_context3 (codec);
@@ -291,8 +294,7 @@ AVCodecContext *createContext(JNIEnv *env, AVCodec *codec, jbyteArray extraData,
291
294
if (context->codec_id == AV_CODEC_ID_PCM_MULAW ||
292
295
context->codec_id == AV_CODEC_ID_PCM_ALAW) {
293
296
context->sample_rate = rawSampleRate;
294
- context->channels = rawChannelCount;
295
- context->channel_layout = av_get_default_channel_layout (rawChannelCount);
297
+ av_channel_layout_default (&context->ch_layout , rawChannelCount);
296
298
}
297
299
context->err_recognition = AV_EF_IGNORE_ERR;
298
300
int result = avcodec_open2 (context, codec, NULL );
@@ -335,8 +337,7 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
335
337
336
338
// Resample output.
337
339
AVSampleFormat sampleFormat = context->sample_fmt ;
338
- int channelCount = context->channels ;
339
- int channelLayout = context->channel_layout ;
340
+ int channelCount = context->ch_layout .nb_channels ;
340
341
int sampleRate = context->sample_rate ;
341
342
int sampleCount = frame->nb_samples ;
342
343
int dataSize = av_samples_get_buffer_size (NULL , channelCount, sampleCount,
@@ -346,8 +347,8 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
346
347
resampleContext = (SwrContext *)context->opaque ;
347
348
} else {
348
349
resampleContext = swr_alloc ();
349
- av_opt_set_int (resampleContext, " in_channel_layout " , channelLayout , 0 );
350
- av_opt_set_int (resampleContext, " out_channel_layout " , channelLayout , 0 );
350
+ av_opt_set_chlayout (resampleContext, " in_chlayout " , &context-> ch_layout , 0 );
351
+ av_opt_set_chlayout (resampleContext, " out_chlayout " , &context-> ch_layout , 0 );
351
352
av_opt_set_int (resampleContext, " in_sample_rate" , sampleRate, 0 );
352
353
av_opt_set_int (resampleContext, " out_sample_rate" , sampleRate, 0 );
353
354
av_opt_set_int (resampleContext, " in_sample_fmt" , sampleFormat, 0 );
0 commit comments