FFSampledSP
FFUtils.h
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 
42 #include <math.h>
43 #include <jni.h>
44 // see http://stackoverflow.com/questions/4585847/g-linking-error-on-mac-while-compiling-ffmpeg
45 // or http://ffmpeg.org/trac/ffmpeg/wiki/Including%20FFmpeg%20headers%20in%20a%20C%2B%2B%20application
46 
47 #include <libavformat/avformat.h>
48 #include <libavcodec/avcodec.h>
49 #include <libswresample/swresample.h>
50 #include <libavutil/timestamp.h>
51 #include <libavutil/opt.h>
52 
56 typedef struct {
57  // general stuff
58  JNIEnv *env;
59  jobject java_instance;
61 
62  // decoding
63  AVFormatContext *format_context;
64  AVStream *stream;
66  AVCodecContext *decode_context;
67  AVPacket decode_packet;
68  AVFrame *decode_frame;
69  uint8_t **audio_data;
70  int got_frame;
71  uint64_t decoded_samples;
72  uint64_t timestamp;
73 
74  // resampling
75  SwrContext *swr_context;
76  uint64_t resampled_samples;
77 
78  // encoding
79  AVCodecContext *encode_context;
80  AVPacket encode_packet;
81  AVFrame *encode_frame;
82 
83 } FFAudioIO;
84 
85 void logWarning(FFAudioIO*, int, const char*);
86 
87 void logFine(FFAudioIO*, int, const char*);
88 
89 void throwUnsupportedAudioFileExceptionIfError(JNIEnv*, int, const char*);
90 
91 void throwIOExceptionIfError(JNIEnv*, int, const char*);
92 
93 void throwIndexOutOfBoundsExceptionIfError(JNIEnv*, int, int);
94 
95 void throwFileNotFoundExceptionIfError(JNIEnv*, int, const char*);
96 
97 void dumpCodecIds();
98 
99 int ff_open_stream(JNIEnv*, AVStream*, AVCodecContext**);
100 
101 int ff_open_format_context(JNIEnv*, AVFormatContext**, const char*);
102 
103 int ff_open_file(JNIEnv*, AVFormatContext**, AVStream**, AVCodecContext**, int*, const char*);
104 
105 int ff_init_audioio(JNIEnv*, FFAudioIO*);
106 
108 
110 
111 AVCodec* ff_find_encoder(enum AVSampleFormat, int, int, int);
112 
113 int ff_init_encoder(JNIEnv*, FFAudioIO*, AVCodec*);
114 
115 int ff_big_endian(enum AVCodecID);
116 
117 
int stream_index
Index of the audio stream we are using.
Definition: FFUtils.h:65
void throwUnsupportedAudioFileExceptionIfError(JNIEnv *, int, const char *)
Throws an UnsupportedAudioFileException.
Definition: FFUtils.c:162
AVFormatContext * format_context
Current AVFormatContext.
Definition: FFUtils.h:63
int ff_init_encoder(JNIEnv *, FFAudioIO *, AVCodec *)
Allocates and initializes the encoder context and frame in FFAudioIO.
Definition: FFUtils.c:484
void throwIOExceptionIfError(JNIEnv *, int, const char *)
Throws an IOException.
Definition: FFUtils.c:192
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
int ff_init_audioio(JNIEnv *, FFAudioIO *)
Initialize our main context FFAudioIO, so that SwrContext, decode buffers and the encoder are set to ...
Definition: FFUtils.c:549
void dumpCodecIds()
Definition: FFUtils.c:1001
jint java_buffer_capacity
Current capacity of the Java nativeBuffer.
Definition: FFUtils.h:60
void ff_audioio_free(FFAudioIO *)
Free all resources held by aio and then itself.
Definition: FFUtils.c:933
void logFine(FFAudioIO *, int, const char *)
Log a debug message.
Definition: FFUtils.c:235
void throwFileNotFoundExceptionIfError(JNIEnv *, int, const char *)
Throws an IllegalArgumentException.
Definition: FFUtils.c:207
int ff_big_endian(enum AVCodecID)
Indicates whether the given id belongs to a big endian codec.
Definition: FFUtils.c:981
int got_frame
Indicates whether we got a frame in the last call to avcodec_decode_audio4.
Definition: FFUtils.h:70
int ff_fill_buffer(FFAudioIO *)
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
AVCodec * ff_find_encoder(enum AVSampleFormat, int, int, int)
Finds an AVCodec encoder for the given sample format, bits per sample, byte order and signed/unsigned...
Definition: FFUtils.c:431
AVFrame * encode_frame
AVFrame for encoding.
Definition: FFUtils.h:81
SwrContext * swr_context
Resampling context.
Definition: FFUtils.h:75
void logWarning(FFAudioIO *, int, const char *)
Log a warning.
Definition: FFUtils.c:220
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
int ff_open_format_context(JNIEnv *, AVFormatContext **, const char *)
Opens the input file/url and allocates a AVFormatContext for it, but does not open the audio stream w...
Definition: FFUtils.c:256
AVCodecContext * decode_context
Codec context for decoding.
Definition: FFUtils.h:66
int ff_open_file(JNIEnv *, AVFormatContext **, AVStream **, AVCodecContext **, int *, const char *)
Opens the input file/url, allocates a AVFormatContext for it and opens the audio stream with an appro...
Definition: FFUtils.c:312
AVCodecContext * encode_context
Codec context for encoding.
Definition: FFUtils.h:79
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
int ff_open_stream(JNIEnv *, AVStream *, AVCodecContext **)
Opens the given stream, i.e.
Definition: FFUtils.c:65
uint64_t timestamp
Current timestamp (in samples, not seconds)
Definition: FFUtils.h:72
void throwIndexOutOfBoundsExceptionIfError(JNIEnv *, int, int)
Throws an IndexOutOfBoundsException.
Definition: FFUtils.c:177
jobject java_instance
Calling Java instance.
Definition: FFUtils.h:59