@@ -70,16 +70,16 @@ 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 ,
81
- jboolean outputFloat, jint rawSampleRate ,
82
- jint rawChannelCount);
80
+ AVCodecContext *createContext (JNIEnv *env, const AVCodec *codec,
81
+ jbyteArray extraData, jboolean outputFloat ,
82
+ jint rawSampleRate, jint rawChannelCount);
83
83
84
84
/* *
85
85
* Decodes the packet into the output buffer, returning the number of bytes
@@ -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,38 @@ 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,
165
- outputSize);
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 =
167
+ decodePacket ((AVCodecContext *)context, packet, outputBuffer, outputSize,
168
+ GrowOutputBufferCallback{env, thiz, decoderOutputBuffer});
169
+ av_packet_free (&packet);
170
+ return ret;
171
+ }
172
+
173
+ uint8_t *GrowOutputBufferCallback::operator ()(int requiredSize) const {
174
+ jobject newOutputData = env->CallObjectMethod (
175
+ thiz, growOutputBufferMethod, decoderOutputBuffer, requiredSize);
176
+ if (env->ExceptionCheck ()) {
177
+ LOGE (" growOutputBuffer() failed" );
178
+ env->ExceptionDescribe ();
179
+ return nullptr ;
180
+ }
181
+ return static_cast <uint8_t *>(env->GetDirectBufferAddress (newOutputData));
182
+ >>>>>>> 45b51d8c97 (Merge pull request #707 from equeim:ffmpeg-6.0 )
166
183
}
167
184
168
185
AUDIO_DECODER_FUNC (jint, ffmpegGetChannelCount, jlong context) {
169
186
if (!context) {
170
187
LOGE (" Context must be non-NULL." );
171
188
return -1 ;
172
189
}
173
- return ((AVCodecContext *)context)->channels ;
190
+ return ((AVCodecContext *)context)->ch_layout . nb_channels ;
174
191
}
175
192
176
193
AUDIO_DECODER_FUNC (jint, ffmpegGetSampleRate, jlong context) {
@@ -193,7 +210,7 @@ AUDIO_DECODER_FUNC(jlong, ffmpegReset, jlong jContext, jbyteArray extraData) {
193
210
// Release and recreate the context if the codec is TrueHD.
194
211
// TODO: Figure out why flushing doesn't work for this codec.
195
212
releaseContext (context);
196
- AVCodec *codec = avcodec_find_decoder (codecId);
213
+ const AVCodec *codec = avcodec_find_decoder (codecId);
197
214
if (!codec) {
198
215
LOGE (" Unexpected error finding codec %d." , codecId);
199
216
return 0L ;
@@ -215,19 +232,19 @@ AUDIO_DECODER_FUNC(void, ffmpegRelease, jlong context) {
215
232
}
216
233
}
217
234
218
- AVCodec *getCodecByName (JNIEnv *env, jstring codecName) {
235
+ const AVCodec *getCodecByName (JNIEnv *env, jstring codecName) {
219
236
if (!codecName) {
220
237
return NULL ;
221
238
}
222
239
const char *codecNameChars = env->GetStringUTFChars (codecName, NULL );
223
- AVCodec *codec = avcodec_find_decoder_by_name (codecNameChars);
240
+ const AVCodec *codec = avcodec_find_decoder_by_name (codecNameChars);
224
241
env->ReleaseStringUTFChars (codecName, codecNameChars);
225
242
return codec;
226
243
}
227
244
228
- AVCodecContext *createContext (JNIEnv *env, AVCodec *codec, jbyteArray extraData ,
229
- jboolean outputFloat, jint rawSampleRate ,
230
- jint rawChannelCount) {
245
+ AVCodecContext *createContext (JNIEnv *env, const AVCodec *codec,
246
+ jbyteArray extraData, jboolean outputFloat ,
247
+ jint rawSampleRate, jint rawChannelCount) {
231
248
AVCodecContext *context = avcodec_alloc_context3 (codec);
232
249
if (!context) {
233
250
LOGE (" Failed to allocate context." );
@@ -250,8 +267,7 @@ AVCodecContext *createContext(JNIEnv *env, AVCodec *codec, jbyteArray extraData,
250
267
if (context->codec_id == AV_CODEC_ID_PCM_MULAW ||
251
268
context->codec_id == AV_CODEC_ID_PCM_ALAW) {
252
269
context->sample_rate = rawSampleRate;
253
- context->channels = rawChannelCount;
254
- context->channel_layout = av_get_default_channel_layout (rawChannelCount);
270
+ av_channel_layout_default (&context->ch_layout , rawChannelCount);
255
271
}
256
272
context->err_recognition = AV_EF_IGNORE_ERR;
257
273
int result = avcodec_open2 (context, codec, NULL );
@@ -293,25 +309,29 @@ int decodePacket(AVCodecContext *context, AVPacket *packet,
293
309
294
310
// Resample output.
295
311
AVSampleFormat sampleFormat = context->sample_fmt ;
296
- int channelCount = context->channels ;
297
- int channelLayout = context->channel_layout ;
312
+ int channelCount = context->ch_layout .nb_channels ;
298
313
int sampleRate = context->sample_rate ;
299
314
int sampleCount = frame->nb_samples ;
300
315
int dataSize = av_samples_get_buffer_size (NULL , channelCount, sampleCount,
301
316
sampleFormat, 1 );
302
- SwrContext *resampleContext;
303
- if (context->opaque ) {
304
- resampleContext = (SwrContext *)context->opaque ;
305
- } else {
306
- 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_int (resampleContext, " in_sample_rate" , sampleRate, 0 );
310
- av_opt_set_int (resampleContext, " out_sample_rate" , sampleRate, 0 );
311
- av_opt_set_int (resampleContext, " in_sample_fmt" , sampleFormat, 0 );
312
- // The output format is always the requested format.
313
- av_opt_set_int (resampleContext, " out_sample_fmt" ,
314
- context->request_sample_fmt , 0 );
317
+ SwrContext *resampleContext = static_cast <SwrContext *>(context->opaque );
318
+ if (!resampleContext) {
319
+ result =
320
+ swr_alloc_set_opts2 (&resampleContext, // ps
321
+ &context->ch_layout , // out_ch_layout
322
+ context->request_sample_fmt , // out_sample_fmt
323
+ sampleRate, // out_sample_rate
324
+ &context->ch_layout , // in_ch_layout
325
+ sampleFormat, // in_sample_fmt
326
+ sampleRate, // in_sample_rate
327
+ 0 , // log_offset
328
+ NULL // log_ctx
329
+ );
330
+ if (result < 0 ) {
331
+ logError (" swr_alloc_set_opts2" , result);
332
+ av_frame_free (&frame);
333
+ return transformError (result);
334
+ }
315
335
result = swr_init (resampleContext);
316
336
if (result < 0 ) {
317
337
logError (" swr_init" , result);
0 commit comments