FFSampledSP
FFUtils.c
Go to the documentation of this file.
1 /*
2  * =================================================
3  * Copyright 2013 tagtraum industries incorporated
4  * This file is part of FFSampledSP.
5  *
6  * FFSampledSP 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  * FFSampledSP 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 FFSampledSP; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  * =================================================
20  *
21  * @author <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
22  */
23 
24 #include "FFUtils.h"
25 
26 static jfieldID nativeBuffer_FID = NULL;
27 static jmethodID rewind_MID = NULL;
28 static jmethodID limit_MID = NULL;
29 static jmethodID capacity_MID = NULL;
30 static jmethodID setNativeBufferCapacity_MID = NULL;
31 static jmethodID logFine_MID = NULL;
32 static jmethodID logWarning_MID = NULL;
33 static int MIN_PROBE_SCORE = 5; // this is fairly arbitrary, but we need to give other javax.sound.sampled impls a chance
34 
41 static void init_ids(JNIEnv *env, jobject stream) {
42  if (!nativeBuffer_FID || !rewind_MID || !limit_MID) {
43  jclass bufferClass = NULL;
44  jclass streamClass = NULL;
45 
46  bufferClass = (*env)->FindClass(env, "java/nio/Buffer");
47  streamClass = (*env)->GetObjectClass(env, stream);
48 
49  nativeBuffer_FID = (*env)->GetFieldID(env, streamClass, "nativeBuffer", "Ljava/nio/ByteBuffer;");
50  rewind_MID = (*env)->GetMethodID(env, bufferClass, "rewind", "()Ljava/nio/Buffer;");
51  limit_MID = (*env)->GetMethodID(env, bufferClass, "limit", "(I)Ljava/nio/Buffer;");
52  capacity_MID = (*env)->GetMethodID(env, bufferClass, "capacity", "()I");
53  setNativeBufferCapacity_MID = (*env)->GetMethodID(env, streamClass, "setNativeBufferCapacity", "(I)I");
54  logFine_MID = (*env)->GetMethodID(env, streamClass, "logFine", "(Ljava/lang/String;)V");
55  logWarning_MID = (*env)->GetMethodID(env, streamClass, "logWarning", "(Ljava/lang/String;)V");
56  }
57 }
58 
65 int ff_open_stream(JNIEnv *env, AVStream *stream, AVCodecContext **context) {
66 #ifdef DEBUG
67  fprintf(stderr, "Opening stream...\n");
68 #endif
69 
70  int res = 0;
71  AVCodec *decoder = NULL;
72  AVDictionary *opts = NULL;
73  int refcount = 0; // is this correct?
74 
75  decoder = avcodec_find_decoder(stream->codecpar->codec_id);
76  if (!decoder) {
77  fprintf(stderr, "Failed to find %s codec\n", av_get_media_type_string(AVMEDIA_TYPE_AUDIO));
78  res = AVERROR(EINVAL);
79  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to find codec.");
80  goto bail;
81  }
82 
83  *context = avcodec_alloc_context3(decoder);
84  if (!context) {
85  fprintf(stderr, "Failed to allocate context\n");
86  res = AVERROR(EINVAL);
87  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to allocate codec context.");
88  goto bail;
89  }
90 
91  /* Copy codec parameters from input stream to output codec context */
92  if ((res = avcodec_parameters_to_context(*context, stream->codecpar)) < 0) {
93  fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n", av_get_media_type_string(AVMEDIA_TYPE_AUDIO));
94  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to copy codec parameters.");
95  goto bail;
96  }
97 
98  /* Init the decoders, with or without reference counting */
99  av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0);
100  if ((res = avcodec_open2(*context, decoder, &opts)) < 0) {
101  fprintf(stderr, "Failed to open %s codec\n", av_get_media_type_string(AVMEDIA_TYPE_AUDIO));
102  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to open codec.");
103  goto bail;
104  }
105 
106 #ifdef DEBUG
107  fprintf(stderr, "Stream was opened.\n");
108 #endif
109  return res;
110 
111 bail:
112  return res;
113 }
114 
122 static int open_codec_context(int *stream_index,
123  AVFormatContext *format_context,
124  AVCodecContext *context,
125  enum AVMediaType type) {
126 
127  int res = 0;
128  AVStream *stream;
129  AVCodec *decoder = NULL;
130 
131  res = av_find_best_stream(format_context, type, -1, -1, NULL, 0);
132  if (res < 0) {
133  goto bail;
134  }
135  *stream_index = res;
136  stream = format_context->streams[*stream_index];
137 
138  // find decoder for the stream
139  decoder = avcodec_find_decoder(stream->codecpar->codec_id);
140  if (!decoder) {
141  goto bail;
142  }
143  context = avcodec_alloc_context3(decoder);
144  if (!context) {
145  goto bail;
146  }
147 
148  res = avcodec_open2(context, decoder, NULL);
149  if (res < 0) {
150  goto bail;
151  }
152  return res;
153 
154 bail:
155  return res;
156 }
157 
158 
162 void throwUnsupportedAudioFileExceptionIfError(JNIEnv *env, int err, const char * message) {
163  if (err) {
164  char formattedMessage [strlen(message)+4+AV_ERROR_MAX_STRING_SIZE];
165  snprintf(formattedMessage, strlen(message)+4+AV_ERROR_MAX_STRING_SIZE, "%s (%.64s)", message, av_err2str(err));
166 #ifdef DEBUG
167  fprintf(stderr, "UnsupportedAudioFileException: %s\n", formattedMessage);
168 #endif
169  jclass excCls = (*env)->FindClass(env, "javax/sound/sampled/UnsupportedAudioFileException");
170  (*env)->ThrowNew(env, excCls, formattedMessage);
171  }
172 }
173 
177 void throwIndexOutOfBoundsExceptionIfError(JNIEnv *env, int err, int index) {
178  if (err) {
179  char formattedMessage [15];
180  snprintf(formattedMessage, 15, "%d", index);
181 #ifdef DEBUG
182  fprintf(stderr, "IndexOutOfBoundsException: %d\n", index);
183 #endif
184  jclass excCls = (*env)->FindClass(env, "java/lang/IndexOutOfBoundsException");
185  (*env)->ThrowNew(env, excCls, formattedMessage);
186  }
187 }
188 
192 void throwIOExceptionIfError(JNIEnv *env, int err, const char *message) {
193  if (err) {
194  char formattedMessage [strlen(message)+4+AV_ERROR_MAX_STRING_SIZE];
195  snprintf(formattedMessage, strlen(message)+4+AV_ERROR_MAX_STRING_SIZE, "%s (%.64s)", message, av_err2str(err));
196 #ifdef DEBUG
197  fprintf(stderr, "IOException: %s\n", formattedMessage);
198 #endif
199  jclass excCls = (*env)->FindClass(env, "java/io/IOException");
200  (*env)->ThrowNew(env, excCls, formattedMessage);
201  }
202 }
203 
207 void throwFileNotFoundExceptionIfError(JNIEnv *env, int err, const char *message) {
208  if (err) {
209 #ifdef DEBUG
210  fprintf (stderr, "FileNotFoundException: '%s' %d (%4.4s)\n", message, (int)err, (char*)&err);
211 #endif
212  jclass excCls = (*env)->FindClass(env, "java/io/FileNotFoundException");
213  (*env)->ThrowNew(env, excCls, message);
214  }
215 }
216 
220 void logWarning(FFAudioIO *aio, int err, const char *message) {
221  if (err) {
222  char formattedMessage [strlen(message)+20+AV_ERROR_MAX_STRING_SIZE];
223  snprintf(formattedMessage, strlen(message)+20+AV_ERROR_MAX_STRING_SIZE, "%s %i (%.64s)", message, err, av_err2str(err));
224  jstring s = (*aio->env)->NewStringUTF(aio->env, formattedMessage);
225  (*aio->env)->CallVoidMethod(aio->env, aio->java_instance, logWarning_MID, s);
226  } else {
227  jstring s = (*aio->env)->NewStringUTF(aio->env, message);
228  (*aio->env)->CallVoidMethod(aio->env, aio->java_instance, logWarning_MID, s);
229  }
230 }
231 
235 void logFine(FFAudioIO *aio, int err, const char *message) {
236  if (err) {
237  char formattedMessage [strlen(message)+20+AV_ERROR_MAX_STRING_SIZE];
238  snprintf(formattedMessage, strlen(message)+20+AV_ERROR_MAX_STRING_SIZE, "%s %i (%.64s)", message, err, av_err2str(err));
239  jstring s = (*aio->env)->NewStringUTF(aio->env, formattedMessage);
240  (*aio->env)->CallVoidMethod(aio->env, aio->java_instance, logFine_MID, s);
241  } else {
242  jstring s = (*aio->env)->NewStringUTF(aio->env, message);
243  (*aio->env)->CallVoidMethod(aio->env, aio->java_instance, logFine_MID, s);
244  }
245 }
246 
256 int ff_open_format_context(JNIEnv *env, AVFormatContext **format_context, const char *url) {
257  int res = 0;
258  int probe_score = 0;
259 
260  res = avformat_open_input(format_context, url, NULL, NULL);
261  if (res) {
262  if (res == AVERROR(ENOENT) || res == AVERROR_HTTP_NOT_FOUND) {
263  throwFileNotFoundExceptionIfError(env, res, url);
264  } else if (res == AVERROR_PROTOCOL_NOT_FOUND
265  || res == AVERROR_HTTP_BAD_REQUEST
266  || res == AVERROR_HTTP_UNAUTHORIZED
267  || res == AVERROR_HTTP_FORBIDDEN
268  || res == AVERROR_HTTP_OTHER_4XX
269  || res == AVERROR_HTTP_SERVER_ERROR
270  || res == AVERROR(EIO)) {
271  throwIOExceptionIfError(env, res, url);
272  } else {
273  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to open audio file");
274  }
275  goto bail;
276  }
277  probe_score = (*format_context)->probe_score;
278 
279  #ifdef DEBUG
280  fprintf(stderr, "ff_open_format_context(): probe score=%i\n", probe_score);
281  #endif
282 
283  if (probe_score < MIN_PROBE_SCORE) {
284  res = probe_score;
285  throwUnsupportedAudioFileExceptionIfError(env, probe_score, "Probe score too low");
286  goto bail;
287  }
288 
289  res = avformat_find_stream_info(*format_context, NULL);
290  if (res < 0) {
291  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to find stream info");
292  goto bail;
293  }
294 
295 bail:
296 
297  return res;
298 }
299 
312 int ff_open_file(JNIEnv *env, AVFormatContext **format_context, AVStream **openedStream, AVCodecContext **context, int *stream_index, const char *url) {
313  int res = 0;
314  res = ff_open_format_context(env, format_context, url);
315  if (res) {
316  // exception has already been thrown
317  goto bail;
318  }
319 
320 #ifdef DEBUG
321  fprintf(stderr, "Desired audio stream index: %i.\n", *stream_index);
322 #endif
323 
324  if (*stream_index < 0) {
325  // use best audio stream
326  res = open_codec_context(stream_index, *format_context, *context, AVMEDIA_TYPE_AUDIO);
327  if (res) {
328  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to open codec context.");
329  goto bail;
330  }
331  *openedStream = (*format_context)->streams[*stream_index];
332  } else {
333  // find xth audio stream
334  // count possible audio streams
335  int i;
336  int audio_stream_number = 0;
337  AVStream* stream = NULL;
338 
339  AVFormatContext* deref_format_context = *format_context;
340  for (i=0; i<deref_format_context->nb_streams; i++) {
341  stream = deref_format_context->streams[i];
342  if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
343  if (audio_stream_number == *stream_index) {
344  *stream_index = i;
345  #ifdef DEBUG
346  fprintf(stderr, "Found desired audio stream at index: %i.\n", i);
347  #endif
348  break;
349  }
350  audio_stream_number++;
351  }
352  stream = NULL;
353  }
354  if (stream == NULL) {
355  // we didn't find a stream with the given index
356  res = -1;
357  throwIndexOutOfBoundsExceptionIfError(env, res, *stream_index);
358  goto bail;
359  }
360  res = ff_open_stream(env, stream, context);
361  if (res) {
362  goto bail;
363  }
364  *openedStream = stream;
365  }
366 
367 #ifdef DEBUG
368  fprintf(stderr, "Opened stream index: %i.\n", *stream_index);
369  fprintf(stderr, "Opened stream: %ld.\n", (long) *openedStream);
370 #endif
371 
372 bail:
373 
374  return res;
375 }
376 
384 static int init_swr(JNIEnv *env, FFAudioIO *aio) {
385  int res = 0;
386 
387  aio->swr_context = swr_alloc();
388  if (!aio->swr_context) {
389  res = AVERROR(ENOMEM);
390  throwIOExceptionIfError(env, res, "Could not allocate swr context.");
391  goto bail;
392  }
393 
394  av_opt_set_sample_fmt(aio->swr_context, "in_sample_fmt", aio->stream->codecpar->format, 0);
395  // make sure we get interleaved/packed output
396  av_opt_set_sample_fmt(aio->swr_context, "out_sample_fmt", av_get_packed_sample_fmt(aio->stream->codecpar->format), 0);
397 
398  // keep everything else the way it was...
399  av_opt_set_int(aio->swr_context, "in_channel_count", aio->stream->codecpar->channels, 0);
400  av_opt_set_int(aio->swr_context, "out_channel_count", aio->stream->codecpar->channels, 0);
401  av_opt_set_int(aio->swr_context, "in_channel_layout", aio->stream->codecpar->channel_layout, 0);
402  av_opt_set_int(aio->swr_context, "out_channel_layout", aio->stream->codecpar->channel_layout, 0);
403  av_opt_set_int(aio->swr_context, "in_sample_rate", aio->stream->codecpar->sample_rate, 0);
404  av_opt_set_int(aio->swr_context, "out_sample_rate", aio->stream->codecpar->sample_rate, 0);
405 
406  res = swr_init(aio->swr_context);
407  if (res < 0) {
408  res = AVERROR(ENOMEM);
409  throwIOExceptionIfError(env, res, "Could not initialize swr context");
410  goto bail;
411  }
412 
413  //fprintf(stderr, "init_swr: dither context: %d\n", aio->swr_context->dither);
414  //fprintf(stderr, "init_swr: output sample bits: %d\n", aio->swr_context->dither.output_sample_bits);
415 
416  bail:
417 
418  return res;
419 }
420 
431 AVCodec* ff_find_encoder(enum AVSampleFormat sampleFormat, int bits, int big_endian, int signedSamples) {
432  enum AVCodecID codec_id;
433 
434 #ifdef DEBUG
435  fprintf(stderr, "ff_find_encoder(fmt=%d,bits=%d,big_endian=%d,signed=%d)\n", sampleFormat, bits, big_endian, signedSamples);
436 #endif
437 
438  switch (sampleFormat) {
439  case AV_SAMPLE_FMT_U8:
440  case AV_SAMPLE_FMT_U8P:
441  if (signedSamples) codec_id = AV_CODEC_ID_PCM_S8;
442  else codec_id = AV_CODEC_ID_PCM_U8;
443  break;
444  case AV_SAMPLE_FMT_S16:
445  case AV_SAMPLE_FMT_S16P:
446  if (signedSamples) codec_id = big_endian ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
447  else codec_id = big_endian ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
448  break;
449  case AV_SAMPLE_FMT_S32:
450  case AV_SAMPLE_FMT_S32P:
451  if (bits == 24) {
452  if (signedSamples) codec_id = big_endian ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
453  else codec_id = big_endian ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
454  } else {
455  if (signedSamples) codec_id = big_endian ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
456  else codec_id = big_endian ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
457  }
458  break;
459  case AV_SAMPLE_FMT_FLT:
460  case AV_SAMPLE_FMT_FLTP:
461  codec_id = big_endian ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
462  break;
463  case AV_SAMPLE_FMT_DBL:
464  case AV_SAMPLE_FMT_DBLP:
465  codec_id = big_endian ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
466  break;
467  default:
468  codec_id = -1;
469  }
470 
471  return avcodec_find_encoder(codec_id);
472 }
473 
484 int ff_init_encoder(JNIEnv *env, FFAudioIO *aio, AVCodec *encoder) {
485  int res = 0;
486  int64_t out_sample_rate;
487  int64_t out_channel_count;
488  int64_t out_channel_layout;
489  enum AVSampleFormat out_sample_fmt;
490 
491  // make sure we clean up before resetting this
492  // in case this is called twice
493  if (aio->encode_frame) {
494  av_frame_free(&aio->encode_frame);
495  }
496  if (aio->encode_context) {
497  avcodec_close(aio->encode_context);
498  av_free(aio->encode_context);
499  }
500 
501  aio->encode_context = avcodec_alloc_context3(encoder);
502  if (!aio->encode_context) {
503  res = AVERROR(ENOMEM);
504  throwIOExceptionIfError(env, res, "Could not allocate codec context.");
505  goto bail;
506  }
507 
508  // init to whatever we have in SwrContext
509  av_opt_get_int(aio->swr_context, "out_channel_count", 0, &out_channel_count);
510  av_opt_get_int(aio->swr_context, "out_channel_layout", 0, &out_channel_layout);
511  av_opt_get_int(aio->swr_context, "out_sample_rate", 0, &out_sample_rate);
512  av_opt_get_sample_fmt(aio->swr_context, "out_sample_fmt", 0, &out_sample_fmt);
513 
514  aio->encode_context->sample_fmt = out_sample_fmt;
515  aio->encode_context->sample_rate = out_sample_rate;
516  aio->encode_context->channel_layout = out_channel_layout;
517  aio->encode_context->channels = out_channel_count;
518 
519  res = avcodec_open2(aio->encode_context, encoder, NULL);
520  if (res < 0) {
521  res = AVERROR(ENOMEM);
522  throwIOExceptionIfError(env, res, "Could not open encoder.");
523  goto bail;
524  }
525 
526  aio->encode_frame = av_frame_alloc();
527  if (!aio->encode_frame) {
528  res = AVERROR(ENOMEM);
529  throwIOExceptionIfError(env, res, "Could not allocate encoder frame.");
530  goto bail;
531  }
532  aio->encode_frame->nb_samples = aio->encode_context->frame_size; // this will be changed later!!
533  aio->encode_frame->format = aio->encode_context->sample_fmt;
534  aio->encode_frame->channel_layout = aio->encode_context->channel_layout;
535 
536  bail:
537 
538  return res;
539 }
540 
549 int ff_init_audioio(JNIEnv *env, FFAudioIO *aio) {
550  int res = 0;
551  int nb_planes;
552  AVCodec *codec = NULL;
553 
554  aio->timestamp = 0;
555 
556  // allocate pointer to the audio buffers, i.e. the multiple planes/channels.
557  nb_planes = av_sample_fmt_is_planar(aio->stream->codecpar->format)
558  ? aio->stream->codecpar->channels
559  : 1;
560 
561  // always init SWR to keep code simpler
562  res = init_swr(env, aio);
563  if (res < 0) {
564  // exception is already thrown
565  goto bail;
566  }
567  // if for some reason the codec delivers 24bit, we need to encode its output to little endian
568  if (aio->stream->codecpar->bits_per_coded_sample == 24) {
569  codec = ff_find_encoder(aio->stream->codecpar->format, aio->stream->codecpar->bits_per_coded_sample, ff_big_endian(aio->stream->codecpar->codec_id), 1);
570  if (!codec) {
571  res = AVERROR(EINVAL);
572  throwIOExceptionIfError(env, res, "Could not find suitable encoder codec.");
573  goto bail;
574  }
575  res = ff_init_encoder(env, aio, codec);
576  if (res<0) {
577  throwIOExceptionIfError(env, res, "Could not initialize encoder codec.");
578  goto bail;
579  }
580  }
581 
582  // allocate the buffer the codec decodes to
583  aio->audio_data = av_mallocz(sizeof(uint8_t *) * nb_planes);
584  if (!aio->audio_data) {
585  res = AVERROR(ENOMEM);
586  throwIOExceptionIfError(env, res, "Could not allocate audio data buffers.");
587  goto bail;
588  }
589 
590  aio->decode_frame = av_frame_alloc();
591  if (!aio->decode_frame) {
592  res = AVERROR(ENOMEM);
593  throwIOExceptionIfError(env, res, "Could not allocate frame.");
594  goto bail;
595  }
596 
597  // initialize packet
598  av_init_packet(&(aio->decode_packet));
599  aio->decode_packet.data = NULL;
600  aio->decode_packet.size = 0;
601 
602 bail:
603 
604  return res;
605 }
606 
607 
617 static int encode_buffer(FFAudioIO *aio, const uint8_t *in_buf, int in_size, const uint8_t *out_buf) {
618  int res = 0;
619  int got_output;
620 
621  res = av_samples_get_buffer_size(NULL, aio->encode_context->channels, aio->encode_frame->nb_samples, aio->encode_context->sample_fmt, 1);
622 
623 #ifdef DEBUG
624  fprintf(stderr, "encode_buffer: channels=%d frame->nb_samples=%d in_size=%d\n", aio->encode_context->channels, aio->encode_frame->nb_samples, in_size);
625  fprintf(stderr, "encode_buffer: needed buffer=%d available=%d\n", res, in_size);
626 #endif
627 
628  // setup the data pointers in the AVFrame
629  res = avcodec_fill_audio_frame(aio->encode_frame,
630  aio->encode_context->channels,
631  aio->encode_context->sample_fmt,
632  in_buf,
633  in_size,
634  1);
635  if (res < 0) {
636  throwIOExceptionIfError(aio->env, res, "Failed to fill audio frame.");
637  goto bail;
638  }
639  av_init_packet(&aio->encode_packet);
640  aio->encode_packet.data = NULL; // packet data will be allocated by the encoder
641  aio->encode_packet.size = 0;
642  // encode the samples
643  res = avcodec_encode_audio2(aio->encode_context, &aio->encode_packet, aio->encode_frame, &got_output);
644  if (res < 0) {
645  throwIOExceptionIfError(aio->env, res, "Failed to encode audio frame.");
646  goto bail;
647  }
648  if (got_output) {
649  res = aio->encode_packet.size;
650  memcpy((char*)out_buf, aio->encode_packet.data, aio->encode_packet.size);
651  av_packet_unref(&aio->encode_packet);
652  }
653 
654  bail:
655 
656  return res;
657 }
658 
670 static int resample(FFAudioIO *aio, uint8_t **out_buf, int out_samples, const uint8_t **in_buf, const int in_samples) {
671  int res = 0;
672  int64_t out_channel_count;
673  enum AVSampleFormat out_sample_format;
674 
675  if (out_samples == 0) goto bail; // nothing to do.
676 
677  av_opt_get_int(aio->swr_context, "out_channel_count", 0, &out_channel_count);
678  av_opt_get_sample_fmt(aio->swr_context, "out_sample_fmt", 0, &out_sample_format);
679 
680  #ifdef DEBUG
681  fprintf(stderr, "resample: out_samples=%d in_samples=%d, channels=%d sample_format=%d\n",
682  out_samples, in_samples, (int)out_channel_count, out_sample_format);
683  #endif
684 
685  // allocate temp buffer for resampled data
686  res = av_samples_alloc(out_buf, NULL, out_channel_count, out_samples, out_sample_format, 1);
687  if (res < 0) {
688  res = AVERROR(ENOMEM);
689  throwIOExceptionIfError(aio->env, res, "Could not allocate resample buffer.");
690  goto bail;
691  }
692 
693  // run the SWR conversion (even if it is not strictly necessary)
694  res = swr_convert(aio->swr_context, out_buf, out_samples, in_buf, in_samples);
695  if (res < 0) {
696  throwIOExceptionIfError(aio->env, res, "Failed to convert audio data.");
697  goto bail;
698  }
699 
700  bail:
701 
702  return res;
703 }
704 
705 static int copy_to_java_buffer(FFAudioIO *aio, uint32_t offset, int samples, uint8_t **resample_buf) {
706  int res = 0;
707  uint32_t buffer_size = 0;
708  jobject byte_buffer = NULL;
709  uint8_t *java_buffer = NULL;
710  int64_t channel_count;
711  enum AVSampleFormat format;
712 
713  av_opt_get_sample_fmt(aio->swr_context, "out_sample_fmt", 0, &format);
714  av_opt_get_int(aio->swr_context, "out_channel_count", 0, &channel_count);
715 
716  res = av_samples_get_buffer_size(NULL, (int)channel_count, samples, format, 1);
717  if (res < 0) goto bail;
718  else buffer_size = res;
719 
720  // ensure native buffer capacity
721  if (aio->java_buffer_capacity < buffer_size + offset) {
722  aio->java_buffer_capacity = (*aio->env)->CallIntMethod(aio->env, aio->java_instance, setNativeBufferCapacity_MID, (jint)(buffer_size + offset));
723  }
724  // get java-managed byte buffer reference
725  byte_buffer = (*aio->env)->GetObjectField(aio->env, aio->java_instance, nativeBuffer_FID);
726  if (!byte_buffer) {
727  res = -1;
728  throwIOExceptionIfError(aio->env, 1, "Failed to get native buffer.");
729  goto bail;
730  }
731 
732  // we have some samples, let's copy them to the java buffer, using the desired encoding at the desired offset
733  java_buffer = (uint8_t *)(*aio->env)->GetDirectBufferAddress(aio->env, byte_buffer) + offset;
734  if (!java_buffer) {
735  throwIOExceptionIfError(aio->env, 1, "Failed to get address for native buffer.");
736  goto bail;
737  }
738  if (aio->encode_context) {
739  aio->encode_frame->nb_samples = samples;
740  res = encode_buffer(aio, resample_buf[0], buffer_size, java_buffer);
741  if (res < 0) {
742  buffer_size = 0;
743  goto bail;
744  }
745  buffer_size = res;
746  } else {
747  memcpy(java_buffer, resample_buf[0], buffer_size);
748  }
749  // we already wrote to the buffer, now we still need to
750  // set new bytebuffer limit and position to 0.
751  if (offset == 0) {
752  // only rewind, if this is the first call for this packet
753  (*aio->env)->CallObjectMethod(aio->env, byte_buffer, rewind_MID);
754  }
755  (*aio->env)->CallObjectMethod(aio->env, byte_buffer, limit_MID, offset + buffer_size);
756 
757  aio->resampled_samples += buffer_size;
758 
759  bail:
760 
761  return res;
762 }
763 
772 static int decode_packet(FFAudioIO *aio, int cached) {
773  int res = 0;
774  uint8_t **resample_buf = NULL;
775  uint32_t java_buffer_offset = 0;
776  uint32_t out_buf_size = 0;
777  int out_buf_samples = 0;
778  int64_t out_sample_rate;
779  int flush = aio->got_frame
780  && aio->decode_packet.size == 0
781  && swr_get_delay(aio->swr_context, aio->stream->codecpar->sample_rate);
782  int bytesConsumed = 0;
783 
784  init_ids(aio->env, aio->java_instance);
785 
786  av_opt_get_int(aio->swr_context, "out_sample_rate", 0, &out_sample_rate);
787 
788  resample_buf = av_mallocz(sizeof(uint8_t *) * 1); // one plane!
789 
790  // decode packet, as long as there is still something in there
791  while ((aio->decode_packet.size > 0 && aio->decode_packet.stream_index == aio->stream_index) || flush) {
792  if (flush) {
793 #ifdef DEBUG
794  fprintf(stderr, "Flushing.\n");
795 #endif
796  res = resample(aio, resample_buf, swr_get_delay(aio->swr_context, aio->stream->codecpar->sample_rate), NULL, 0);
797  if (res < 0) goto bail;
798  else out_buf_samples = res;
799  flush = 0; // break while loop
800  } else {
801 
802 #ifdef DEBUG
803  fprintf(stderr, "aio->decode_packet.size: %i\n", aio->decode_packet.size);
804 #endif
805  // decode frame
806  // got_frame indicates whether we got a frame
807  bytesConsumed = avcodec_decode_audio4(aio->decode_context, aio->decode_frame, &aio->got_frame, &aio->decode_packet);
808  if (bytesConsumed < 0) {
809  logWarning(aio, bytesConsumed, "Skipping packet. avcodec_decode_audio4 failed:");
810  aio->decode_packet.size = 0;
811  aio->decode_packet.data = NULL;
812  // pretend we didn't read anything, so we can try our luck with the next packet
813  res = 0;
814  goto bail;
815  }
816 #ifdef DEBUG
817  fprintf(stderr, "bytesConsumed: %i\n", bytesConsumed);
818 #endif
819  // adjust packet size and offset for next avcodec_decode_audio4 call
820  aio->decode_packet.size -= bytesConsumed;
821  aio->decode_packet.data += bytesConsumed;
822 
823  if (aio->got_frame) {
824 
825  aio->decoded_samples += aio->decode_frame->nb_samples;
826  out_buf_samples = aio->decode_frame->nb_samples;
827 #ifdef DEBUG
828  fprintf(stderr, "samples%s n:%" PRIu64 " nb_samples:%d pts:%s\n",
829  cached ? "(cached)" : "",
830  aio->decoded_samples, aio->decode_frame->nb_samples,
831  av_ts2timestr(aio->decode_frame->pts, &aio->decode_context->time_base));
832 #endif
833 
834  // adjust out sample number for a different sample rate
835  // this is an estimate!!
836  out_buf_samples = av_rescale_rnd(
837  swr_get_delay(aio->swr_context, aio->stream->codecpar->sample_rate) + aio->decode_frame->nb_samples,
838  out_sample_rate,
839  aio->stream->codecpar->sample_rate,
840  AV_ROUND_UP
841  );
842 
843  // allocate new aio->audio_data buffers
844  res = av_samples_alloc(aio->audio_data, NULL, aio->decode_frame->channels,
845  aio->decode_frame->nb_samples, aio->decode_frame->format, 1);
846  if (res < 0) {
847  throwIOExceptionIfError(aio->env, res, "Could not allocate audio buffer.");
848  return AVERROR(ENOMEM);
849  }
850  // copy audio data to aio->audio_data
851  av_samples_copy(aio->audio_data, aio->decode_frame->data, 0, 0,
852  aio->decode_frame->nb_samples, aio->decode_frame->channels, aio->decode_frame->format);
853 
854  res = resample(aio, resample_buf, out_buf_samples, (const uint8_t **)aio->audio_data, aio->decode_frame->nb_samples);
855  if (res < 0) goto bail;
856  else out_buf_samples = res;
857 
858  } else {
859 #ifdef DEBUG
860  fprintf(stderr, "Got no frame.\n");
861 #endif
862  }
863  }
864 
865  if (out_buf_samples > 0) {
866  // copy what we have decoded to the java buffer
867  java_buffer_offset += out_buf_size;
868  res = copy_to_java_buffer(aio, java_buffer_offset, out_buf_samples, resample_buf);
869  if (res < 0) goto bail;
870  out_buf_size = res;
871  }
872  if (resample_buf[0]) av_freep(&resample_buf[0]);
873  if (aio->audio_data[0]) av_freep(&aio->audio_data[0]);
874  }
875 
876 
877 bail:
878 
879  if (resample_buf) {
880  if (resample_buf[0]) av_freep(&resample_buf[0]);
881  av_free(resample_buf);
882  }
883  if (aio->audio_data[0]) av_freep(&aio->audio_data[0]);
884 
885  return res;
886 }
887 
888 
898  int res = 0;
899  int read_frame = 0;
900 
901  aio->timestamp += aio->decode_packet.duration;
902 
903  while (res == 0 && read_frame >= 0) {
904  read_frame = av_read_frame(aio->format_context, &aio->decode_packet);
905  if (read_frame >= 0) {
906  res = decode_packet(aio, 0);
907 #ifdef DEBUG
908  fprintf(stderr, "res : %i\n", res);
909  fprintf(stderr, "read_frame: %i\n", read_frame);
910  fprintf(stderr, "duration : %lli\n", aio->decode_packet.duration);
911  fprintf(stderr, "timestamp : %" PRId64 "\n", aio->timestamp);
912  fprintf(stderr, "pts : %" PRId64 "\n", aio->decode_packet.pts);
913  fprintf(stderr, "dts : %" PRId64 "\n", aio->decode_packet.dts);
914 #endif
915  av_packet_unref(&(aio->decode_packet));
916  } else {
917  #ifdef DEBUG
918  fprintf(stderr, "Reading cached frames: %i\n", aio->got_frame);
919  #endif
920  // flush cached frames
921  av_packet_unref(&(aio->decode_packet));
922  res = decode_packet(aio, 1);
923  }
924  }
925 
926  return res;
927 }
928 
929 
934  #ifdef DEBUG
935  fprintf(stderr, "ff_audioio_free\n");
936  #endif
937 
938  if (aio) {
939 
940  if (aio->encode_frame) {
941  av_frame_free(&aio->encode_frame);
942  }
943  if (aio->decode_context) {
944  avcodec_close(aio->decode_context);
945  av_free(aio->decode_context);
946  }
947  if (aio->encode_context) {
948  avcodec_close(aio->encode_context);
949  av_free(aio->encode_context);
950  }
951  if (aio->swr_context) {
952  swr_free(&aio->swr_context);
953  }
954  if (aio->format_context) {
955  AVFormatContext *s = aio->format_context;
956  if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) || (s->flags & AVFMT_FLAG_CUSTOM_IO)) {
957  if (aio->format_context->pb) {
958  avio_flush(aio->format_context->pb);
959  av_free(aio->format_context->pb->buffer);
960  av_free(aio->format_context->pb);
961  }
962  }
963  avformat_close_input(&(aio->format_context));
964  }
965  if (aio->decode_frame) {
966  av_free(aio->decode_frame);
967  }
968  if (aio->audio_data) {
969  av_free(aio->audio_data);
970  }
971  free(aio);
972  }
973 }
974 
981 int ff_big_endian(enum AVCodecID codec_id) {
982  int big_endian = 0;
983  switch (codec_id) {
984  case AV_CODEC_ID_PCM_S16BE:
985  case AV_CODEC_ID_PCM_U16BE:
986  case AV_CODEC_ID_PCM_S32BE:
987  case AV_CODEC_ID_PCM_U32BE:
988  case AV_CODEC_ID_PCM_S24BE:
989  case AV_CODEC_ID_PCM_U24BE:
990  case AV_CODEC_ID_PCM_F32BE:
991  case AV_CODEC_ID_PCM_F64BE:
992  big_endian = 1;
993  break;
994  default:
995  big_endian = 0;
996  }
997  return big_endian;
998 }
999 
1000 
1002 
1003  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S16LE = %#x;\n", AV_CODEC_ID_PCM_S16LE);
1004  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S16BE = %#x;\n", AV_CODEC_ID_PCM_S16BE);
1005  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U16LE = %#x;\n", AV_CODEC_ID_PCM_U16LE);
1006  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U16BE = %#x;\n", AV_CODEC_ID_PCM_U16BE);
1007  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S8 = %#x;\n", AV_CODEC_ID_PCM_S8);
1008  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U8 = %#x;\n", AV_CODEC_ID_PCM_U8);
1009  fprintf(stdout, "private final int AV_CODEC_ID_PCM_MULAW = %#x;\n", AV_CODEC_ID_PCM_MULAW);
1010  fprintf(stdout, "private final int AV_CODEC_ID_PCM_ALAW = %#x;\n", AV_CODEC_ID_PCM_ALAW);
1011  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S32LE = %#x;\n", AV_CODEC_ID_PCM_S32LE);
1012  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S32BE = %#x;\n", AV_CODEC_ID_PCM_S32BE);
1013  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U32LE = %#x;\n", AV_CODEC_ID_PCM_U32LE);
1014  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U32BE = %#x;\n", AV_CODEC_ID_PCM_U32BE);
1015  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S24LE = %#x;\n", AV_CODEC_ID_PCM_S24LE);
1016  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S24BE = %#x;\n", AV_CODEC_ID_PCM_S24BE);
1017  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U24LE = %#x;\n", AV_CODEC_ID_PCM_U24LE);
1018  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U24BE = %#x;\n", AV_CODEC_ID_PCM_U24BE);
1019  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S24DAUD = %#x;\n", AV_CODEC_ID_PCM_S24DAUD);
1020  fprintf(stdout, "private final int AV_CODEC_ID_PCM_ZORK = %#x;\n", AV_CODEC_ID_PCM_ZORK);
1021  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S16LE_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S16LE_PLANAR);
1022  fprintf(stdout, "private final int AV_CODEC_ID_PCM_DVD = %#x;\n", AV_CODEC_ID_PCM_DVD);
1023  fprintf(stdout, "private final int AV_CODEC_ID_PCM_F32BE = %#x;\n", AV_CODEC_ID_PCM_F32BE);
1024  fprintf(stdout, "private final int AV_CODEC_ID_PCM_F32LE = %#x;\n", AV_CODEC_ID_PCM_F32LE);
1025  fprintf(stdout, "private final int AV_CODEC_ID_PCM_F64BE = %#x;\n", AV_CODEC_ID_PCM_F64BE);
1026  fprintf(stdout, "private final int AV_CODEC_ID_PCM_F64LE = %#x;\n", AV_CODEC_ID_PCM_F64LE);
1027  fprintf(stdout, "private final int AV_CODEC_ID_PCM_BLURAY = %#x;\n", AV_CODEC_ID_PCM_BLURAY);
1028  fprintf(stdout, "private final int AV_CODEC_ID_PCM_LXF = %#x;\n", AV_CODEC_ID_PCM_LXF);
1029  fprintf(stdout, "private final int AV_CODEC_ID_S302M = %#x;\n", AV_CODEC_ID_S302M);
1030  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S8_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S8_PLANAR);
1031  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S24LE_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S24LE_PLANAR);
1032  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S32LE_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S32LE_PLANAR);
1033  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S16BE_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S16BE_PLANAR);
1034 
1035  /* various ADPCM codecs */
1036  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_QT = %#x;\n", AV_CODEC_ID_ADPCM_IMA_QT);
1037  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_WAV = %#x;\n", AV_CODEC_ID_ADPCM_IMA_WAV);
1038  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_DK3 = %#x;\n", AV_CODEC_ID_ADPCM_IMA_DK3);
1039  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_DK4 = %#x;\n", AV_CODEC_ID_ADPCM_IMA_DK4);
1040  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_WS = %#x;\n", AV_CODEC_ID_ADPCM_IMA_WS);
1041  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_SMJPEG = %#x;\n", AV_CODEC_ID_ADPCM_IMA_SMJPEG);
1042  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_MS = %#x;\n", AV_CODEC_ID_ADPCM_MS);
1043  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_4XM = %#x;\n", AV_CODEC_ID_ADPCM_4XM);
1044  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_XA = %#x;\n", AV_CODEC_ID_ADPCM_XA);
1045  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_ADX = %#x;\n", AV_CODEC_ID_ADPCM_ADX);
1046  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA = %#x;\n", AV_CODEC_ID_ADPCM_EA);
1047  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_G726 = %#x;\n", AV_CODEC_ID_ADPCM_G726);
1048  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_CT = %#x;\n", AV_CODEC_ID_ADPCM_CT);
1049  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_SWF = %#x;\n", AV_CODEC_ID_ADPCM_SWF);
1050  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_YAMAHA = %#x;\n", AV_CODEC_ID_ADPCM_YAMAHA);
1051  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_SBPRO_4 = %#x;\n", AV_CODEC_ID_ADPCM_SBPRO_4);
1052  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_SBPRO_3 = %#x;\n", AV_CODEC_ID_ADPCM_SBPRO_3);
1053  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_SBPRO_2 = %#x;\n", AV_CODEC_ID_ADPCM_SBPRO_2);
1054  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_THP = %#x;\n", AV_CODEC_ID_ADPCM_THP);
1055  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_AMV = %#x;\n", AV_CODEC_ID_ADPCM_IMA_AMV);
1056  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_R1 = %#x;\n", AV_CODEC_ID_ADPCM_EA_R1);
1057  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_R3 = %#x;\n", AV_CODEC_ID_ADPCM_EA_R3);
1058  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_R2 = %#x;\n", AV_CODEC_ID_ADPCM_EA_R2);
1059  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_EA_SEAD = %#x;\n", AV_CODEC_ID_ADPCM_IMA_EA_SEAD);
1060  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_EA_EACS = %#x;\n", AV_CODEC_ID_ADPCM_IMA_EA_EACS);
1061  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_XAS = %#x;\n", AV_CODEC_ID_ADPCM_EA_XAS);
1062  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_MAXIS_XA = %#x;\n", AV_CODEC_ID_ADPCM_EA_MAXIS_XA);
1063  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_ISS = %#x;\n", AV_CODEC_ID_ADPCM_IMA_ISS);
1064  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_G722 = %#x;\n", AV_CODEC_ID_ADPCM_G722);
1065  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_APC = %#x;\n", AV_CODEC_ID_ADPCM_IMA_APC);
1066  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_VIMA = %#x;\n", AV_CODEC_ID_ADPCM_VIMA);
1067  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_AFC = %#x;\n", AV_CODEC_ID_ADPCM_AFC);
1068  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_OKI = %#x;\n", AV_CODEC_ID_ADPCM_IMA_OKI);
1069 
1070  /* AMR */
1071  fprintf(stdout, "private final int AV_CODEC_ID_AMR_NB = %#x;\n", AV_CODEC_ID_AMR_NB);
1072  fprintf(stdout, "private final int AV_CODEC_ID_AMR_WB = %#x;\n", AV_CODEC_ID_AMR_WB);
1073 
1074  /* RealAudio codecs*/
1075  fprintf(stdout, "private final int AV_CODEC_ID_RA_144 = %#x;\n", AV_CODEC_ID_RA_144);
1076  fprintf(stdout, "private final int AV_CODEC_ID_RA_288 = %#x;\n", AV_CODEC_ID_RA_288);
1077 
1078  /* various DPCM codecs */
1079  fprintf(stdout, "private final int AV_CODEC_ID_ROQ_DPCM = %#x;\n", AV_CODEC_ID_ROQ_DPCM);
1080  fprintf(stdout, "private final int AV_CODEC_ID_INTERPLAY_DPCM = %#x;\n", AV_CODEC_ID_INTERPLAY_DPCM);
1081  fprintf(stdout, "private final int AV_CODEC_ID_XAN_DPCM = %#x;\n", AV_CODEC_ID_XAN_DPCM);
1082  fprintf(stdout, "private final int AV_CODEC_ID_SOL_DPCM = %#x;\n", AV_CODEC_ID_SOL_DPCM);
1083 
1084  /* audio codecs */
1085  fprintf(stdout, "private final int AV_CODEC_ID_MP2 = %#x;\n", AV_CODEC_ID_MP2);
1086  fprintf(stdout, "private final int AV_CODEC_ID_MP3 = %#x;\n", AV_CODEC_ID_MP3);
1087  fprintf(stdout, "private final int AV_CODEC_ID_AAC = %#x;\n", AV_CODEC_ID_AAC);
1088  fprintf(stdout, "private final int AV_CODEC_ID_AC3 = %#x;\n", AV_CODEC_ID_AC3);
1089  fprintf(stdout, "private final int AV_CODEC_ID_DTS = %#x;\n", AV_CODEC_ID_DTS);
1090  fprintf(stdout, "private final int AV_CODEC_ID_VORBIS = %#x;\n", AV_CODEC_ID_VORBIS);
1091  fprintf(stdout, "private final int AV_CODEC_ID_DVAUDIO = %#x;\n", AV_CODEC_ID_DVAUDIO);
1092  fprintf(stdout, "private final int AV_CODEC_ID_WMAV1 = %#x;\n", AV_CODEC_ID_WMAV1);
1093  fprintf(stdout, "private final int AV_CODEC_ID_WMAV2 = %#x;\n", AV_CODEC_ID_WMAV2);
1094  fprintf(stdout, "private final int AV_CODEC_ID_MACE3 = %#x;\n", AV_CODEC_ID_MACE3);
1095  fprintf(stdout, "private final int AV_CODEC_ID_MACE6 = %#x;\n", AV_CODEC_ID_MACE6);
1096  fprintf(stdout, "private final int AV_CODEC_ID_VMDAUDIO = %#x;\n", AV_CODEC_ID_VMDAUDIO);
1097  fprintf(stdout, "private final int AV_CODEC_ID_FLAC = %#x;\n", AV_CODEC_ID_FLAC);
1098  fprintf(stdout, "private final int AV_CODEC_ID_MP3ADU = %#x;\n", AV_CODEC_ID_MP3ADU);
1099  fprintf(stdout, "private final int AV_CODEC_ID_MP3ON4 = %#x;\n", AV_CODEC_ID_MP3ON4);
1100  fprintf(stdout, "private final int AV_CODEC_ID_SHORTEN = %#x;\n", AV_CODEC_ID_SHORTEN);
1101  fprintf(stdout, "private final int AV_CODEC_ID_ALAC = %#x;\n", AV_CODEC_ID_ALAC);
1102  fprintf(stdout, "private final int AV_CODEC_ID_WESTWOOD_SND1 = %#x;\n", AV_CODEC_ID_WESTWOOD_SND1);
1103  fprintf(stdout, "private final int AV_CODEC_ID_GSM = %#x;\n", AV_CODEC_ID_GSM);
1104  fprintf(stdout, "private final int AV_CODEC_ID_QDM2 = %#x;\n", AV_CODEC_ID_QDM2);
1105  fprintf(stdout, "private final int AV_CODEC_ID_COOK = %#x;\n", AV_CODEC_ID_COOK);
1106  fprintf(stdout, "private final int AV_CODEC_ID_TRUESPEECH = %#x;\n", AV_CODEC_ID_TRUESPEECH);
1107  fprintf(stdout, "private final int AV_CODEC_ID_TTA = %#x;\n", AV_CODEC_ID_TTA);
1108  fprintf(stdout, "private final int AV_CODEC_ID_SMACKAUDIO = %#x;\n", AV_CODEC_ID_SMACKAUDIO);
1109  fprintf(stdout, "private final int AV_CODEC_ID_QCELP = %#x;\n", AV_CODEC_ID_QCELP);
1110  fprintf(stdout, "private final int AV_CODEC_ID_WAVPACK = %#x;\n", AV_CODEC_ID_WAVPACK);
1111  fprintf(stdout, "private final int AV_CODEC_ID_DSICINAUDIO = %#x;\n", AV_CODEC_ID_DSICINAUDIO);
1112  fprintf(stdout, "private final int AV_CODEC_ID_IMC = %#x;\n", AV_CODEC_ID_IMC);
1113  fprintf(stdout, "private final int AV_CODEC_ID_MUSEPACK7 = %#x;\n", AV_CODEC_ID_MUSEPACK7);
1114  fprintf(stdout, "private final int AV_CODEC_ID_MLP = %#x;\n", AV_CODEC_ID_MLP);
1115  fprintf(stdout, "private final int AV_CODEC_ID_GSM_MS = %#x;\n", AV_CODEC_ID_GSM_MS); /* as found in WAV */
1116  fprintf(stdout, "private final int AV_CODEC_ID_ATRAC3 = %#x;\n", AV_CODEC_ID_ATRAC3);
1117  //fprintf(stdout, "private final int AV_CODEC_ID_VOXWARE = %#x;\n", AV_CODEC_ID_VOXWARE);
1118  fprintf(stdout, "private final int AV_CODEC_ID_APE = %#x;\n", AV_CODEC_ID_APE);
1119  fprintf(stdout, "private final int AV_CODEC_ID_NELLYMOSER = %#x;\n", AV_CODEC_ID_NELLYMOSER);
1120  fprintf(stdout, "private final int AV_CODEC_ID_MUSEPACK8 = %#x;\n", AV_CODEC_ID_MUSEPACK8);
1121  fprintf(stdout, "private final int AV_CODEC_ID_SPEEX = %#x;\n", AV_CODEC_ID_SPEEX);
1122  fprintf(stdout, "private final int AV_CODEC_ID_WMAVOICE = %#x;\n", AV_CODEC_ID_WMAVOICE);
1123  fprintf(stdout, "private final int AV_CODEC_ID_WMAPRO = %#x;\n", AV_CODEC_ID_WMAPRO);
1124  fprintf(stdout, "private final int AV_CODEC_ID_WMALOSSLESS = %#x;\n", AV_CODEC_ID_WMALOSSLESS);
1125  fprintf(stdout, "private final int AV_CODEC_ID_ATRAC3P = %#x;\n", AV_CODEC_ID_ATRAC3P);
1126  fprintf(stdout, "private final int AV_CODEC_ID_EAC3 = %#x;\n", AV_CODEC_ID_EAC3);
1127  fprintf(stdout, "private final int AV_CODEC_ID_SIPR = %#x;\n", AV_CODEC_ID_SIPR);
1128  fprintf(stdout, "private final int AV_CODEC_ID_MP1 = %#x;\n", AV_CODEC_ID_MP1);
1129  fprintf(stdout, "private final int AV_CODEC_ID_TWINVQ = %#x;\n", AV_CODEC_ID_TWINVQ);
1130  fprintf(stdout, "private final int AV_CODEC_ID_TRUEHD = %#x;\n", AV_CODEC_ID_TRUEHD);
1131  fprintf(stdout, "private final int AV_CODEC_ID_MP4ALS = %#x;\n", AV_CODEC_ID_MP4ALS);
1132  fprintf(stdout, "private final int AV_CODEC_ID_ATRAC1 = %#x;\n", AV_CODEC_ID_ATRAC1);
1133  fprintf(stdout, "private final int AV_CODEC_ID_BINKAUDIO_RDFT = %#x;\n", AV_CODEC_ID_BINKAUDIO_RDFT);
1134  fprintf(stdout, "private final int AV_CODEC_ID_BINKAUDIO_DCT = %#x;\n", AV_CODEC_ID_BINKAUDIO_DCT);
1135  fprintf(stdout, "private final int AV_CODEC_ID_AAC_LATM = %#x;\n", AV_CODEC_ID_AAC_LATM);
1136  fprintf(stdout, "private final int AV_CODEC_ID_QDMC = %#x;\n", AV_CODEC_ID_QDMC);
1137  fprintf(stdout, "private final int AV_CODEC_ID_CELT = %#x;\n", AV_CODEC_ID_CELT);
1138  fprintf(stdout, "private final int AV_CODEC_ID_G723_1 = %#x;\n", AV_CODEC_ID_G723_1);
1139  fprintf(stdout, "private final int AV_CODEC_ID_G729 = %#x;\n", AV_CODEC_ID_G729);
1140  fprintf(stdout, "private final int AV_CODEC_ID_8SVX_EXP = %#x;\n", AV_CODEC_ID_8SVX_EXP);
1141  fprintf(stdout, "private final int AV_CODEC_ID_8SVX_FIB = %#x;\n", AV_CODEC_ID_8SVX_FIB);
1142  fprintf(stdout, "private final int AV_CODEC_ID_BMV_AUDIO = %#x;\n", AV_CODEC_ID_BMV_AUDIO);
1143  fprintf(stdout, "private final int AV_CODEC_ID_RALF = %#x;\n", AV_CODEC_ID_RALF);
1144  fprintf(stdout, "private final int AV_CODEC_ID_IAC = %#x;\n", AV_CODEC_ID_IAC);
1145  fprintf(stdout, "private final int AV_CODEC_ID_ILBC = %#x;\n", AV_CODEC_ID_ILBC);
1146  fprintf(stdout, "private final int AV_CODEC_ID_COMFORT_NOISE = %#x;\n", AV_CODEC_ID_COMFORT_NOISE);
1147  fprintf(stdout, "private final int AV_CODEC_ID_FFWAVESYNTH = %#x;\n", AV_CODEC_ID_FFWAVESYNTH);
1148  // apparently not existent in FFmpeg 2:
1149  //fprintf(stdout, "private final int AV_CODEC_ID_8SVX_RAW = %#x;\n", AV_CODEC_ID_8SVX_RAW);
1150  fprintf(stdout, "private final int AV_CODEC_ID_SONIC = %#x;\n", AV_CODEC_ID_SONIC);
1151  fprintf(stdout, "private final int AV_CODEC_ID_SONIC_LS = %#x;\n", AV_CODEC_ID_SONIC_LS);
1152  fprintf(stdout, "private final int AV_CODEC_ID_PAF_AUDIO = %#x;\n", AV_CODEC_ID_PAF_AUDIO);
1153  fprintf(stdout, "private final int AV_CODEC_ID_OPUS = %#x;\n", AV_CODEC_ID_OPUS);
1154  fprintf(stdout, "private final int AV_CODEC_ID_TAK = %#x;\n", AV_CODEC_ID_TAK);
1155  fprintf(stdout, "private final int AV_CODEC_ID_EVRC = %#x;\n", AV_CODEC_ID_EVRC);
1156  fprintf(stdout, "private final int AV_CODEC_ID_SMV = %#x;\n", AV_CODEC_ID_SMV);
1157 
1158 }
1159 
1163 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
1164  //av_register_all();
1165  avformat_network_init();
1166  //avcodec_register_all();
1167  return JNI_VERSION_1_6;
1168 }
void logFine(FFAudioIO *aio, int err, const char *message)
Log a debug message.
Definition: FFUtils.c:235
void logWarning(FFAudioIO *aio, int err, const char *message)
Log a warning.
Definition: FFUtils.c:220
int stream_index
Index of the audio stream we are using.
Definition: FFUtils.h:65
void throwIOExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an IOException.
Definition: FFUtils.c:192
AVFormatContext * format_context
Current AVFormatContext.
Definition: FFUtils.h:63
AVStream * stream
Audio stream we are interested in.
Definition: FFUtils.h:64
uint8_t ** audio_data
Audio data (accommodates multiple planes)
Definition: FFUtils.h:69
uint64_t decoded_samples
Running count of decoded samples.
Definition: FFUtils.h:71
void throwFileNotFoundExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an IllegalArgumentException.
Definition: FFUtils.c:207
int ff_open_stream(JNIEnv *env, AVStream *stream, AVCodecContext **context)
Opens the given stream, i.e.
Definition: FFUtils.c:65
int ff_big_endian(enum AVCodecID codec_id)
Indicates whether the given id belongs to a big endian codec.
Definition: FFUtils.c:981
jint java_buffer_capacity
Current capacity of the Java nativeBuffer.
Definition: FFUtils.h:60
void dumpCodecIds()
Definition: FFUtils.c:1001
void throwUnsupportedAudioFileExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an UnsupportedAudioFileException.
Definition: FFUtils.c:162
int ff_open_file(JNIEnv *env, AVFormatContext **format_context, AVStream **openedStream, AVCodecContext **context, int *stream_index, const char *url)
Opens the input file/url, allocates a AVFormatContext for it and opens the audio stream with an appro...
Definition: FFUtils.c:312
int ff_fill_buffer(FFAudioIO *aio)
Reads a frame via av_read_frame(AVFormatContext, AVPacket), decodes it to a AVPacket, and writes the result to the Java-side nativeBuffer.
Definition: FFUtils.c:897
int ff_init_audioio(JNIEnv *env, FFAudioIO *aio)
Initialize our main context FFAudioIO, so that SwrContext, decode buffers and the encoder are set to ...
Definition: FFUtils.c:549
int got_frame
Indicates whether we got a frame in the last call to avcodec_decode_audio4.
Definition: FFUtils.h:70
int ff_init_encoder(JNIEnv *env, FFAudioIO *aio, AVCodec *encoder)
Allocates and initializes the encoder context and frame in FFAudioIO.
Definition: FFUtils.c:484
AVFrame * encode_frame
AVFrame for encoding.
Definition: FFUtils.h:81
int ff_open_format_context(JNIEnv *env, AVFormatContext **format_context, const char *url)
Opens the input file/url and allocates a AVFormatContext for it, but does not open the audio stream w...
Definition: FFUtils.c:256
SwrContext * swr_context
Resampling context.
Definition: FFUtils.h:75
AVPacket decode_packet
AVPacket for decoding.
Definition: FFUtils.h:67
uint64_t resampled_samples
Count of resampled samples.
Definition: FFUtils.h:76
JNIEnv * env
JNI environment.
Definition: FFUtils.h:58
AVPacket encode_packet
AVPacket for encoding.
Definition: FFUtils.h:80
AVCodec * ff_find_encoder(enum AVSampleFormat sampleFormat, int bits, int big_endian, int signedSamples)
Finds an AVCodec encoder for the given sample format, bits per sample, byte order and signed/unsigned...
Definition: FFUtils.c:431
void throwIndexOutOfBoundsExceptionIfError(JNIEnv *env, int err, int index)
Throws an IndexOutOfBoundsException.
Definition: FFUtils.c:177
AVCodecContext * decode_context
Codec context for decoding.
Definition: FFUtils.h:66
AVCodecContext * encode_context
Codec context for encoding.
Definition: FFUtils.h:79
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
Make sure FFmpeg is initialized all the way.
Definition: FFUtils.c:1163
Central context representing the native peer to the Java FFNativePeerInputStream object.
Definition: FFUtils.h:56
AVFrame * decode_frame
AVFrame for decoding.
Definition: FFUtils.h:68
void ff_audioio_free(FFAudioIO *aio)
Free all resources held by aio and then itself.
Definition: FFUtils.c:933
uint64_t timestamp
Current timestamp (in samples, not seconds)
Definition: FFUtils.h:72
jobject java_instance
Calling Java instance.
Definition: FFUtils.h:59