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 int MIN_PROBE_SCORE = 5; // this is fairly arbitrary, but we need to give other javax.sound.sampled impls a chance
32 
39 static void init_ids(JNIEnv *env, jobject stream) {
40  if (!nativeBuffer_FID || !rewind_MID || !limit_MID) {
41  jclass bufferClass = NULL;
42  jclass streamClass = NULL;
43 
44  bufferClass = (*env)->FindClass(env, "java/nio/Buffer");
45  streamClass = (*env)->GetObjectClass(env, stream);
46 
47  nativeBuffer_FID = (*env)->GetFieldID(env, streamClass, "nativeBuffer", "Ljava/nio/ByteBuffer;");
48  rewind_MID = (*env)->GetMethodID(env, bufferClass, "rewind", "()Ljava/nio/Buffer;");
49  limit_MID = (*env)->GetMethodID(env, bufferClass, "limit", "(I)Ljava/nio/Buffer;");
50  capacity_MID = (*env)->GetMethodID(env, bufferClass, "capacity", "()I");
51  setNativeBufferCapacity_MID = (*env)->GetMethodID(env, streamClass, "setNativeBufferCapacity", "(I)I");
52  }
53 }
54 
61 int ff_open_stream(JNIEnv *env, AVStream *stream, AVCodecContext **context) {
62 #ifdef DEBUG
63  fprintf(stderr, "Opening stream...\n");
64 #endif
65 
66  int res = 0;
67  AVCodec *decoder = NULL;
68  AVDictionary *opts = NULL;
69  int refcount = 0; // is this correct?
70 
71  decoder = avcodec_find_decoder(stream->codecpar->codec_id);
72  if (!decoder) {
73  fprintf(stderr, "Failed to find %s codec\n", av_get_media_type_string(AVMEDIA_TYPE_AUDIO));
74  res = AVERROR(EINVAL);
75  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to find codec.");
76  goto bail;
77  }
78 
79  *context = avcodec_alloc_context3(decoder);
80  if (!context) {
81  fprintf(stderr, "Failed to allocate context\n");
82  res = AVERROR(EINVAL);
83  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to allocate codec context.");
84  goto bail;
85  }
86 
87  /* Copy codec parameters from input stream to output codec context */
88  if ((res = avcodec_parameters_to_context(*context, stream->codecpar)) < 0) {
89  fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n", av_get_media_type_string(AVMEDIA_TYPE_AUDIO));
90  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to copy codec parameters.");
91  goto bail;
92  }
93 
94  /* Init the decoders, with or without reference counting */
95  av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0);
96  if ((res = avcodec_open2(*context, decoder, &opts)) < 0) {
97  fprintf(stderr, "Failed to open %s codec\n", av_get_media_type_string(AVMEDIA_TYPE_AUDIO));
98  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to open codec.");
99  goto bail;
100  }
101 
102 #ifdef DEBUG
103  fprintf(stderr, "Stream was opened.\n");
104 #endif
105  return res;
106 
107 bail:
108  return res;
109 }
110 
118 static int open_codec_context(int *stream_index,
119  AVFormatContext *format_context,
120  AVCodecContext *context,
121  enum AVMediaType type) {
122 
123  int res = 0;
124  AVStream *stream;
125  AVCodec *decoder = NULL;
126 
127  res = av_find_best_stream(format_context, type, -1, -1, NULL, 0);
128  if (res < 0) {
129  goto bail;
130  }
131  *stream_index = res;
132  stream = format_context->streams[*stream_index];
133 
134  // find decoder for the stream
135  decoder = avcodec_find_decoder(stream->codecpar->codec_id);
136  if (!decoder) {
137  goto bail;
138  }
139  context = avcodec_alloc_context3(decoder);
140  if (!context) {
141  goto bail;
142  }
143 
144  res = avcodec_open2(context, decoder, NULL);
145  if (res < 0) {
146  goto bail;
147  }
148  return res;
149 
150 bail:
151  return res;
152 }
153 
154 
158 void throwUnsupportedAudioFileExceptionIfError(JNIEnv *env, int err, const char * message) {
159  if (err) {
160  char formattedMessage [strlen(message)+4+AV_ERROR_MAX_STRING_SIZE];
161  snprintf(formattedMessage, strlen(message)+4+AV_ERROR_MAX_STRING_SIZE, "%s (%.64s)", message, av_err2str(err));
162 #ifdef DEBUG
163  fprintf(stderr, "UnsupportedAudioFileException: %s\n", formattedMessage);
164 #endif
165  jclass excCls = (*env)->FindClass(env, "javax/sound/sampled/UnsupportedAudioFileException");
166  (*env)->ThrowNew(env, excCls, formattedMessage);
167  }
168 }
169 
173 void throwIndexOutOfBoundsExceptionIfError(JNIEnv *env, int err, int index) {
174  if (err) {
175  char formattedMessage [15];
176  snprintf(formattedMessage, 15, "%d", index);
177 #ifdef DEBUG
178  fprintf(stderr, "IndexOutOfBoundsException: %d\n", index);
179 #endif
180  jclass excCls = (*env)->FindClass(env, "java/lang/IndexOutOfBoundsException");
181  (*env)->ThrowNew(env, excCls, formattedMessage);
182  }
183 }
184 
188 void throwIOExceptionIfError(JNIEnv *env, int err, const char *message) {
189  if (err) {
190  char formattedMessage [strlen(message)+4+AV_ERROR_MAX_STRING_SIZE];
191  snprintf(formattedMessage, strlen(message)+4+AV_ERROR_MAX_STRING_SIZE, "%s (%.64s)", message, av_err2str(err));
192 #ifdef DEBUG
193  fprintf(stderr, "IOException: %s\n", formattedMessage);
194 #endif
195  jclass excCls = (*env)->FindClass(env, "java/io/IOException");
196  (*env)->ThrowNew(env, excCls, formattedMessage);
197  }
198 }
199 
203 void throwFileNotFoundExceptionIfError(JNIEnv *env, int err, const char *message) {
204  if (err) {
205 #ifdef DEBUG
206  fprintf (stderr, "FileNotFoundException: '%s' %d (%4.4s)\n", message, (int)err, (char*)&err);
207 #endif
208  jclass excCls = (*env)->FindClass(env, "java/io/FileNotFoundException");
209  (*env)->ThrowNew(env, excCls, message);
210  }
211 }
212 
213 
223 int ff_open_format_context(JNIEnv *env, AVFormatContext **format_context, const char *url) {
224  int res = 0;
225  int probe_score = 0;
226 
227  res = avformat_open_input(format_context, url, NULL, NULL);
228  if (res) {
229  if (res == AVERROR(ENOENT) || res == AVERROR_HTTP_NOT_FOUND) {
230  throwFileNotFoundExceptionIfError(env, res, url);
231  } else if (res == AVERROR_PROTOCOL_NOT_FOUND
232  || res == AVERROR_HTTP_BAD_REQUEST
233  || res == AVERROR_HTTP_UNAUTHORIZED
234  || res == AVERROR_HTTP_FORBIDDEN
235  || res == AVERROR_HTTP_OTHER_4XX
236  || res == AVERROR_HTTP_SERVER_ERROR
237  || res == AVERROR(EIO)) {
238  throwIOExceptionIfError(env, res, url);
239  } else {
240  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to open audio file");
241  }
242  goto bail;
243  }
244  probe_score = (*format_context)->probe_score;
245 
246  #ifdef DEBUG
247  fprintf(stderr, "ff_open_format_context(): probe score=%i\n", probe_score);
248  #endif
249 
250  if (probe_score < MIN_PROBE_SCORE) {
251  res = probe_score;
252  throwUnsupportedAudioFileExceptionIfError(env, probe_score, "Probe score too low");
253  goto bail;
254  }
255 
256  res = avformat_find_stream_info(*format_context, NULL);
257  if (res < 0) {
258  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to find stream info");
259  goto bail;
260  }
261 
262 bail:
263 
264  return res;
265 }
266 
279 int ff_open_file(JNIEnv *env, AVFormatContext **format_context, AVStream **openedStream, AVCodecContext **context, int *stream_index, const char *url) {
280  int res = 0;
281  res = ff_open_format_context(env, format_context, url);
282  if (res) {
283  // exception has already been thrown
284  goto bail;
285  }
286 
287 #ifdef DEBUG
288  fprintf(stderr, "Desired audio stream index: %i.\n", *stream_index);
289 #endif
290 
291  if (*stream_index < 0) {
292  // use best audio stream
293  res = open_codec_context(stream_index, *format_context, *context, AVMEDIA_TYPE_AUDIO);
294  if (res) {
295  throwUnsupportedAudioFileExceptionIfError(env, res, "Failed to open codec context.");
296  goto bail;
297  }
298  *openedStream = (*format_context)->streams[*stream_index];
299  } else {
300  // find xth audio stream
301  // count possible audio streams
302  int i;
303  int audio_stream_number = 0;
304  AVStream* stream = NULL;
305 
306  AVFormatContext* deref_format_context = *format_context;
307  for (i=0; i<deref_format_context->nb_streams; i++) {
308  stream = deref_format_context->streams[i];
309  if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
310  if (audio_stream_number == *stream_index) {
311  *stream_index = i;
312  #ifdef DEBUG
313  fprintf(stderr, "Found desired audio stream at index: %i.\n", i);
314  #endif
315  break;
316  }
317  audio_stream_number++;
318  }
319  stream = NULL;
320  }
321  if (stream == NULL) {
322  // we didn't find a stream with the given index
323  res = -1;
324  throwIndexOutOfBoundsExceptionIfError(env, res, *stream_index);
325  goto bail;
326  }
327  res = ff_open_stream(env, stream, context);
328  if (res) {
329  goto bail;
330  }
331  *openedStream = stream;
332  }
333 
334 #ifdef DEBUG
335  fprintf(stderr, "Opened stream index: %i.\n", *stream_index);
336  fprintf(stderr, "Opened stream: %ld.\n", (long) *openedStream);
337 #endif
338 
339 bail:
340 
341  return res;
342 }
343 
351 static int init_swr(JNIEnv *env, FFAudioIO *aio) {
352  int res = 0;
353 
354  aio->swr_context = swr_alloc();
355  if (!aio->swr_context) {
356  res = AVERROR(ENOMEM);
357  throwIOExceptionIfError(env, res, "Could not allocate swr context.");
358  goto bail;
359  }
360 
361  av_opt_set_sample_fmt(aio->swr_context, "in_sample_fmt", aio->stream->codecpar->format, 0);
362  // make sure we get interleaved/packed output
363  av_opt_set_sample_fmt(aio->swr_context, "out_sample_fmt", av_get_packed_sample_fmt(aio->stream->codecpar->format), 0);
364 
365  // keep everything else the way it was...
366  av_opt_set_int(aio->swr_context, "in_channel_count", aio->stream->codecpar->channels, 0);
367  av_opt_set_int(aio->swr_context, "out_channel_count", aio->stream->codecpar->channels, 0);
368  av_opt_set_int(aio->swr_context, "in_channel_layout", aio->stream->codecpar->channel_layout, 0);
369  av_opt_set_int(aio->swr_context, "out_channel_layout", aio->stream->codecpar->channel_layout, 0);
370  av_opt_set_int(aio->swr_context, "in_sample_rate", aio->stream->codecpar->sample_rate, 0);
371  av_opt_set_int(aio->swr_context, "out_sample_rate", aio->stream->codecpar->sample_rate, 0);
372 
373  res = swr_init(aio->swr_context);
374  if (res < 0) {
375  res = AVERROR(ENOMEM);
376  throwIOExceptionIfError(env, res, "Could not initialize swr context");
377  goto bail;
378  }
379 
380  //fprintf(stderr, "init_swr: dither context: %d\n", aio->swr_context->dither);
381  //fprintf(stderr, "init_swr: output sample bits: %d\n", aio->swr_context->dither.output_sample_bits);
382 
383  bail:
384 
385  return res;
386 }
387 
398 AVCodec* ff_find_encoder(enum AVSampleFormat sampleFormat, int bits, int big_endian, int signedSamples) {
399  enum AVCodecID codec_id;
400 
401 #ifdef DEBUG
402  fprintf(stderr, "ff_find_encoder(fmt=%d,bits=%d,big_endian=%d,signed=%d)\n", sampleFormat, bits, big_endian, signedSamples);
403 #endif
404 
405  switch (sampleFormat) {
406  case AV_SAMPLE_FMT_U8:
407  case AV_SAMPLE_FMT_U8P:
408  if (signedSamples) codec_id = AV_CODEC_ID_PCM_S8;
409  else codec_id = AV_CODEC_ID_PCM_U8;
410  break;
411  case AV_SAMPLE_FMT_S16:
412  case AV_SAMPLE_FMT_S16P:
413  if (signedSamples) codec_id = big_endian ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
414  else codec_id = big_endian ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
415  break;
416  case AV_SAMPLE_FMT_S32:
417  case AV_SAMPLE_FMT_S32P:
418  if (bits == 24) {
419  if (signedSamples) codec_id = big_endian ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
420  else codec_id = big_endian ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
421  } else {
422  if (signedSamples) codec_id = big_endian ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
423  else codec_id = big_endian ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
424  }
425  break;
426  case AV_SAMPLE_FMT_FLT:
427  case AV_SAMPLE_FMT_FLTP:
428  codec_id = big_endian ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
429  break;
430  case AV_SAMPLE_FMT_DBL:
431  case AV_SAMPLE_FMT_DBLP:
432  codec_id = big_endian ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
433  break;
434  default:
435  codec_id = -1;
436  }
437 
438  return avcodec_find_encoder(codec_id);
439 }
440 
451 int ff_init_encoder(JNIEnv *env, FFAudioIO *aio, AVCodec *encoder) {
452  int res = 0;
453  int64_t out_sample_rate;
454  int64_t out_channel_count;
455  int64_t out_channel_layout;
456  enum AVSampleFormat out_sample_fmt;
457 
458  // make sure we clean up before resetting this
459  // in case this is called twice
460  if (aio->encode_frame) {
461  av_frame_free(&aio->encode_frame);
462  }
463  if (aio->encode_context) {
464  avcodec_close(aio->encode_context);
465  av_free(aio->encode_context);
466  }
467 
468  aio->encode_context = avcodec_alloc_context3(encoder);
469  if (!aio->encode_context) {
470  res = AVERROR(ENOMEM);
471  throwIOExceptionIfError(env, res, "Could not allocate codec context.");
472  goto bail;
473  }
474 
475  // init to whatever we have in SwrContext
476  av_opt_get_int(aio->swr_context, "out_channel_count", 0, &out_channel_count);
477  av_opt_get_int(aio->swr_context, "out_channel_layout", 0, &out_channel_layout);
478  av_opt_get_int(aio->swr_context, "out_sample_rate", 0, &out_sample_rate);
479  av_opt_get_sample_fmt(aio->swr_context, "out_sample_fmt", 0, &out_sample_fmt);
480 
481  aio->encode_context->sample_fmt = out_sample_fmt;
482  aio->encode_context->sample_rate = out_sample_rate;
483  aio->encode_context->channel_layout = out_channel_layout;
484  aio->encode_context->channels = out_channel_count;
485 
486  res = avcodec_open2(aio->encode_context, encoder, NULL);
487  if (res < 0) {
488  res = AVERROR(ENOMEM);
489  throwIOExceptionIfError(env, res, "Could not open encoder.");
490  goto bail;
491  }
492 
493  aio->encode_frame = av_frame_alloc();
494  if (!aio->encode_frame) {
495  res = AVERROR(ENOMEM);
496  throwIOExceptionIfError(env, res, "Could not allocate encoder frame.");
497  goto bail;
498  }
499  aio->encode_frame->nb_samples = aio->encode_context->frame_size; // this will be changed later!!
500  aio->encode_frame->format = aio->encode_context->sample_fmt;
501  aio->encode_frame->channel_layout = aio->encode_context->channel_layout;
502 
503  bail:
504 
505  return res;
506 }
507 
516 int ff_init_audioio(JNIEnv *env, FFAudioIO *aio) {
517  int res = 0;
518  int nb_planes;
519  AVCodec *codec = NULL;
520 
521  aio->timestamp = 0;
522 
523  // allocate pointer to the audio buffers, i.e. the multiple planes/channels.
524  nb_planes = av_sample_fmt_is_planar(aio->stream->codecpar->format)
525  ? aio->stream->codecpar->channels
526  : 1;
527 
528  // always init SWR to keep code simpler
529  res = init_swr(env, aio);
530  if (res < 0) {
531  // exception is already thrown
532  goto bail;
533  }
534  // if for some reason the codec delivers 24bit, we need to encode its output to little endian
535  if (aio->stream->codecpar->bits_per_coded_sample == 24) {
536  codec = ff_find_encoder(aio->stream->codecpar->format, aio->stream->codecpar->bits_per_coded_sample, ff_big_endian(aio->stream->codecpar->codec_id), 1);
537  if (!codec) {
538  res = AVERROR(EINVAL);
539  throwIOExceptionIfError(env, res, "Could not find suitable encoder codec.");
540  goto bail;
541  }
542  res = ff_init_encoder(env, aio, codec);
543  if (res<0) {
544  throwIOExceptionIfError(env, res, "Could not initialize encoder codec.");
545  goto bail;
546  }
547  }
548 
549  // allocate the buffer the codec decodes to
550  aio->audio_data = av_mallocz(sizeof(uint8_t *) * nb_planes);
551  if (!aio->audio_data) {
552  res = AVERROR(ENOMEM);
553  throwIOExceptionIfError(env, res, "Could not allocate audio data buffers.");
554  goto bail;
555  }
556 
557  aio->decode_frame = av_frame_alloc();
558  if (!aio->decode_frame) {
559  res = AVERROR(ENOMEM);
560  throwIOExceptionIfError(env, res, "Could not allocate frame.");
561  goto bail;
562  }
563 
564  // initialize packet
565  av_init_packet(&(aio->decode_packet));
566  aio->decode_packet.data = NULL;
567  aio->decode_packet.size = 0;
568 
569 bail:
570 
571  return res;
572 }
573 
574 
584 static int encode_buffer(FFAudioIO *aio, const uint8_t *in_buf, int in_size, const uint8_t *out_buf) {
585  int res = 0;
586  int got_output;
587 
588  res = av_samples_get_buffer_size(NULL, aio->encode_context->channels, aio->encode_frame->nb_samples, aio->encode_context->sample_fmt, 1);
589 
590 #ifdef DEBUG
591  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);
592  fprintf(stderr, "encode_buffer: needed buffer=%d available=%d\n", res, in_size);
593 #endif
594 
595  // setup the data pointers in the AVFrame
596  res = avcodec_fill_audio_frame(aio->encode_frame,
597  aio->encode_context->channels,
598  aio->encode_context->sample_fmt,
599  in_buf,
600  in_size,
601  1);
602  if (res < 0) {
603  throwIOExceptionIfError(aio->env, res, "Failed to fill audio frame.");
604  goto bail;
605  }
606  av_init_packet(&aio->encode_packet);
607  aio->encode_packet.data = NULL; // packet data will be allocated by the encoder
608  aio->encode_packet.size = 0;
609  // encode the samples
610  res = avcodec_encode_audio2(aio->encode_context, &aio->encode_packet, aio->encode_frame, &got_output);
611  if (res < 0) {
612  throwIOExceptionIfError(aio->env, res, "Failed to encode audio frame.");
613  goto bail;
614  }
615  if (got_output) {
616  res = aio->encode_packet.size;
617  memcpy((char*)out_buf, aio->encode_packet.data, aio->encode_packet.size);
618  av_packet_unref(&aio->encode_packet);
619  }
620 
621  bail:
622 
623  return res;
624 }
625 
637 static int resample(FFAudioIO *aio, uint8_t **out_buf, int out_samples, const uint8_t **in_buf, const int in_samples) {
638  int res = 0;
639  int64_t out_channel_count;
640  enum AVSampleFormat out_sample_format;
641 
642  if (out_samples == 0) goto bail; // nothing to do.
643 
644  av_opt_get_int(aio->swr_context, "out_channel_count", 0, &out_channel_count);
645  av_opt_get_sample_fmt(aio->swr_context, "out_sample_fmt", 0, &out_sample_format);
646 
647  #ifdef DEBUG
648  fprintf(stderr, "resample: out_samples=%d in_samples=%d, channels=%d sample_format=%d\n",
649  out_samples, in_samples, (int)out_channel_count, out_sample_format);
650  #endif
651 
652  // allocate temp buffer for resampled data
653  res = av_samples_alloc(out_buf, NULL, out_channel_count, out_samples, out_sample_format, 1);
654  if (res < 0) {
655  res = AVERROR(ENOMEM);
656  throwIOExceptionIfError(aio->env, res, "Could not allocate resample buffer.");
657  goto bail;
658  }
659 
660  // run the SWR conversion (even if it is not strictly necessary)
661  res = swr_convert(aio->swr_context, out_buf, out_samples, in_buf, in_samples);
662  if (res < 0) {
663  throwIOExceptionIfError(aio->env, res, "Failed to convert audio data.");
664  goto bail;
665  }
666 
667  bail:
668 
669  return res;
670 }
671 
672 
681 static int decode_packet(FFAudioIO *aio, int cached) {
682  int res = 0;
683  uint8_t **resample_buf = NULL;
684  jobject byte_buffer = NULL;
685  uint8_t *javaBuffer = NULL;
686  uint32_t out_buf_size = 0;
687  int out_buf_samples = 0;
688  int64_t out_channel_count;
689  int64_t out_sample_rate;
690  int flush = aio->got_frame;
691  enum AVSampleFormat out;
692  int bytesConsumed = 0;
693 
694  init_ids(aio->env, aio->java_instance);
695 
696  av_opt_get_int(aio->swr_context, "out_channel_count", 0, &out_channel_count);
697  av_opt_get_int(aio->swr_context, "out_sample_rate", 0, &out_sample_rate);
698  av_opt_get_sample_fmt(aio->swr_context, "out_sample_fmt", 0, &out);
699 
700  resample_buf = av_mallocz(sizeof(uint8_t *) * 1); // one plane!
701 
702  // make sure we really have an audio packet
703  if (aio->decode_packet.stream_index == aio->stream_index) {
704  // decode frame
705  // got_frame indicates whether we got a frame
706  bytesConsumed = avcodec_decode_audio4(aio->decode_context, aio->decode_frame, &aio->got_frame, &aio->decode_packet);
707  if (bytesConsumed < 0) {
708  throwUnsupportedAudioFileExceptionIfError(aio->env, bytesConsumed, "Failed to decode audio frame.");
709  return bytesConsumed;
710  }
711 
712  if (aio->got_frame) {
713 
714  aio->decoded_samples += aio->decode_frame->nb_samples;
715  out_buf_samples = aio->decode_frame->nb_samples;
716 #ifdef DEBUG
717  fprintf(stderr, "samples%s n:%" PRIu64 " nb_samples:%d pts:%s\n",
718  cached ? "(cached)" : "",
719  aio->decoded_samples, aio->decode_frame->nb_samples,
720  av_ts2timestr(aio->decode_frame->pts, &aio->decode_context->time_base));
721 #endif
722 
723  // adjust out sample number for a different sample rate
724  // this is an estimate!!
725  out_buf_samples = av_rescale_rnd(
726  swr_get_delay(aio->swr_context, aio->stream->codecpar->sample_rate) + aio->decode_frame->nb_samples,
727  out_sample_rate,
728  aio->stream->codecpar->sample_rate,
729  AV_ROUND_UP
730  );
731 
732  // allocate new aio->audio_data buffers
733  res = av_samples_alloc(aio->audio_data, NULL, aio->decode_frame->channels,
734  aio->decode_frame->nb_samples, aio->decode_frame->format, 1);
735  if (res < 0) {
736  throwIOExceptionIfError(aio->env, res, "Could not allocate audio buffer.");
737  return AVERROR(ENOMEM);
738  }
739  // copy audio data to aio->audio_data
740  av_samples_copy(aio->audio_data, aio->decode_frame->data, 0, 0,
741  aio->decode_frame->nb_samples, aio->decode_frame->channels, aio->decode_frame->format);
742 
743  res = resample(aio, resample_buf, out_buf_samples, (const uint8_t **)aio->audio_data, aio->decode_frame->nb_samples);
744  if (res < 0) goto bail;
745  else out_buf_samples = res;
746 
747  } else if (flush && swr_get_delay(aio->swr_context, aio->stream->codecpar->sample_rate)) {
748 
749  res = resample(aio, resample_buf, swr_get_delay(aio->swr_context, aio->stream->codecpar->sample_rate), NULL, 0);
750  if (res < 0) goto bail;
751  else out_buf_samples = res;
752  } else {
753 #ifdef DEBUG
754  fprintf(stderr, "Got no frame.\n");
755 #endif
756  }
757 
758  if (out_buf_samples > 0) {
759 
760  res = av_samples_get_buffer_size(NULL, (int)out_channel_count, out_buf_samples, out, 1);
761  if (res < 0) goto bail;
762  else out_buf_size = res;
763 
764  // ensure native buffer capacity
765  if (aio->java_buffer_capacity < out_buf_size) {
766  aio->java_buffer_capacity = (*aio->env)->CallIntMethod(aio->env, aio->java_instance, setNativeBufferCapacity_MID, (jint)out_buf_size);
767  }
768  // get java-managed byte buffer reference
769  byte_buffer = (*aio->env)->GetObjectField(aio->env, aio->java_instance, nativeBuffer_FID);
770  if (!byte_buffer) {
771  res = -1;
772  throwIOExceptionIfError(aio->env, 1, "Failed to get native buffer.");
773  goto bail;
774  }
775 
776  // we have some samples, let's copy them to the java buffer, using the desired encoding
777  javaBuffer = (uint8_t *)(*aio->env)->GetDirectBufferAddress(aio->env, byte_buffer);
778  if (!javaBuffer) {
779  throwIOExceptionIfError(aio->env, 1, "Failed to get address for native buffer.");
780  goto bail;
781  }
782  if (aio->encode_context) {
783  aio->encode_frame->nb_samples = out_buf_samples;
784  res = encode_buffer(aio, resample_buf[0], out_buf_size, javaBuffer);
785  if (res < 0) {
786  out_buf_size = 0;
787  goto bail;
788  }
789  out_buf_size = res;
790  } else {
791  memcpy(javaBuffer, resample_buf[0], out_buf_size);
792  }
793  // we already wrote to the buffer, now we still need to
794  // set new bytebuffer limit and position to 0.
795  (*aio->env)->CallObjectMethod(aio->env, byte_buffer, rewind_MID);
796  (*aio->env)->CallObjectMethod(aio->env, byte_buffer, limit_MID, out_buf_size);
797  }
798  }
799 
800  aio->resampled_samples += out_buf_size;
801 
802 bail:
803 
804  if (resample_buf) {
805  if (resample_buf[0]) av_freep(&resample_buf[0]);
806  av_free(resample_buf);
807  }
808  if (aio->audio_data[0]) av_freep(&aio->audio_data[0]);
809 
810  return res;
811 }
812 
813 
823  int res = 0;
824  int read_frame = 0;
825 
826  aio->timestamp += aio->decode_packet.duration;
827 
828  while (res == 0 && read_frame >= 0) {
829  read_frame = av_read_frame(aio->format_context, &aio->decode_packet);
830  if (read_frame >= 0) {
831  res = decode_packet(aio, 0);
832  #ifdef DEBUG
833  fprintf(stderr, "res : %i\n", res);
834  fprintf(stderr, "read_frame: %i\n", read_frame);
835  fprintf(stderr, "duration : %i\n", aio->decode_packet.duration);
836  fprintf(stderr, "timestamp : %" PRId64 "\n", aio->timestamp);
837  fprintf(stderr, "pts : %" PRId64 "\n", aio->decode_packet.pts);
838  fprintf(stderr, "dts : %" PRId64 "\n", aio->decode_packet.dts);
839  #endif
840  av_packet_unref(&(aio->decode_packet));
841  } else {
842  #ifdef DEBUG
843  fprintf(stderr, "Reading cached frames: %i\n", aio->got_frame);
844  #endif
845  // flush cached frames
846  av_packet_unref(&(aio->decode_packet));
847  res = decode_packet(aio, 1);
848  }
849  }
850 
851  return res;
852 }
853 
854 
859  #ifdef DEBUG
860  fprintf(stderr, "ff_audioio_free\n");
861  #endif
862 
863  if (aio) {
864 
865  if (aio->encode_frame) {
866  av_frame_free(&aio->encode_frame);
867  }
868  if (aio->decode_context) {
869  avcodec_close(aio->decode_context);
870  av_free(aio->decode_context);
871  }
872  if (aio->encode_context) {
873  avcodec_close(aio->encode_context);
874  av_free(aio->encode_context);
875  }
876  if (aio->swr_context) {
877  swr_free(&aio->swr_context);
878  }
879  if (aio->format_context) {
880  AVFormatContext *s = aio->format_context;
881  if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) || (s->flags & AVFMT_FLAG_CUSTOM_IO)) {
882  if (aio->format_context->pb) {
883  avio_flush(aio->format_context->pb);
884  av_free(aio->format_context->pb->buffer);
885  av_free(aio->format_context->pb);
886  }
887  }
888  avformat_close_input(&(aio->format_context));
889  }
890  if (aio->decode_frame) {
891  av_free(aio->decode_frame);
892  }
893  if (aio->audio_data) {
894  av_free(aio->audio_data);
895  }
896  free(aio);
897  }
898 }
899 
906 int ff_big_endian(enum AVCodecID codec_id) {
907  int big_endian = 0;
908  switch (codec_id) {
909  case AV_CODEC_ID_PCM_S16BE:
910  case AV_CODEC_ID_PCM_U16BE:
911  case AV_CODEC_ID_PCM_S32BE:
912  case AV_CODEC_ID_PCM_U32BE:
913  case AV_CODEC_ID_PCM_S24BE:
914  case AV_CODEC_ID_PCM_U24BE:
915  case AV_CODEC_ID_PCM_F32BE:
916  case AV_CODEC_ID_PCM_F64BE:
917  big_endian = 1;
918  break;
919  default:
920  big_endian = 0;
921  }
922  return big_endian;
923 }
924 
925 
926 void dumpCodecIds() {
927 
928  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S16LE = %#x;\n", AV_CODEC_ID_PCM_S16LE);
929  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S16BE = %#x;\n", AV_CODEC_ID_PCM_S16BE);
930  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U16LE = %#x;\n", AV_CODEC_ID_PCM_U16LE);
931  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U16BE = %#x;\n", AV_CODEC_ID_PCM_U16BE);
932  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S8 = %#x;\n", AV_CODEC_ID_PCM_S8);
933  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U8 = %#x;\n", AV_CODEC_ID_PCM_U8);
934  fprintf(stdout, "private final int AV_CODEC_ID_PCM_MULAW = %#x;\n", AV_CODEC_ID_PCM_MULAW);
935  fprintf(stdout, "private final int AV_CODEC_ID_PCM_ALAW = %#x;\n", AV_CODEC_ID_PCM_ALAW);
936  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S32LE = %#x;\n", AV_CODEC_ID_PCM_S32LE);
937  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S32BE = %#x;\n", AV_CODEC_ID_PCM_S32BE);
938  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U32LE = %#x;\n", AV_CODEC_ID_PCM_U32LE);
939  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U32BE = %#x;\n", AV_CODEC_ID_PCM_U32BE);
940  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S24LE = %#x;\n", AV_CODEC_ID_PCM_S24LE);
941  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S24BE = %#x;\n", AV_CODEC_ID_PCM_S24BE);
942  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U24LE = %#x;\n", AV_CODEC_ID_PCM_U24LE);
943  fprintf(stdout, "private final int AV_CODEC_ID_PCM_U24BE = %#x;\n", AV_CODEC_ID_PCM_U24BE);
944  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S24DAUD = %#x;\n", AV_CODEC_ID_PCM_S24DAUD);
945  fprintf(stdout, "private final int AV_CODEC_ID_PCM_ZORK = %#x;\n", AV_CODEC_ID_PCM_ZORK);
946  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S16LE_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S16LE_PLANAR);
947  fprintf(stdout, "private final int AV_CODEC_ID_PCM_DVD = %#x;\n", AV_CODEC_ID_PCM_DVD);
948  fprintf(stdout, "private final int AV_CODEC_ID_PCM_F32BE = %#x;\n", AV_CODEC_ID_PCM_F32BE);
949  fprintf(stdout, "private final int AV_CODEC_ID_PCM_F32LE = %#x;\n", AV_CODEC_ID_PCM_F32LE);
950  fprintf(stdout, "private final int AV_CODEC_ID_PCM_F64BE = %#x;\n", AV_CODEC_ID_PCM_F64BE);
951  fprintf(stdout, "private final int AV_CODEC_ID_PCM_F64LE = %#x;\n", AV_CODEC_ID_PCM_F64LE);
952  fprintf(stdout, "private final int AV_CODEC_ID_PCM_BLURAY = %#x;\n", AV_CODEC_ID_PCM_BLURAY);
953  fprintf(stdout, "private final int AV_CODEC_ID_PCM_LXF = %#x;\n", AV_CODEC_ID_PCM_LXF);
954  fprintf(stdout, "private final int AV_CODEC_ID_S302M = %#x;\n", AV_CODEC_ID_S302M);
955  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S8_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S8_PLANAR);
956  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S24LE_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S24LE_PLANAR);
957  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S32LE_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S32LE_PLANAR);
958  fprintf(stdout, "private final int AV_CODEC_ID_PCM_S16BE_PLANAR = %#x;\n", AV_CODEC_ID_PCM_S16BE_PLANAR);
959 
960  /* various ADPCM codecs */
961  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_QT = %#x;\n", AV_CODEC_ID_ADPCM_IMA_QT);
962  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_WAV = %#x;\n", AV_CODEC_ID_ADPCM_IMA_WAV);
963  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_DK3 = %#x;\n", AV_CODEC_ID_ADPCM_IMA_DK3);
964  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_DK4 = %#x;\n", AV_CODEC_ID_ADPCM_IMA_DK4);
965  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_WS = %#x;\n", AV_CODEC_ID_ADPCM_IMA_WS);
966  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_SMJPEG = %#x;\n", AV_CODEC_ID_ADPCM_IMA_SMJPEG);
967  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_MS = %#x;\n", AV_CODEC_ID_ADPCM_MS);
968  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_4XM = %#x;\n", AV_CODEC_ID_ADPCM_4XM);
969  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_XA = %#x;\n", AV_CODEC_ID_ADPCM_XA);
970  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_ADX = %#x;\n", AV_CODEC_ID_ADPCM_ADX);
971  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA = %#x;\n", AV_CODEC_ID_ADPCM_EA);
972  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_G726 = %#x;\n", AV_CODEC_ID_ADPCM_G726);
973  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_CT = %#x;\n", AV_CODEC_ID_ADPCM_CT);
974  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_SWF = %#x;\n", AV_CODEC_ID_ADPCM_SWF);
975  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_YAMAHA = %#x;\n", AV_CODEC_ID_ADPCM_YAMAHA);
976  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_SBPRO_4 = %#x;\n", AV_CODEC_ID_ADPCM_SBPRO_4);
977  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_SBPRO_3 = %#x;\n", AV_CODEC_ID_ADPCM_SBPRO_3);
978  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_SBPRO_2 = %#x;\n", AV_CODEC_ID_ADPCM_SBPRO_2);
979  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_THP = %#x;\n", AV_CODEC_ID_ADPCM_THP);
980  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_AMV = %#x;\n", AV_CODEC_ID_ADPCM_IMA_AMV);
981  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_R1 = %#x;\n", AV_CODEC_ID_ADPCM_EA_R1);
982  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_R3 = %#x;\n", AV_CODEC_ID_ADPCM_EA_R3);
983  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_R2 = %#x;\n", AV_CODEC_ID_ADPCM_EA_R2);
984  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_EA_SEAD = %#x;\n", AV_CODEC_ID_ADPCM_IMA_EA_SEAD);
985  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_EA_EACS = %#x;\n", AV_CODEC_ID_ADPCM_IMA_EA_EACS);
986  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_XAS = %#x;\n", AV_CODEC_ID_ADPCM_EA_XAS);
987  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_EA_MAXIS_XA = %#x;\n", AV_CODEC_ID_ADPCM_EA_MAXIS_XA);
988  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_ISS = %#x;\n", AV_CODEC_ID_ADPCM_IMA_ISS);
989  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_G722 = %#x;\n", AV_CODEC_ID_ADPCM_G722);
990  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_APC = %#x;\n", AV_CODEC_ID_ADPCM_IMA_APC);
991  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_VIMA = %#x;\n", AV_CODEC_ID_ADPCM_VIMA);
992  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_AFC = %#x;\n", AV_CODEC_ID_ADPCM_AFC);
993  fprintf(stdout, "private final int AV_CODEC_ID_ADPCM_IMA_OKI = %#x;\n", AV_CODEC_ID_ADPCM_IMA_OKI);
994 
995  /* AMR */
996  fprintf(stdout, "private final int AV_CODEC_ID_AMR_NB = %#x;\n", AV_CODEC_ID_AMR_NB);
997  fprintf(stdout, "private final int AV_CODEC_ID_AMR_WB = %#x;\n", AV_CODEC_ID_AMR_WB);
998 
999  /* RealAudio codecs*/
1000  fprintf(stdout, "private final int AV_CODEC_ID_RA_144 = %#x;\n", AV_CODEC_ID_RA_144);
1001  fprintf(stdout, "private final int AV_CODEC_ID_RA_288 = %#x;\n", AV_CODEC_ID_RA_288);
1002 
1003  /* various DPCM codecs */
1004  fprintf(stdout, "private final int AV_CODEC_ID_ROQ_DPCM = %#x;\n", AV_CODEC_ID_ROQ_DPCM);
1005  fprintf(stdout, "private final int AV_CODEC_ID_INTERPLAY_DPCM = %#x;\n", AV_CODEC_ID_INTERPLAY_DPCM);
1006  fprintf(stdout, "private final int AV_CODEC_ID_XAN_DPCM = %#x;\n", AV_CODEC_ID_XAN_DPCM);
1007  fprintf(stdout, "private final int AV_CODEC_ID_SOL_DPCM = %#x;\n", AV_CODEC_ID_SOL_DPCM);
1008 
1009  /* audio codecs */
1010  fprintf(stdout, "private final int AV_CODEC_ID_MP2 = %#x;\n", AV_CODEC_ID_MP2);
1011  fprintf(stdout, "private final int AV_CODEC_ID_MP3 = %#x;\n", AV_CODEC_ID_MP3);
1012  fprintf(stdout, "private final int AV_CODEC_ID_AAC = %#x;\n", AV_CODEC_ID_AAC);
1013  fprintf(stdout, "private final int AV_CODEC_ID_AC3 = %#x;\n", AV_CODEC_ID_AC3);
1014  fprintf(stdout, "private final int AV_CODEC_ID_DTS = %#x;\n", AV_CODEC_ID_DTS);
1015  fprintf(stdout, "private final int AV_CODEC_ID_VORBIS = %#x;\n", AV_CODEC_ID_VORBIS);
1016  fprintf(stdout, "private final int AV_CODEC_ID_DVAUDIO = %#x;\n", AV_CODEC_ID_DVAUDIO);
1017  fprintf(stdout, "private final int AV_CODEC_ID_WMAV1 = %#x;\n", AV_CODEC_ID_WMAV1);
1018  fprintf(stdout, "private final int AV_CODEC_ID_WMAV2 = %#x;\n", AV_CODEC_ID_WMAV2);
1019  fprintf(stdout, "private final int AV_CODEC_ID_MACE3 = %#x;\n", AV_CODEC_ID_MACE3);
1020  fprintf(stdout, "private final int AV_CODEC_ID_MACE6 = %#x;\n", AV_CODEC_ID_MACE6);
1021  fprintf(stdout, "private final int AV_CODEC_ID_VMDAUDIO = %#x;\n", AV_CODEC_ID_VMDAUDIO);
1022  fprintf(stdout, "private final int AV_CODEC_ID_FLAC = %#x;\n", AV_CODEC_ID_FLAC);
1023  fprintf(stdout, "private final int AV_CODEC_ID_MP3ADU = %#x;\n", AV_CODEC_ID_MP3ADU);
1024  fprintf(stdout, "private final int AV_CODEC_ID_MP3ON4 = %#x;\n", AV_CODEC_ID_MP3ON4);
1025  fprintf(stdout, "private final int AV_CODEC_ID_SHORTEN = %#x;\n", AV_CODEC_ID_SHORTEN);
1026  fprintf(stdout, "private final int AV_CODEC_ID_ALAC = %#x;\n", AV_CODEC_ID_ALAC);
1027  fprintf(stdout, "private final int AV_CODEC_ID_WESTWOOD_SND1 = %#x;\n", AV_CODEC_ID_WESTWOOD_SND1);
1028  fprintf(stdout, "private final int AV_CODEC_ID_GSM = %#x;\n", AV_CODEC_ID_GSM);
1029  fprintf(stdout, "private final int AV_CODEC_ID_QDM2 = %#x;\n", AV_CODEC_ID_QDM2);
1030  fprintf(stdout, "private final int AV_CODEC_ID_COOK = %#x;\n", AV_CODEC_ID_COOK);
1031  fprintf(stdout, "private final int AV_CODEC_ID_TRUESPEECH = %#x;\n", AV_CODEC_ID_TRUESPEECH);
1032  fprintf(stdout, "private final int AV_CODEC_ID_TTA = %#x;\n", AV_CODEC_ID_TTA);
1033  fprintf(stdout, "private final int AV_CODEC_ID_SMACKAUDIO = %#x;\n", AV_CODEC_ID_SMACKAUDIO);
1034  fprintf(stdout, "private final int AV_CODEC_ID_QCELP = %#x;\n", AV_CODEC_ID_QCELP);
1035  fprintf(stdout, "private final int AV_CODEC_ID_WAVPACK = %#x;\n", AV_CODEC_ID_WAVPACK);
1036  fprintf(stdout, "private final int AV_CODEC_ID_DSICINAUDIO = %#x;\n", AV_CODEC_ID_DSICINAUDIO);
1037  fprintf(stdout, "private final int AV_CODEC_ID_IMC = %#x;\n", AV_CODEC_ID_IMC);
1038  fprintf(stdout, "private final int AV_CODEC_ID_MUSEPACK7 = %#x;\n", AV_CODEC_ID_MUSEPACK7);
1039  fprintf(stdout, "private final int AV_CODEC_ID_MLP = %#x;\n", AV_CODEC_ID_MLP);
1040  fprintf(stdout, "private final int AV_CODEC_ID_GSM_MS = %#x;\n", AV_CODEC_ID_GSM_MS); /* as found in WAV */
1041  fprintf(stdout, "private final int AV_CODEC_ID_ATRAC3 = %#x;\n", AV_CODEC_ID_ATRAC3);
1042  //fprintf(stdout, "private final int AV_CODEC_ID_VOXWARE = %#x;\n", AV_CODEC_ID_VOXWARE);
1043  fprintf(stdout, "private final int AV_CODEC_ID_APE = %#x;\n", AV_CODEC_ID_APE);
1044  fprintf(stdout, "private final int AV_CODEC_ID_NELLYMOSER = %#x;\n", AV_CODEC_ID_NELLYMOSER);
1045  fprintf(stdout, "private final int AV_CODEC_ID_MUSEPACK8 = %#x;\n", AV_CODEC_ID_MUSEPACK8);
1046  fprintf(stdout, "private final int AV_CODEC_ID_SPEEX = %#x;\n", AV_CODEC_ID_SPEEX);
1047  fprintf(stdout, "private final int AV_CODEC_ID_WMAVOICE = %#x;\n", AV_CODEC_ID_WMAVOICE);
1048  fprintf(stdout, "private final int AV_CODEC_ID_WMAPRO = %#x;\n", AV_CODEC_ID_WMAPRO);
1049  fprintf(stdout, "private final int AV_CODEC_ID_WMALOSSLESS = %#x;\n", AV_CODEC_ID_WMALOSSLESS);
1050  fprintf(stdout, "private final int AV_CODEC_ID_ATRAC3P = %#x;\n", AV_CODEC_ID_ATRAC3P);
1051  fprintf(stdout, "private final int AV_CODEC_ID_EAC3 = %#x;\n", AV_CODEC_ID_EAC3);
1052  fprintf(stdout, "private final int AV_CODEC_ID_SIPR = %#x;\n", AV_CODEC_ID_SIPR);
1053  fprintf(stdout, "private final int AV_CODEC_ID_MP1 = %#x;\n", AV_CODEC_ID_MP1);
1054  fprintf(stdout, "private final int AV_CODEC_ID_TWINVQ = %#x;\n", AV_CODEC_ID_TWINVQ);
1055  fprintf(stdout, "private final int AV_CODEC_ID_TRUEHD = %#x;\n", AV_CODEC_ID_TRUEHD);
1056  fprintf(stdout, "private final int AV_CODEC_ID_MP4ALS = %#x;\n", AV_CODEC_ID_MP4ALS);
1057  fprintf(stdout, "private final int AV_CODEC_ID_ATRAC1 = %#x;\n", AV_CODEC_ID_ATRAC1);
1058  fprintf(stdout, "private final int AV_CODEC_ID_BINKAUDIO_RDFT = %#x;\n", AV_CODEC_ID_BINKAUDIO_RDFT);
1059  fprintf(stdout, "private final int AV_CODEC_ID_BINKAUDIO_DCT = %#x;\n", AV_CODEC_ID_BINKAUDIO_DCT);
1060  fprintf(stdout, "private final int AV_CODEC_ID_AAC_LATM = %#x;\n", AV_CODEC_ID_AAC_LATM);
1061  fprintf(stdout, "private final int AV_CODEC_ID_QDMC = %#x;\n", AV_CODEC_ID_QDMC);
1062  fprintf(stdout, "private final int AV_CODEC_ID_CELT = %#x;\n", AV_CODEC_ID_CELT);
1063  fprintf(stdout, "private final int AV_CODEC_ID_G723_1 = %#x;\n", AV_CODEC_ID_G723_1);
1064  fprintf(stdout, "private final int AV_CODEC_ID_G729 = %#x;\n", AV_CODEC_ID_G729);
1065  fprintf(stdout, "private final int AV_CODEC_ID_8SVX_EXP = %#x;\n", AV_CODEC_ID_8SVX_EXP);
1066  fprintf(stdout, "private final int AV_CODEC_ID_8SVX_FIB = %#x;\n", AV_CODEC_ID_8SVX_FIB);
1067  fprintf(stdout, "private final int AV_CODEC_ID_BMV_AUDIO = %#x;\n", AV_CODEC_ID_BMV_AUDIO);
1068  fprintf(stdout, "private final int AV_CODEC_ID_RALF = %#x;\n", AV_CODEC_ID_RALF);
1069  fprintf(stdout, "private final int AV_CODEC_ID_IAC = %#x;\n", AV_CODEC_ID_IAC);
1070  fprintf(stdout, "private final int AV_CODEC_ID_ILBC = %#x;\n", AV_CODEC_ID_ILBC);
1071  fprintf(stdout, "private final int AV_CODEC_ID_COMFORT_NOISE = %#x;\n", AV_CODEC_ID_COMFORT_NOISE);
1072  fprintf(stdout, "private final int AV_CODEC_ID_FFWAVESYNTH = %#x;\n", AV_CODEC_ID_FFWAVESYNTH);
1073  // apparently not existent in FFmpeg 2:
1074  //fprintf(stdout, "private final int AV_CODEC_ID_8SVX_RAW = %#x;\n", AV_CODEC_ID_8SVX_RAW);
1075  fprintf(stdout, "private final int AV_CODEC_ID_SONIC = %#x;\n", AV_CODEC_ID_SONIC);
1076  fprintf(stdout, "private final int AV_CODEC_ID_SONIC_LS = %#x;\n", AV_CODEC_ID_SONIC_LS);
1077  fprintf(stdout, "private final int AV_CODEC_ID_PAF_AUDIO = %#x;\n", AV_CODEC_ID_PAF_AUDIO);
1078  fprintf(stdout, "private final int AV_CODEC_ID_OPUS = %#x;\n", AV_CODEC_ID_OPUS);
1079  fprintf(stdout, "private final int AV_CODEC_ID_TAK = %#x;\n", AV_CODEC_ID_TAK);
1080  fprintf(stdout, "private final int AV_CODEC_ID_EVRC = %#x;\n", AV_CODEC_ID_EVRC);
1081  fprintf(stdout, "private final int AV_CODEC_ID_SMV = %#x;\n", AV_CODEC_ID_SMV);
1082 
1083 }
1084 
1088 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
1089  //av_register_all();
1090  avformat_network_init();
1091  //avcodec_register_all();
1092  return JNI_VERSION_1_6;
1093 }
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:188
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:203
int ff_open_stream(JNIEnv *env, AVStream *stream, AVCodecContext **context)
Opens the given stream, i.e.
Definition: FFUtils.c:61
int ff_big_endian(enum AVCodecID codec_id)
Indicates whether the given id belongs to a big endian codec.
Definition: FFUtils.c:906
jint java_buffer_capacity
Current capacity of the Java nativeBuffer.
Definition: FFUtils.h:60
void dumpCodecIds()
Definition: FFUtils.c:926
void throwUnsupportedAudioFileExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an UnsupportedAudioFileException.
Definition: FFUtils.c:158
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:279
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:822
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:516
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:451
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:223
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:398
void throwIndexOutOfBoundsExceptionIfError(JNIEnv *env, int err, int index)
Throws an IndexOutOfBoundsException.
Definition: FFUtils.c:173
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:1088
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:858
uint64_t timestamp
Current timestamp (in samples, not seconds)
Definition: FFUtils.h:72
jobject java_instance
Calling Java instance.
Definition: FFUtils.h:59