FFSampledSP
Loading...
Searching...
No Matches
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
40
41#include <jni.h>
42#include <math.h>
43// see
44// http://stackoverflow.com/questions/4585847/g-linking-error-on-mac-while-compiling-ffmpeg
45// or
46// http://ffmpeg.org/trac/ffmpeg/wiki/Including%20FFmpeg%20headers%20in%20a%20C%2B%2B%20application
47
48#include <libavcodec/avcodec.h>
49#include <libavformat/avformat.h>
50#include <libavutil/channel_layout.h>
51#include <libavutil/opt.h>
52#include <libavutil/timestamp.h>
53#include <libswresample/swresample.h>
54
59typedef struct {
60 // general stuff
61 JNIEnv *env;
62 jobject java_instance;
64
65 // decoding
66 AVFormatContext *format_context;
67 AVStream *stream;
69 AVCodecContext *decode_context;
70 AVPacket *decode_packet;
71 AVFrame *decode_frame;
72 uint8_t **audio_data;
73 uint64_t decoded_samples;
74 uint64_t timestamp;
75
76 // resampling
77 SwrContext *swr_context;
79
80 // encoding
81 AVCodecContext *encode_context;
82 AVPacket *encode_packet;
83 AVFrame *encode_frame;
84
85} FFAudioIO;
86
87extern const uint32_t CODEC_TAG_DRMS;
88
89void logWarning(FFAudioIO *, int, const char *);
90
91void logFine(FFAudioIO *, int, const char *);
92
93void throwUnsupportedAudioFileExceptionIfError(JNIEnv *, int, const char *);
94
95void throwIOExceptionIfError(JNIEnv *, int, const char *);
96
97void throwIndexOutOfBoundsExceptionIfError(JNIEnv *, int, int);
98
99void throwFileNotFoundExceptionIfError(JNIEnv *, int, const char *);
100
101int ff_open_stream(JNIEnv *, AVStream *, AVCodecContext **);
102
103int ff_open_format_context(JNIEnv *, AVFormatContext **, const char *, int);
104
105int ff_open_file(JNIEnv *, AVFormatContext **, AVStream **, AVCodecContext **,
106 int *, const char *, int);
107
108int ff_init_audioio(JNIEnv *, FFAudioIO *);
109
111
113
114AVCodec *ff_find_encoder(enum AVSampleFormat, int, int, int);
115
116int ff_init_encoder(JNIEnv *, FFAudioIO *, AVCodec *);
117
118int ff_big_endian(enum AVCodecID);
void ff_audioio_free(FFAudioIO *aio)
Free all resources held by aio and then itself.
Definition FFUtils.c:1169
int ff_big_endian(enum AVCodecID codec_id)
Indicates whether the given id belongs to a big endian codec.
Definition FFUtils.c:1221
void logWarning(FFAudioIO *aio, int err, const char *message)
Log a warning.
Definition FFUtils.c:277
void throwFileNotFoundExceptionIfError(JNIEnv *env, int err, const char *message)
Throws a FileNotFoundException.
Definition FFUtils.c:228
int ff_open_file(JNIEnv *env, AVFormatContext **format_context, AVStream **openedStream, AVCodecContext **context, int *stream_index, const char *url, int io_buffer_size)
Opens the input file/url, allocates a AVFormatContext for it and opens the audio stream with an appro...
Definition FFUtils.c:378
int ff_open_format_context(JNIEnv *env, AVFormatContext **format_context, const char *url, int io_buffer_size)
Opens the input file/url and allocates a AVFormatContext for it, but does not open the audio stream w...
Definition FFUtils.c:297
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:528
int ff_fill_buffer(FFAudioIO *aio)
Reads frames via av_read_frame, decodes them, and writes the result to the Java-side nativeBuffer.
Definition FFUtils.c:1107
void throwUnsupportedAudioFileExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an UnsupportedAudioFileException.
Definition FFUtils.c:182
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:663
int ff_init_encoder(JNIEnv *env, FFAudioIO *aio, AVCodec *encoder)
Allocates and initializes the encoder context and frame in FFAudioIO.
Definition FFUtils.c:591
void throwIOExceptionIfError(JNIEnv *env, int err, const char *message)
Throws an IOException.
Definition FFUtils.c:213
const uint32_t CODEC_TAG_DRMS
Definition FFUtils.c:27
int ff_open_stream(JNIEnv *env, AVStream *stream, AVCodecContext **context)
Opens the given stream, i.e.
Definition FFUtils.c:93
void throwIndexOutOfBoundsExceptionIfError(JNIEnv *env, int err, int index)
Throws an IndexOutOfBoundsException.
Definition FFUtils.c:199
void logFine(FFAudioIO *aio, int err, const char *message)
Log a debug message.
Definition FFUtils.c:284
Central context representing the native peer to the Java FFNativePeerInputStream object.
Definition FFUtils.h:59
uint64_t timestamp
Current timestamp (in samples, not seconds).
Definition FFUtils.h:74
AVFormatContext * format_context
Current AVFormatContext.
Definition FFUtils.h:66
int stream_index
Index of the audio stream we are using.
Definition FFUtils.h:68
AVStream * stream
Audio stream we are interested in.
Definition FFUtils.h:67
JNIEnv * env
JNI environment.
Definition FFUtils.h:61
AVPacket * encode_packet
AVPacket for encoding.
Definition FFUtils.h:82
uint64_t decoded_samples
Running count of decoded samples.
Definition FFUtils.h:73
AVFrame * decode_frame
AVFrame for decoding.
Definition FFUtils.h:71
AVFrame * encode_frame
AVFrame for encoding.
Definition FFUtils.h:83
AVPacket * decode_packet
AVPacket for decoding.
Definition FFUtils.h:70
jint java_buffer_capacity
Current capacity of the Java nativeBuffer.
Definition FFUtils.h:63
AVCodecContext * decode_context
Codec context for decoding.
Definition FFUtils.h:69
uint8_t ** audio_data
Audio data (accommodates multiple planes).
Definition FFUtils.h:72
SwrContext * swr_context
Resampling context.
Definition FFUtils.h:77
jobject java_instance
Calling Java instance.
Definition FFUtils.h:62
uint64_t resampled_samples
Count of resampled samples.
Definition FFUtils.h:78
AVCodecContext * encode_context
Codec context for encoding.
Definition FFUtils.h:81