├── .gitignore
├── .gitmodules
├── Project
├── .classpath
├── .gitignore
├── .project
├── AndroidManifest.xml
├── ant.properties
├── build.xml
├── jni
│ ├── .gitignore
│ ├── Android.mk
│ ├── config_make_everything.sh
│ ├── configure_ffmpeg.sh
│ ├── configure_x264.sh
│ ├── create_toolchain.sh
│ ├── make_ffmpeg.sh
│ ├── make_x264.sh
│ ├── settings.sh
│ └── videokit
│ │ ├── cmdutils.c
│ │ ├── ffmpeg.c
│ │ ├── logjam.h
│ │ ├── uk_co_halfninja_videokit_Videokit.c
│ │ └── uk_co_halfninja_videokit_Videokit.h
├── proguard.cfg
├── project.properties
├── res
│ ├── drawable-hdpi
│ │ └── icon.png
│ ├── drawable-ldpi
│ │ └── icon.png
│ ├── drawable-mdpi
│ │ └── icon.png
│ ├── layout
│ │ └── main.xml
│ └── values
│ │ └── strings.xml
└── src
│ └── uk
│ └── co
│ └── halfninja
│ └── videokit
│ └── Videokit.java
├── ProjectTest
├── .classpath
├── .gitignore
├── .project
├── AndroidManifest.xml
├── assets
│ └── image.jpg
├── build.properties
├── build.xml
├── proguard.cfg
├── project.properties
└── src
│ └── uk
│ └── co
│ └── halfninja
│ └── videokit
│ └── VideokitTest.java
├── README.textile
└── init-submodules.sh
/.gitignore:
--------------------------------------------------------------------------------
1 | obj
2 | toolchain
3 | .*.swp
4 |
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "x264"]
2 | path = Project/jni/x264
3 | url = git://git.videolan.org/x264.git
4 | [submodule "ffmpeg"]
5 | path = Project/jni/ffmpeg
6 | url = git://git.videolan.org/ffmpeg.git
7 |
--------------------------------------------------------------------------------
/Project/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Project/.gitignore:
--------------------------------------------------------------------------------
1 | bin
2 | gen
3 | libs
4 | obj
5 | local.properties
6 |
--------------------------------------------------------------------------------
/Project/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Project
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/Project/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Project/ant.properties:
--------------------------------------------------------------------------------
1 | # This file is used to override default values used by the Ant build system.
2 | #
3 | # This file must be checked in Version Control Systems, as it is
4 | # integral to the build system of your project.
5 |
6 | # This file is only used by the Ant script.
7 |
8 | # You can use this to override default values such as
9 | # 'source.dir' for the location of your java source folder and
10 | # 'out.dir' for the location of your output folder.
11 |
12 | # You can also use it define how the release builds are signed by declaring
13 | # the following properties:
14 | # 'key.store' for the location of your keystore and
15 | # 'key.alias' for the name of the key to use.
16 | # The password will be asked during the build when you use the 'release' target.
17 |
18 |
--------------------------------------------------------------------------------
/Project/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
29 |
30 |
31 |
40 |
41 |
42 |
43 |
47 |
48 |
49 |
51 |
63 |
64 |
82 |
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/Project/jni/.gitignore:
--------------------------------------------------------------------------------
1 | output
2 |
--------------------------------------------------------------------------------
/Project/jni/Android.mk:
--------------------------------------------------------------------------------
1 | LOCAL_PATH := $(call my-dir)
2 |
3 | include $(CLEAR_VARS)
4 | LOCAL_MODULE := videokit
5 | # These need to be in the right order
6 | FFMPEG_LIBS := $(addprefix ffmpeg/, \
7 | libavdevice/libavdevice.a \
8 | libavformat/libavformat.a \
9 | libavcodec/libavcodec.a \
10 | libavfilter/libavfilter.a \
11 | libswscale/libswscale.a \
12 | libavutil/libavutil.a \
13 | libpostproc/libpostproc.a )
14 | # ffmpeg uses its own deprecated functions liberally, so turn off that annoying noise
15 | LOCAL_CFLAGS += -g -Iffmpeg -Ivideokit -Wno-deprecated-declarations
16 | LOCAL_LDLIBS += -llog -lz $(FFMPEG_LIBS) x264/libx264.a
17 | LOCAL_SRC_FILES := videokit/uk_co_halfninja_videokit_Videokit.c videokit/ffmpeg.c videokit/cmdutils.c
18 | include $(BUILD_SHARED_LIBRARY)
19 |
20 |
21 | include $(CLEAR_VARS)
22 | LOCAL_MODULE := ffmpeg
23 | FFMPEG_LIBS := $(addprefix ffmpeg/, \
24 | libavdevice/libavdevice.a \
25 | libavformat/libavformat.a \
26 | libavcodec/libavcodec.a \
27 | libavfilter/libavfilter.a \
28 | libswscale/libswscale.a \
29 | libavutil/libavutil.a \
30 | libpostproc/libpostproc.a )
31 | LOCAL_CFLAGS += -g -Iffmpeg -Ivideokit -Wno-deprecated-declarations
32 | LOCAL_LDLIBS += -llog -lz $(FFMPEG_LIBS) x264/libx264.a
33 | LOCAL_SRC_FILES := ffmpeg/ffmpeg.c ffmpeg/cmdutils.c
34 | include $(BUILD_EXECUTABLE)
35 |
36 |
--------------------------------------------------------------------------------
/Project/jni/config_make_everything.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | function die {
4 | echo "$1 failed" && exit 1
5 | }
6 |
7 | ./configure_x264.sh || die "X264 configure"
8 | ./make_x264.sh || die "X264 make"
9 | ./configure_ffmpeg.sh || die "FFMPEG configure"
10 | ./make_ffmpeg.sh || die "FFMPEG make"
11 |
--------------------------------------------------------------------------------
/Project/jni/configure_ffmpeg.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | pushd `dirname $0`
3 | . settings.sh
4 |
5 | if [[ $minimal_featureset == 1 ]]; then
6 | echo "Using minimal featureset"
7 | featureflags="--disable-everything \
8 | --enable-decoder=mjpeg --enable-demuxer=mjpeg --enable-parser=mjpeg \
9 | --enable-demuxer=image2 --enable-muxer=mp4 --enable-encoder=libx264 --enable-libx264 \
10 | --enable-decoder=rawvideo \
11 | --enable-protocol=file \
12 | --enable-hwaccels \
13 | --extra-cflags=../x264"
14 | fi
15 |
16 | if [[ $DEBUG == 1 ]]; then
17 | echo "DEBUG = 1"
18 | DEBUG_FLAG="--disable-stripping"
19 | fi
20 |
21 | pushd ffmpeg
22 |
23 | ./configure $DEBUG_FLAG --enable-cross-compile \
24 | --arch=arm5te \
25 | --enable-armv5te \
26 | --target-os=linux \
27 | --disable-stripping \
28 | --prefix=../output \
29 | --disable-neon \
30 | --enable-version3 \
31 | --disable-shared \
32 | --enable-static \
33 | --enable-gpl \
34 | --enable-memalign-hack \
35 | --cc=arm-linux-androideabi-gcc \
36 | --ld=arm-linux-androideabi-ld \
37 | --extra-cflags="-fPIC -DANDROID -D__thumb__ -mthumb -Wfatal-errors -Wno-deprecated" \
38 | $featureflags \
39 | --disable-ffmpeg \
40 | --disable-ffplay \
41 | --disable-ffprobe \
42 | --disable-ffserver \
43 | --disable-network \
44 | --enable-filter=buffer \
45 | --enable-filter=buffersink \
46 | --disable-demuxer=v4l \
47 | --disable-demuxer=v4l2 \
48 | --disable-indev=v4l \
49 | --disable-indev=v4l2 \
50 | --extra-cflags="-I/usr/local/include -Ivideokit" \
51 | --extra-ldflags="-L/usr/local/lib"
52 |
53 | popd; popd
54 |
--------------------------------------------------------------------------------
/Project/jni/configure_x264.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | pushd `dirname $0`
3 | . settings.sh
4 |
5 | pushd x264
6 |
7 | ./configure --cross-prefix=arm-linux-androideabi- \
8 | --enable-pic --enable-static \
9 | --host=arm-linux
10 |
11 | popd;popd
12 |
--------------------------------------------------------------------------------
/Project/jni/create_toolchain.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | pushd `dirname $0`
3 | . settings.sh
4 |
5 | $NDK/build/tools/make-standalone-toolchain.sh --install-dir=./toolchain
6 |
--------------------------------------------------------------------------------
/Project/jni/make_ffmpeg.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | pushd `dirname $0`
3 | . settings.sh
4 | pushd ffmpeg
5 | make
6 | popd; popd
7 |
--------------------------------------------------------------------------------
/Project/jni/make_x264.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | pushd `dirname $0`
3 | . settings.sh
4 | pushd x264
5 | make
6 | popd;popd
7 |
--------------------------------------------------------------------------------
/Project/jni/settings.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # set to path of your NDK (or export NDK to environment)
4 |
5 | if [[ "x$NDK" == "x" ]]; then
6 | NDK=/home/andy/src/android-ndk-r7
7 | fi
8 | # i use only a small number of formats - set this to 0 if you want everything.
9 | # changed 0 to the default, so it'll compile shitloads of codecs normally
10 | if [[ "x$minimal_featureset" == "x" ]]; then
11 | minimal_featureset=0
12 | fi
13 |
14 | ## stop editing
15 |
16 | if [[ ! -d $NDK ]]; then
17 | echo "$NDK is not a directory. Exiting."
18 | exit 1
19 | fi
20 |
21 | function current_dir {
22 | echo "$(cd "$(dirname $0)"; pwd)"
23 | }
24 |
25 | export PATH=$PATH:$NDK:$(current_dir)/toolchain/bin
26 |
27 | echo $PATH
28 |
29 |
--------------------------------------------------------------------------------
/Project/jni/videokit/cmdutils.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Various utilities for command line tools
3 | * Copyright (c) 2000-2003 Fabrice Bellard
4 | *
5 | * This file is part of FFmpeg.
6 | *
7 | * FFmpeg is free software; you can redistribute it and/or
8 | * modify it under the terms of the GNU Lesser General Public
9 | * License as published by the Free Software Foundation; either
10 | * version 2.1 of the License, or (at your option) any later version.
11 | *
12 | * FFmpeg is distributed in the hope that it will be useful,
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 | * Lesser General Public License for more details.
16 | *
17 | * You should have received a copy of the GNU Lesser General Public
18 | * License along with FFmpeg; if not, write to the Free Software
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 | */
21 |
22 | #include "logjam.h"
23 |
24 | #include
25 | #include
26 | #include
27 | #include
28 |
29 | /* Include only the enabled headers since some compilers (namely, Sun
30 | Studio) will not omit unused inline functions and create undefined
31 | references to libraries that are not being built. */
32 |
33 | #include "config.h"
34 | #include "libavformat/avformat.h"
35 | #include "libavfilter/avfilter.h"
36 | #include "libavdevice/avdevice.h"
37 | #include "libswscale/swscale.h"
38 | #include "libpostproc/postprocess.h"
39 | #include "libavutil/avstring.h"
40 | #include "libavutil/parseutils.h"
41 | #include "libavutil/pixdesc.h"
42 | #include "libavutil/eval.h"
43 | #include "libavutil/dict.h"
44 | #include "libavutil/opt.h"
45 | #include "cmdutils.h"
46 | #include "version.h"
47 | #if CONFIG_NETWORK
48 | #include "libavformat/network.h"
49 | #endif
50 | #if HAVE_SYS_RESOURCE_H
51 | #include
52 | #endif
53 |
54 | const char **opt_names;
55 | const char **opt_values;
56 | static int opt_name_count;
57 | AVCodecContext *avcodec_opts[AVMEDIA_TYPE_NB];
58 | AVFormatContext *avformat_opts;
59 | struct SwsContext *sws_opts;
60 | AVDictionary *format_opts, *video_opts, *audio_opts, *sub_opts;
61 |
62 | static const int this_year = 2011;
63 |
64 | void init_opts(void)
65 | {
66 | int i;
67 | for (i = 0; i < AVMEDIA_TYPE_NB; i++)
68 | avcodec_opts[i] = avcodec_alloc_context2(i);
69 | avformat_opts = avformat_alloc_context();
70 | #if CONFIG_SWSCALE
71 | sws_opts = sws_getContext(16, 16, 0, 16, 16, 0, SWS_BICUBIC, NULL, NULL, NULL);
72 | #endif
73 | }
74 |
75 | void uninit_opts(void)
76 | {
77 | int i;
78 | for (i = 0; i < AVMEDIA_TYPE_NB; i++)
79 | av_freep(&avcodec_opts[i]);
80 | av_freep(&avformat_opts->key);
81 | av_freep(&avformat_opts);
82 | #if CONFIG_SWSCALE
83 | sws_freeContext(sws_opts);
84 | sws_opts = NULL;
85 | #endif
86 | for (i = 0; i < opt_name_count; i++) {
87 | av_freep(&opt_names[i]);
88 | av_freep(&opt_values[i]);
89 | }
90 | av_freep(&opt_names);
91 | av_freep(&opt_values);
92 | opt_name_count = 0;
93 | av_dict_free(&format_opts);
94 | av_dict_free(&video_opts);
95 | av_dict_free(&audio_opts);
96 | av_dict_free(&sub_opts);
97 | }
98 |
99 | void log_callback_help(void* ptr, int level, const char* fmt, va_list vl)
100 | {
101 | vfprintf(stdout, fmt, vl);
102 | }
103 |
104 | double parse_number_or_die(const char *context, const char *numstr, int type, double min, double max)
105 | {
106 | char *tail;
107 | const char *error;
108 | double d = av_strtod(numstr, &tail);
109 | if (*tail)
110 | error= "Expected number for %s but found: %s\n";
111 | else if (d < min || d > max)
112 | error= "The value for %s was %s which is not within %f - %f\n";
113 | else if(type == OPT_INT64 && (int64_t)d != d)
114 | error= "Expected int64 for %s but found %s\n";
115 | else if (type == OPT_INT && (int)d != d)
116 | error= "Expected int for %s but found %s\n";
117 | else
118 | return d;
119 | LOGE( error, context, numstr, min, max);
120 | exit(1);
121 | }
122 |
123 | int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
124 | {
125 | int64_t us;
126 | if (av_parse_time(&us, timestr, is_duration) < 0) {
127 | LOGE( "Invalid %s specification for %s: %s\n",
128 | is_duration ? "duration" : "date", context, timestr);
129 | exit(1);
130 | }
131 | return us;
132 | }
133 |
134 | void show_help_options(const OptionDef *options, const char *msg, int mask, int value)
135 | {
136 | const OptionDef *po;
137 | int first;
138 |
139 | first = 1;
140 | for(po = options; po->name != NULL; po++) {
141 | char buf[64];
142 | if ((po->flags & mask) == value) {
143 | if (first) {
144 | printf("%s", msg);
145 | first = 0;
146 | }
147 | av_strlcpy(buf, po->name, sizeof(buf));
148 | if (po->flags & HAS_ARG) {
149 | av_strlcat(buf, " ", sizeof(buf));
150 | av_strlcat(buf, po->argname, sizeof(buf));
151 | }
152 | printf("-%-17s %s\n", buf, po->help);
153 | }
154 | }
155 | }
156 |
157 | static const OptionDef* find_option(const OptionDef *po, const char *name){
158 | while (po->name != NULL) {
159 | if (!strcmp(name, po->name))
160 | break;
161 | po++;
162 | }
163 | return po;
164 | }
165 |
166 | #if defined(_WIN32) && !defined(__MINGW32CE__)
167 | #include
168 | /* Will be leaked on exit */
169 | static char** win32_argv_utf8 = NULL;
170 | static int win32_argc = 0;
171 |
172 | /**
173 | * Prepare command line arguments for executable.
174 | * For Windows - perform wide-char to UTF-8 conversion.
175 | * Input arguments should be main() function arguments.
176 | * @param argc_ptr Arguments number (including executable)
177 | * @param argv_ptr Arguments list.
178 | */
179 | static void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
180 | {
181 | char *argstr_flat;
182 | wchar_t **argv_w;
183 | int i, buffsize = 0, offset = 0;
184 |
185 | if (win32_argv_utf8) {
186 | *argc_ptr = win32_argc;
187 | *argv_ptr = win32_argv_utf8;
188 | return;
189 | }
190 |
191 | win32_argc = 0;
192 | argv_w = CommandLineToArgvW(GetCommandLineW(), &win32_argc);
193 | if (win32_argc <= 0 || !argv_w)
194 | return;
195 |
196 | /* determine the UTF-8 buffer size (including NULL-termination symbols) */
197 | for (i = 0; i < win32_argc; i++)
198 | buffsize += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
199 | NULL, 0, NULL, NULL);
200 |
201 | win32_argv_utf8 = av_mallocz(sizeof(char*) * (win32_argc + 1) + buffsize);
202 | argstr_flat = (char*)win32_argv_utf8 + sizeof(char*) * (win32_argc + 1);
203 | if (win32_argv_utf8 == NULL) {
204 | LocalFree(argv_w);
205 | return;
206 | }
207 |
208 | for (i = 0; i < win32_argc; i++) {
209 | win32_argv_utf8[i] = &argstr_flat[offset];
210 | offset += WideCharToMultiByte(CP_UTF8, 0, argv_w[i], -1,
211 | &argstr_flat[offset],
212 | buffsize - offset, NULL, NULL);
213 | }
214 | win32_argv_utf8[i] = NULL;
215 | LocalFree(argv_w);
216 |
217 | *argc_ptr = win32_argc;
218 | *argv_ptr = win32_argv_utf8;
219 | }
220 | #else
221 | static inline void prepare_app_arguments(int *argc_ptr, char ***argv_ptr)
222 | {
223 | /* nothing to do */
224 | }
225 | #endif /* WIN32 && !__MINGW32CE__ */
226 |
227 | void parse_options(int argc, char **argv, const OptionDef *options,
228 | int (* parse_arg_function)(const char *opt, const char *arg))
229 | {
230 | const char *opt, *arg;
231 | int optindex, handleoptions=1;
232 | const OptionDef *po;
233 |
234 | LOGD("parse_options has %d options to parse", argc);
235 |
236 | /* perform system-dependent conversions for arguments list */
237 | prepare_app_arguments(&argc, &argv);
238 |
239 | /* parse options */
240 | optindex = 1;
241 | while (optindex < argc) {
242 | opt = argv[optindex++];
243 |
244 | if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
245 | int bool_val = 1;
246 | if (opt[1] == '-' && opt[2] == '\0') {
247 | handleoptions = 0;
248 | continue;
249 | }
250 | opt++;
251 | po= find_option(options, opt);
252 | if (!po->name && opt[0] == 'n' && opt[1] == 'o') {
253 | /* handle 'no' bool option */
254 | po = find_option(options, opt + 2);
255 | if (!(po->name && (po->flags & OPT_BOOL)))
256 | goto unknown_opt;
257 | bool_val = 0;
258 | }
259 | if (!po->name)
260 | po= find_option(options, "default");
261 | if (!po->name) {
262 | unknown_opt:
263 | LOGE( "%s: unrecognized option '%s'\n", argv[0], opt);
264 | exit(1);
265 | }
266 | arg = NULL;
267 | if (po->flags & HAS_ARG) {
268 | arg = argv[optindex++];
269 | if (!arg) {
270 | LOGE( "%s: missing argument for option '%s'\n", argv[0], opt);
271 | exit(1);
272 | }
273 | }
274 | if (po->flags & OPT_STRING) {
275 | char *str;
276 | str = av_strdup(arg);
277 | *po->u.str_arg = str;
278 | } else if (po->flags & OPT_BOOL) {
279 | *po->u.int_arg = bool_val;
280 | } else if (po->flags & OPT_INT) {
281 | *po->u.int_arg = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
282 | } else if (po->flags & OPT_INT64) {
283 | *po->u.int64_arg = parse_number_or_die(opt, arg, OPT_INT64, INT64_MIN, INT64_MAX);
284 | } else if (po->flags & OPT_FLOAT) {
285 | *po->u.float_arg = parse_number_or_die(opt, arg, OPT_FLOAT, -INFINITY, INFINITY);
286 | } else if (po->u.func_arg) {
287 | if (po->u.func_arg(opt, arg) < 0) {
288 | LOGE( "%s: failed to set value '%s' for option '%s'\n", argv[0], arg, opt);
289 | exit(1);
290 | }
291 | }
292 | if(po->flags & OPT_EXIT) {
293 | LOGE("Oh alright bye");
294 | //exit(0);
295 | }
296 | } else {
297 | if (parse_arg_function) {
298 | if (parse_arg_function(NULL, opt) < 0) {
299 | LOGE("Something about parsing?");
300 | exit(1);
301 | }
302 | }
303 | }
304 | }
305 | }
306 |
307 | #define FLAGS (o->type == FF_OPT_TYPE_FLAGS) ? AV_DICT_APPEND : 0
308 | #define SET_PREFIXED_OPTS(ch, flag, output) \
309 | if (opt[0] == ch && avcodec_opts[0] && (o = av_opt_find(avcodec_opts[0], opt+1, NULL, flag, 0)))\
310 | av_dict_set(&output, opt+1, arg, FLAGS);
311 | static int opt_default2(const char *opt, const char *arg)
312 | {
313 | const AVOption *o;
314 | if ((o = av_opt_find(avcodec_opts[0], opt, NULL, 0, AV_OPT_SEARCH_CHILDREN))) {
315 | if (o->flags & AV_OPT_FLAG_VIDEO_PARAM)
316 | av_dict_set(&video_opts, opt, arg, FLAGS);
317 | if (o->flags & AV_OPT_FLAG_AUDIO_PARAM)
318 | av_dict_set(&audio_opts, opt, arg, FLAGS);
319 | if (o->flags & AV_OPT_FLAG_SUBTITLE_PARAM)
320 | av_dict_set(&sub_opts, opt, arg, FLAGS);
321 | } else if ((o = av_opt_find(avformat_opts, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN)))
322 | av_dict_set(&format_opts, opt, arg, FLAGS);
323 | else if ((o = av_opt_find(sws_opts, opt, NULL, 0, AV_OPT_SEARCH_CHILDREN))) {
324 | // XXX we only support sws_flags, not arbitrary sws options
325 | int ret = av_set_string3(sws_opts, opt, arg, 1, NULL);
326 | if (ret < 0) {
327 | av_log(NULL, AV_LOG_ERROR, "Error setting option %s.\n", opt);
328 | return ret;
329 | }
330 | }
331 |
332 | if (!o) {
333 | SET_PREFIXED_OPTS('v', AV_OPT_FLAG_VIDEO_PARAM, video_opts)
334 | SET_PREFIXED_OPTS('a', AV_OPT_FLAG_AUDIO_PARAM, audio_opts)
335 | SET_PREFIXED_OPTS('s', AV_OPT_FLAG_SUBTITLE_PARAM, sub_opts)
336 | }
337 |
338 | if (o)
339 | return 0;
340 | LOGE( "Unrecognized option '%s'\n", opt);
341 | return AVERROR_OPTION_NOT_FOUND;
342 | }
343 |
344 | int opt_default(const char *opt, const char *arg){
345 | int type;
346 | int ret= 0;
347 | const AVOption *o= NULL;
348 | int opt_types[]={AV_OPT_FLAG_VIDEO_PARAM, AV_OPT_FLAG_AUDIO_PARAM, 0, AV_OPT_FLAG_SUBTITLE_PARAM, 0};
349 | AVCodec *p = NULL;
350 | AVOutputFormat *oformat = NULL;
351 | AVInputFormat *iformat = NULL;
352 |
353 | while ((p = av_codec_next(p))) {
354 | const AVClass *c = p->priv_class;
355 | if (c && av_find_opt(&c, opt, NULL, 0, 0))
356 | break;
357 | }
358 | if (p)
359 | goto out;
360 | while ((oformat = av_oformat_next(oformat))) {
361 | const AVClass *c = oformat->priv_class;
362 | if (c && av_find_opt(&c, opt, NULL, 0, 0))
363 | break;
364 | }
365 | if (oformat)
366 | goto out;
367 | while ((iformat = av_iformat_next(iformat))) {
368 | const AVClass *c = iformat->priv_class;
369 | if (c && av_find_opt(&c, opt, NULL, 0, 0))
370 | break;
371 | }
372 | if (iformat)
373 | goto out;
374 |
375 | for(type=0; *avcodec_opts && type= 0; type++){
376 | const AVOption *o2 = av_opt_find(avcodec_opts[0], opt, NULL, opt_types[type], 0);
377 | if(o2)
378 | ret = av_set_string3(avcodec_opts[type], opt, arg, 1, &o);
379 | }
380 | if(!o && avformat_opts)
381 | ret = av_set_string3(avformat_opts, opt, arg, 1, &o);
382 | if(!o && sws_opts)
383 | ret = av_set_string3(sws_opts, opt, arg, 1, &o);
384 | if(!o){
385 | if (opt[0] == 'a' && avcodec_opts[AVMEDIA_TYPE_AUDIO])
386 | ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_AUDIO], opt+1, arg, 1, &o);
387 | else if(opt[0] == 'v' && avcodec_opts[AVMEDIA_TYPE_VIDEO])
388 | ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_VIDEO], opt+1, arg, 1, &o);
389 | else if(opt[0] == 's' && avcodec_opts[AVMEDIA_TYPE_SUBTITLE])
390 | ret = av_set_string3(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], opt+1, arg, 1, &o);
391 | if (ret >= 0)
392 | opt += 1;
393 | }
394 | if (o && ret < 0) {
395 | LOGE( "Invalid value '%s' for option '%s'\n", arg, opt);
396 | exit(1);
397 | }
398 | if (!o) {
399 | LOGE( "Unrecognized option '%s'\n", opt);
400 | exit(1);
401 | }
402 |
403 | out:
404 | if ((ret = opt_default2(opt, arg)) < 0)
405 | return ret;
406 |
407 | // av_log(NULL, AV_LOG_ERROR, "%s:%s: %f 0x%0X\n", opt, arg, av_get_double(avcodec_opts, opt, NULL), (int)av_get_int(avcodec_opts, opt, NULL));
408 |
409 | opt_values= av_realloc(opt_values, sizeof(void*)*(opt_name_count+1));
410 | opt_values[opt_name_count] = av_strdup(arg);
411 | opt_names= av_realloc(opt_names, sizeof(void*)*(opt_name_count+1));
412 | opt_names[opt_name_count++] = av_strdup(opt);
413 |
414 | if ((*avcodec_opts && avcodec_opts[0]->debug) || (avformat_opts && avformat_opts->debug))
415 | av_log_set_level(AV_LOG_DEBUG);
416 | return 0;
417 | }
418 |
419 | int opt_loglevel(const char *opt, const char *arg)
420 | {
421 | const struct { const char *name; int level; } log_levels[] = {
422 | { "quiet" , AV_LOG_QUIET },
423 | { "panic" , AV_LOG_PANIC },
424 | { "fatal" , AV_LOG_FATAL },
425 | { "error" , AV_LOG_ERROR },
426 | { "warning", AV_LOG_WARNING },
427 | { "info" , AV_LOG_INFO },
428 | { "verbose", AV_LOG_VERBOSE },
429 | { "debug" , AV_LOG_DEBUG },
430 | };
431 | char *tail;
432 | int level;
433 | int i;
434 |
435 | for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
436 | if (!strcmp(log_levels[i].name, arg)) {
437 | av_log_set_level(log_levels[i].level);
438 | return 0;
439 | }
440 | }
441 |
442 | level = strtol(arg, &tail, 10);
443 | if (*tail) {
444 | LOGE( "Invalid loglevel \"%s\". "
445 | "Possible levels are numbers or:\n", arg);
446 | for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)
447 | LOGE( "\"%s\"\n", log_levels[i].name);
448 | exit(1);
449 | }
450 | av_log_set_level(level);
451 | return 0;
452 | }
453 |
454 | int opt_timelimit(const char *opt, const char *arg)
455 | {
456 | #if HAVE_SETRLIMIT
457 | int lim = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
458 | struct rlimit rl = { lim, lim + 1 };
459 | if (setrlimit(RLIMIT_CPU, &rl))
460 | perror("setrlimit");
461 | #else
462 | LOGE( "Warning: -%s not implemented on this OS\n", opt);
463 | #endif
464 | return 0;
465 | }
466 |
467 | static void *alloc_priv_context(int size, const AVClass *class)
468 | {
469 | void *p = av_mallocz(size);
470 | if (p) {
471 | *(const AVClass **)p = class;
472 | av_opt_set_defaults(p);
473 | }
474 | return p;
475 | }
476 |
477 | void set_context_opts(void *ctx, void *opts_ctx, int flags, AVCodec *codec)
478 | {
479 | int i;
480 | void *priv_ctx=NULL;
481 | if(!strcmp("AVCodecContext", (*(AVClass**)ctx)->class_name)){
482 | AVCodecContext *avctx= ctx;
483 | if(codec && codec->priv_class){
484 | if(!avctx->priv_data && codec->priv_data_size)
485 | avctx->priv_data= alloc_priv_context(codec->priv_data_size, codec->priv_class);
486 | priv_ctx= avctx->priv_data;
487 | }
488 | } else if (!strcmp("AVFormatContext", (*(AVClass**)ctx)->class_name)) {
489 | AVFormatContext *avctx = ctx;
490 | if (avctx->oformat && avctx->oformat->priv_class) {
491 | priv_ctx = avctx->priv_data;
492 | } else if (avctx->iformat && avctx->iformat->priv_class) {
493 | priv_ctx = avctx->priv_data;
494 | }
495 | }
496 |
497 | for(i=0; iflags & flags) == flags))
515 | av_set_string3(ctx, opt_names[i], str, 1, NULL);
516 | }
517 | }
518 | }
519 |
520 | void print_error(const char *filename, int err)
521 | {
522 | char errbuf[128];
523 | const char *errbuf_ptr = errbuf;
524 |
525 | if (av_strerror(err, errbuf, sizeof(errbuf)) < 0)
526 | errbuf_ptr = strerror(AVUNERROR(err));
527 | LOGE( "%s: %s\n", filename, errbuf_ptr);
528 | }
529 |
530 | static int warned_cfg = 0;
531 |
532 | #define INDENT 1
533 | #define SHOW_VERSION 2
534 | #define SHOW_CONFIG 4
535 |
536 | #define PRINT_LIB_INFO(outstream,libname,LIBNAME,flags) \
537 | if (CONFIG_##LIBNAME) { \
538 | const char *indent = flags & INDENT? " " : ""; \
539 | if (flags & SHOW_VERSION) { \
540 | unsigned int version = libname##_version(); \
541 | fprintf(outstream, "%slib%-9s %2d.%3d.%2d / %2d.%3d.%2d\n", \
542 | indent, #libname, \
543 | LIB##LIBNAME##_VERSION_MAJOR, \
544 | LIB##LIBNAME##_VERSION_MINOR, \
545 | LIB##LIBNAME##_VERSION_MICRO, \
546 | version >> 16, version >> 8 & 0xff, version & 0xff); \
547 | } \
548 | if (flags & SHOW_CONFIG) { \
549 | const char *cfg = libname##_configuration(); \
550 | if (strcmp(FFMPEG_CONFIGURATION, cfg)) { \
551 | if (!warned_cfg) { \
552 | fprintf(outstream, \
553 | "%sWARNING: library configuration mismatch\n", \
554 | indent); \
555 | warned_cfg = 1; \
556 | } \
557 | LOGE( "%s%-11s configuration: %s\n", \
558 | indent, #libname, cfg); \
559 | } \
560 | } \
561 | } \
562 |
563 | static void print_all_libs_info(FILE* outstream, int flags)
564 | {
565 | PRINT_LIB_INFO(outstream, avutil, AVUTIL, flags);
566 | PRINT_LIB_INFO(outstream, avcodec, AVCODEC, flags);
567 | PRINT_LIB_INFO(outstream, avformat, AVFORMAT, flags);
568 | PRINT_LIB_INFO(outstream, avdevice, AVDEVICE, flags);
569 | PRINT_LIB_INFO(outstream, avfilter, AVFILTER, flags);
570 | PRINT_LIB_INFO(outstream, swscale, SWSCALE, flags);
571 | PRINT_LIB_INFO(outstream, postproc, POSTPROC, flags);
572 | }
573 |
574 | void show_banner(void)
575 | {
576 | LOGE( "%s version " FFMPEG_VERSION ", Copyright (c) %d-%d the FFmpeg developers\n",
577 | program_name, program_birth_year, this_year);
578 | LOGE( " built on %s %s with %s %s\n",
579 | __DATE__, __TIME__, CC_TYPE, CC_VERSION);
580 | LOGE( " configuration: " FFMPEG_CONFIGURATION "\n");
581 | print_all_libs_info(stderr, INDENT|SHOW_CONFIG);
582 | print_all_libs_info(stderr, INDENT|SHOW_VERSION);
583 | }
584 |
585 | void show_version(void) {
586 | printf("%s " FFMPEG_VERSION "\n", program_name);
587 | print_all_libs_info(stdout, SHOW_VERSION);
588 | }
589 |
590 | void show_license(void)
591 | {
592 | printf(
593 | #if CONFIG_NONFREE
594 | "This version of %s has nonfree parts compiled in.\n"
595 | "Therefore it is not legally redistributable.\n",
596 | program_name
597 | #elif CONFIG_GPLV3
598 | "%s is free software; you can redistribute it and/or modify\n"
599 | "it under the terms of the GNU General Public License as published by\n"
600 | "the Free Software Foundation; either version 3 of the License, or\n"
601 | "(at your option) any later version.\n"
602 | "\n"
603 | "%s is distributed in the hope that it will be useful,\n"
604 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
605 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
606 | "GNU General Public License for more details.\n"
607 | "\n"
608 | "You should have received a copy of the GNU General Public License\n"
609 | "along with %s. If not, see .\n",
610 | program_name, program_name, program_name
611 | #elif CONFIG_GPL
612 | "%s is free software; you can redistribute it and/or modify\n"
613 | "it under the terms of the GNU General Public License as published by\n"
614 | "the Free Software Foundation; either version 2 of the License, or\n"
615 | "(at your option) any later version.\n"
616 | "\n"
617 | "%s is distributed in the hope that it will be useful,\n"
618 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
619 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
620 | "GNU General Public License for more details.\n"
621 | "\n"
622 | "You should have received a copy of the GNU General Public License\n"
623 | "along with %s; if not, write to the Free Software\n"
624 | "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
625 | program_name, program_name, program_name
626 | #elif CONFIG_LGPLV3
627 | "%s is free software; you can redistribute it and/or modify\n"
628 | "it under the terms of the GNU Lesser General Public License as published by\n"
629 | "the Free Software Foundation; either version 3 of the License, or\n"
630 | "(at your option) any later version.\n"
631 | "\n"
632 | "%s is distributed in the hope that it will be useful,\n"
633 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
634 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
635 | "GNU Lesser General Public License for more details.\n"
636 | "\n"
637 | "You should have received a copy of the GNU Lesser General Public License\n"
638 | "along with %s. If not, see .\n",
639 | program_name, program_name, program_name
640 | #else
641 | "%s is free software; you can redistribute it and/or\n"
642 | "modify it under the terms of the GNU Lesser General Public\n"
643 | "License as published by the Free Software Foundation; either\n"
644 | "version 2.1 of the License, or (at your option) any later version.\n"
645 | "\n"
646 | "%s is distributed in the hope that it will be useful,\n"
647 | "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
648 | "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
649 | "Lesser General Public License for more details.\n"
650 | "\n"
651 | "You should have received a copy of the GNU Lesser General Public\n"
652 | "License along with %s; if not, write to the Free Software\n"
653 | "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n",
654 | program_name, program_name, program_name
655 | #endif
656 | );
657 | }
658 |
659 | void show_formats(void)
660 | {
661 | AVInputFormat *ifmt=NULL;
662 | AVOutputFormat *ofmt=NULL;
663 | const char *last_name;
664 |
665 | printf(
666 | "File formats:\n"
667 | " D. = Demuxing supported\n"
668 | " .E = Muxing supported\n"
669 | " --\n");
670 | last_name= "000";
671 | for(;;){
672 | int decode=0;
673 | int encode=0;
674 | const char *name=NULL;
675 | const char *long_name=NULL;
676 |
677 | while((ofmt= av_oformat_next(ofmt))) {
678 | if((name == NULL || strcmp(ofmt->name, name)<0) &&
679 | strcmp(ofmt->name, last_name)>0){
680 | name= ofmt->name;
681 | long_name= ofmt->long_name;
682 | encode=1;
683 | }
684 | }
685 | while((ifmt= av_iformat_next(ifmt))) {
686 | if((name == NULL || strcmp(ifmt->name, name)<0) &&
687 | strcmp(ifmt->name, last_name)>0){
688 | name= ifmt->name;
689 | long_name= ifmt->long_name;
690 | encode=0;
691 | }
692 | if(name && strcmp(ifmt->name, name)==0)
693 | decode=1;
694 | }
695 | if(name==NULL)
696 | break;
697 | last_name= name;
698 |
699 | printf(
700 | " %s%s %-15s %s\n",
701 | decode ? "D":" ",
702 | encode ? "E":" ",
703 | name,
704 | long_name ? long_name:" ");
705 | }
706 | }
707 |
708 | void show_codecs(void)
709 | {
710 | AVCodec *p=NULL, *p2;
711 | const char *last_name;
712 | printf(
713 | "Codecs:\n"
714 | " D..... = Decoding supported\n"
715 | " .E.... = Encoding supported\n"
716 | " ..V... = Video codec\n"
717 | " ..A... = Audio codec\n"
718 | " ..S... = Subtitle codec\n"
719 | " ...S.. = Supports draw_horiz_band\n"
720 | " ....D. = Supports direct rendering method 1\n"
721 | " .....T = Supports weird frame truncation\n"
722 | " ------\n");
723 | last_name= "000";
724 | for(;;){
725 | int decode=0;
726 | int encode=0;
727 | int cap=0;
728 | const char *type_str;
729 |
730 | p2=NULL;
731 | while((p= av_codec_next(p))) {
732 | if((p2==NULL || strcmp(p->name, p2->name)<0) &&
733 | strcmp(p->name, last_name)>0){
734 | p2= p;
735 | decode= encode= cap=0;
736 | }
737 | if(p2 && strcmp(p->name, p2->name)==0){
738 | if(p->decode) decode=1;
739 | if(p->encode) encode=1;
740 | cap |= p->capabilities;
741 | }
742 | }
743 | if(p2==NULL)
744 | break;
745 | last_name= p2->name;
746 |
747 | switch(p2->type) {
748 | case AVMEDIA_TYPE_VIDEO:
749 | type_str = "V";
750 | break;
751 | case AVMEDIA_TYPE_AUDIO:
752 | type_str = "A";
753 | break;
754 | case AVMEDIA_TYPE_SUBTITLE:
755 | type_str = "S";
756 | break;
757 | default:
758 | type_str = "?";
759 | break;
760 | }
761 | printf(
762 | " %s%s%s%s%s%s %-15s %s",
763 | decode ? "D": (/*p2->decoder ? "d":*/" "),
764 | encode ? "E":" ",
765 | type_str,
766 | cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
767 | cap & CODEC_CAP_DR1 ? "D":" ",
768 | cap & CODEC_CAP_TRUNCATED ? "T":" ",
769 | p2->name,
770 | p2->long_name ? p2->long_name : "");
771 | /* if(p2->decoder && decode==0)
772 | printf(" use %s for decoding", p2->decoder->name);*/
773 | printf("\n");
774 | }
775 | printf("\n");
776 | printf(
777 | "Note, the names of encoders and decoders do not always match, so there are\n"
778 | "several cases where the above table shows encoder only or decoder only entries\n"
779 | "even though both encoding and decoding are supported. For example, the h263\n"
780 | "decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
781 | "worse.\n");
782 | }
783 |
784 | void show_bsfs(void)
785 | {
786 | AVBitStreamFilter *bsf=NULL;
787 |
788 | printf("Bitstream filters:\n");
789 | while((bsf = av_bitstream_filter_next(bsf)))
790 | printf("%s\n", bsf->name);
791 | printf("\n");
792 | }
793 |
794 | void show_protocols(void)
795 | {
796 | URLProtocol *up=NULL;
797 |
798 | printf("Supported file protocols:\n"
799 | "I.. = Input supported\n"
800 | ".O. = Output supported\n"
801 | "..S = Seek supported\n"
802 | "FLAGS NAME\n"
803 | "----- \n");
804 | while((up = av_protocol_next(up)))
805 | printf("%c%c%c %s\n",
806 | up->url_read ? 'I' : '.',
807 | up->url_write ? 'O' : '.',
808 | up->url_seek ? 'S' : '.',
809 | up->name);
810 | }
811 |
812 | void show_filters(void)
813 | {
814 | AVFilter av_unused(**filter) = NULL;
815 |
816 | printf("Filters:\n");
817 | #if CONFIG_AVFILTER
818 | while ((filter = av_filter_next(filter)) && *filter)
819 | printf("%-16s %s\n", (*filter)->name, (*filter)->description);
820 | #endif
821 | }
822 |
823 | void show_pix_fmts(void)
824 | {
825 | enum PixelFormat pix_fmt;
826 |
827 | printf(
828 | "Pixel formats:\n"
829 | "I.... = Supported Input format for conversion\n"
830 | ".O... = Supported Output format for conversion\n"
831 | "..H.. = Hardware accelerated format\n"
832 | "...P. = Paletted format\n"
833 | "....B = Bitstream format\n"
834 | "FLAGS NAME NB_COMPONENTS BITS_PER_PIXEL\n"
835 | "-----\n");
836 |
837 | #if !CONFIG_SWSCALE
838 | # define sws_isSupportedInput(x) 0
839 | # define sws_isSupportedOutput(x) 0
840 | #endif
841 |
842 | for (pix_fmt = 0; pix_fmt < PIX_FMT_NB; pix_fmt++) {
843 | const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
844 | printf("%c%c%c%c%c %-16s %d %2d\n",
845 | sws_isSupportedInput (pix_fmt) ? 'I' : '.',
846 | sws_isSupportedOutput(pix_fmt) ? 'O' : '.',
847 | pix_desc->flags & PIX_FMT_HWACCEL ? 'H' : '.',
848 | pix_desc->flags & PIX_FMT_PAL ? 'P' : '.',
849 | pix_desc->flags & PIX_FMT_BITSTREAM ? 'B' : '.',
850 | pix_desc->name,
851 | pix_desc->nb_components,
852 | av_get_bits_per_pixel(pix_desc));
853 | }
854 | }
855 |
856 | int read_yesno(void)
857 | {
858 | int c = getchar();
859 | int yesno = (toupper(c) == 'Y');
860 |
861 | while (c != '\n' && c != EOF)
862 | c = getchar();
863 |
864 | return yesno;
865 | }
866 |
867 | int read_file(const char *filename, char **bufptr, size_t *size)
868 | {
869 | FILE *f = fopen(filename, "rb");
870 |
871 | if (!f) {
872 | LOGE( "Cannot read file '%s': %s\n", filename, strerror(errno));
873 | return AVERROR(errno);
874 | }
875 | fseek(f, 0, SEEK_END);
876 | *size = ftell(f);
877 | fseek(f, 0, SEEK_SET);
878 | *bufptr = av_malloc(*size + 1);
879 | if (!*bufptr) {
880 | LOGE( "Could not allocate file buffer\n");
881 | fclose(f);
882 | return AVERROR(ENOMEM);
883 | }
884 | fread(*bufptr, 1, *size, f);
885 | (*bufptr)[*size++] = '\0';
886 |
887 | fclose(f);
888 | return 0;
889 | }
890 |
891 | FILE *get_preset_file(char *filename, size_t filename_size,
892 | const char *preset_name, int is_path, const char *codec_name)
893 | {
894 | FILE *f = NULL;
895 | int i;
896 | const char *base[3]= { getenv("FFMPEG_DATADIR"),
897 | getenv("HOME"),
898 | FFMPEG_DATADIR,
899 | };
900 |
901 | if (is_path) {
902 | av_strlcpy(filename, preset_name, filename_size);
903 | f = fopen(filename, "r");
904 | } else {
905 | #ifdef _WIN32
906 | char datadir[MAX_PATH], *ls;
907 | base[2] = NULL;
908 |
909 | if (GetModuleFileNameA(GetModuleHandleA(NULL), datadir, sizeof(datadir) - 1))
910 | {
911 | for (ls = datadir; ls < datadir + strlen(datadir); ls++)
912 | if (*ls == '\\') *ls = '/';
913 |
914 | if (ls = strrchr(datadir, '/'))
915 | {
916 | *ls = 0;
917 | strncat(datadir, "/ffpresets", sizeof(datadir) - 1 - strlen(datadir));
918 | base[2] = datadir;
919 | }
920 | }
921 | #endif
922 | for (i = 0; i < 3 && !f; i++) {
923 | if (!base[i])
924 | continue;
925 | snprintf(filename, filename_size, "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
926 | f = fopen(filename, "r");
927 | if (!f && codec_name) {
928 | snprintf(filename, filename_size,
929 | "%s%s/%s-%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
930 | f = fopen(filename, "r");
931 | }
932 | }
933 | }
934 |
935 | return f;
936 | }
937 |
--------------------------------------------------------------------------------
/Project/jni/videokit/logjam.h:
--------------------------------------------------------------------------------
1 | #ifndef LOGJAM_H
2 | #define LOGJAM_H
3 |
4 | #include
5 |
6 | #define LOGTAG "Videokit"
7 |
8 | #define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOGTAG, __VA_ARGS__)
9 | #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , LOGTAG, __VA_ARGS__)
10 | #define LOGI(...) __android_log_print(ANDROID_LOG_INFO , LOGTAG, __VA_ARGS__)
11 | #define LOGW(...) __android_log_print(ANDROID_LOG_WARN , LOGTAG, __VA_ARGS__)
12 | #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR , LOGTAG, __VA_ARGS__)
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/Project/jni/videokit/uk_co_halfninja_videokit_Videokit.c:
--------------------------------------------------------------------------------
1 |
2 |
3 | #include
4 | #include "logjam.h"
5 | #include "uk_co_halfninja_videokit_Videokit.h"
6 |
7 | #include
8 | #include
9 |
10 | bool initted = false;
11 |
12 | int main(int argc, char **argv);
13 |
14 | static JavaVM *sVm;
15 |
16 | JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved)
17 | {
18 | LOGI("Loading native library compiled at " __TIME__ " " __DATE__);
19 | sVm = jvm;
20 | return JNI_VERSION_1_2;
21 | }
22 |
23 | // the fuck is this exit shit doing
24 | #define exit exit_function_not_allowed
25 | #define LOG_ERROR(message) __android_log_write(ANDROID_LOG_ERROR, "VideoKit", message)
26 | #define LOG_INFO(message) __android_log_write(ANDROID_LOG_INFO, "VideoKit", message)
27 | #define EXCEPTION_CODE 256
28 |
29 | int throwException(JNIEnv *env, const char* message)
30 | {
31 | jclass newExcCls;
32 | (*env)->ExceptionDescribe(env);
33 | (*env)->ExceptionClear(env);
34 | newExcCls = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
35 | (*env)->ThrowNew(env, newExcCls, message);
36 | (*env)->DeleteLocalRef(env, newExcCls);
37 | return EXCEPTION_CODE;
38 | }
39 |
40 |
41 | JNIEXPORT void JNICALL Java_uk_co_halfninja_videokit_Videokit_run(JNIEnv *env, jobject obj, jobjectArray args)
42 | {
43 | LOGD("run() called");
44 | int i = 0;
45 | int argc = 0;
46 | char **argv = NULL;
47 |
48 | if (args != NULL) {
49 | argc = (*env)->GetArrayLength(env, args);
50 | argv = (char **) malloc(sizeof(char *) * argc);
51 |
52 | for(i=0;iGetObjectArrayElement(env, args, i);
55 | argv[i] = (char *)(*env)->GetStringUTFChars(env, str, NULL);
56 | }
57 | }
58 |
59 | LOGD("run passing off to main()");
60 | main(argc, argv);
61 | }
62 |
63 | #if 0
64 | JNIEXPORT void JNICALL Java_uk_co_halfninja_videokit_Videokit_initialise(JNIEnv *env, jobject self)
65 | {
66 | if (!initted)
67 | {
68 | LOG_INFO("Initialising VideoKit");
69 | av_register_all();
70 | initted = true;
71 | }
72 | else
73 | {
74 | LOG_INFO("Already initialised Videokit, ignoring init()");
75 | }
76 | }
77 |
78 | JNIEXPORT void JNICALL Java_uk_co_halfninja_videokit_Videokit_setSize (JNIEnv *env, jobject self, jstring size)
79 | {
80 | LOG_INFO("Let's throw an exception!");
81 | throwException(env, "Bam, not supported");
82 | }
83 |
84 | #endif
85 |
86 |
--------------------------------------------------------------------------------
/Project/jni/videokit/uk_co_halfninja_videokit_Videokit.h:
--------------------------------------------------------------------------------
1 | /* DO NOT EDIT THIS FILE - it is machine generated */
2 | #include
3 | /* Header for class uk_co_halfninja_videokit_Videokit */
4 |
5 | #ifndef _Included_uk_co_halfninja_videokit_Videokit
6 | #define _Included_uk_co_halfninja_videokit_Videokit
7 | #ifdef __cplusplus
8 | extern "C" {
9 | #endif
10 | /*
11 | * Class: uk_co_halfninja_videokit_Videokit
12 | * Method: run
13 | * Signature: ([Ljava/lang/String;)V
14 | */
15 | JNIEXPORT void JNICALL Java_uk_co_halfninja_videokit_Videokit_run
16 | (JNIEnv *, jobject, jobjectArray);
17 |
18 | #ifdef __cplusplus
19 | }
20 | #endif
21 | #endif
22 |
--------------------------------------------------------------------------------
/Project/proguard.cfg:
--------------------------------------------------------------------------------
1 | -optimizationpasses 5
2 | -dontusemixedcaseclassnames
3 | -dontskipnonpubliclibraryclasses
4 | -dontpreverify
5 | -verbose
6 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
7 |
8 | -keep public class * extends android.app.Activity
9 | -keep public class * extends android.app.Application
10 | -keep public class * extends android.app.Service
11 | -keep public class * extends android.content.BroadcastReceiver
12 | -keep public class * extends android.content.ContentProvider
13 | -keep public class * extends android.app.backup.BackupAgentHelper
14 | -keep public class * extends android.preference.Preference
15 | -keep public class com.android.vending.licensing.ILicensingService
16 |
17 | -keepclasseswithmembernames class * {
18 | native ;
19 | }
20 |
21 | -keepclasseswithmembers class * {
22 | public (android.content.Context, android.util.AttributeSet);
23 | }
24 |
25 | -keepclasseswithmembers class * {
26 | public (android.content.Context, android.util.AttributeSet, int);
27 | }
28 |
29 | -keepclassmembers class * extends android.app.Activity {
30 | public void *(android.view.View);
31 | }
32 |
33 | -keepclassmembers enum * {
34 | public static **[] values();
35 | public static ** valueOf(java.lang.String);
36 | }
37 |
38 | -keep class * implements android.os.Parcelable {
39 | public static final android.os.Parcelable$Creator *;
40 | }
41 |
--------------------------------------------------------------------------------
/Project/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system use,
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 |
10 | # Project target.
11 | target=android-8
12 | android.library=true
13 |
--------------------------------------------------------------------------------
/Project/res/drawable-hdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andynicholson/android-ffmpeg-x264/ff4e5379f442116dc1a8abadad0db98cb097d9b6/Project/res/drawable-hdpi/icon.png
--------------------------------------------------------------------------------
/Project/res/drawable-ldpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andynicholson/android-ffmpeg-x264/ff4e5379f442116dc1a8abadad0db98cb097d9b6/Project/res/drawable-ldpi/icon.png
--------------------------------------------------------------------------------
/Project/res/drawable-mdpi/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andynicholson/android-ffmpeg-x264/ff4e5379f442116dc1a8abadad0db98cb097d9b6/Project/res/drawable-mdpi/icon.png
--------------------------------------------------------------------------------
/Project/res/layout/main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/Project/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | VideoKit-Vidiom
4 |
5 |
--------------------------------------------------------------------------------
/Project/src/uk/co/halfninja/videokit/Videokit.java:
--------------------------------------------------------------------------------
1 | package uk.co.halfninja.videokit;
2 |
3 | public final class Videokit {
4 |
5 | static {
6 | System.loadLibrary("videokit");
7 | }
8 |
9 | public native void run(String[] args);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/ProjectTest/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/ProjectTest/.gitignore:
--------------------------------------------------------------------------------
1 | bin
2 | gen
3 | libs
4 | obj
5 | local.properties
6 |
--------------------------------------------------------------------------------
/ProjectTest/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ProjectTest
4 |
5 |
6 | Project
7 |
8 |
9 |
10 | com.android.ide.eclipse.adt.ResourceManagerBuilder
11 |
12 |
13 |
14 |
15 | com.android.ide.eclipse.adt.PreCompilerBuilder
16 |
17 |
18 |
19 |
20 | org.eclipse.jdt.core.javabuilder
21 |
22 |
23 |
24 |
25 | com.android.ide.eclipse.adt.ApkBuilder
26 |
27 |
28 |
29 |
30 |
31 | com.android.ide.eclipse.adt.AndroidNature
32 | org.eclipse.jdt.core.javanature
33 |
34 |
35 |
--------------------------------------------------------------------------------
/ProjectTest/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
13 |
14 |
15 |
20 |
23 |
24 |
--------------------------------------------------------------------------------
/ProjectTest/assets/image.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/andynicholson/android-ffmpeg-x264/ff4e5379f442116dc1a8abadad0db98cb097d9b6/ProjectTest/assets/image.jpg
--------------------------------------------------------------------------------
/ProjectTest/build.properties:
--------------------------------------------------------------------------------
1 | # This file is used to override default values used by the Ant build system.
2 | #
3 | # This file must be checked in Version Control Systems, as it is
4 | # integral to the build system of your project.
5 |
6 | # This file is only used by the Ant script.
7 |
8 | # You can use this to override default values such as
9 | # 'source.dir' for the location of your java source folder and
10 | # 'out.dir' for the location of your output folder.
11 |
12 | # You can also use it define how the release builds are signed by declaring
13 | # the following properties:
14 | # 'key.store' for the location of your keystore and
15 | # 'key.alias' for the name of the key to use.
16 | # The password will be asked during the build when you use the 'release' target.
17 |
18 | tested.project.dir=../Project
19 |
--------------------------------------------------------------------------------
/ProjectTest/build.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
8 |
9 |
10 |
27 |
28 |
29 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
42 |
54 |
55 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/ProjectTest/proguard.cfg:
--------------------------------------------------------------------------------
1 | -optimizationpasses 5
2 | -dontusemixedcaseclassnames
3 | -dontskipnonpubliclibraryclasses
4 | -dontpreverify
5 | -verbose
6 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
7 |
8 | -keep public class * extends android.app.Activity
9 | -keep public class * extends android.app.Application
10 | -keep public class * extends android.app.Service
11 | -keep public class * extends android.content.BroadcastReceiver
12 | -keep public class * extends android.content.ContentProvider
13 | -keep public class * extends android.app.backup.BackupAgentHelper
14 | -keep public class * extends android.preference.Preference
15 | -keep public class com.android.vending.licensing.ILicensingService
16 |
17 | -keepclasseswithmembernames class * {
18 | native ;
19 | }
20 |
21 | -keepclasseswithmembers class * {
22 | public (android.content.Context, android.util.AttributeSet);
23 | }
24 |
25 | -keepclasseswithmembers class * {
26 | public (android.content.Context, android.util.AttributeSet, int);
27 | }
28 |
29 | -keepclassmembers class * extends android.app.Activity {
30 | public void *(android.view.View);
31 | }
32 |
33 | -keepclassmembers enum * {
34 | public static **[] values();
35 | public static ** valueOf(java.lang.String);
36 | }
37 |
38 | -keep class * implements android.os.Parcelable {
39 | public static final android.os.Parcelable$Creator *;
40 | }
41 |
--------------------------------------------------------------------------------
/ProjectTest/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system use,
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 |
10 | # Project target.
11 | target=android-10
12 |
--------------------------------------------------------------------------------
/ProjectTest/src/uk/co/halfninja/videokit/VideokitTest.java:
--------------------------------------------------------------------------------
1 | package uk.co.halfninja.videokit;
2 |
3 | import java.io.BufferedOutputStream;
4 | import java.io.File;
5 | import java.io.FileOutputStream;
6 | import java.io.InputStream;
7 |
8 | import android.os.Environment;
9 | import android.test.AndroidTestCase;
10 | import android.test.InstrumentationTestCase;
11 | import android.util.Log;
12 |
13 | public class VideokitTest extends InstrumentationTestCase {
14 |
15 | Videokit vk = new Videokit();
16 |
17 | public VideokitTest() {
18 | }
19 |
20 |
21 | public void testEncode() throws Exception {
22 | File images = new File(Environment.getExternalStorageDirectory(), "fun");
23 | images.mkdirs();
24 |
25 | for (int i=0; i<10; i++) {
26 | String filename = String.format("snap%04d.jpg", i);
27 | File dest = new File(images, filename);
28 | Log.i("Test", "Adding image at " + dest.getAbsolutePath());
29 | InputStream is = getInstrumentation().getContext().getAssets().open("image.jpg");
30 | BufferedOutputStream o = null;
31 | try {
32 | byte[] buff = new byte[10000];
33 | int read = -1;
34 | o = new BufferedOutputStream(new FileOutputStream(dest), 10000);
35 | while ((read = is.read(buff)) > -1) {
36 | o.write(buff, 0, read);
37 | }
38 | } finally {
39 | is.close();
40 | if (o != null) o.close();
41 |
42 | }
43 | }
44 |
45 |
46 | String filename = "explode.mp4";
47 | File dest = new File(images, filename);
48 | Log.i("Test", "Adding image at " + dest.getAbsolutePath());
49 | InputStream is = getInstrumentation().getContext().getAssets().open("orig.mp4");
50 | BufferedOutputStream o = null;
51 | try {
52 | byte[] buff = new byte[10000];
53 | int read = -1;
54 | o = new BufferedOutputStream(new FileOutputStream(dest), 10000);
55 | while ((read = is.read(buff)) > -1) {
56 | o.write(buff, 0, read);
57 | }
58 | } finally {
59 | is.close();
60 | if (o != null) o.close();
61 |
62 | }
63 |
64 | //videokit.initialise();
65 |
66 |
67 |
68 |
69 | File file = new File(images.getAbsolutePath(), "explode.mp4");
70 | assertTrue("File exist", file.exists());
71 |
72 | String input = file.getAbsolutePath();
73 | Log.i("Test", "Let's set input to " + input);
74 | // videokit.setInputFile(input);
75 | String output = new File(Environment.getExternalStorageDirectory(), "video.3gp").getAbsolutePath();
76 | Log.i("Test", "Let's set output to " + output);
77 | // videokit.setOutputFile(output);
78 |
79 | vk.run(new String[]{
80 | "ffmpeg",
81 | "-i", input,
82 | //"-s" ,"480x320"
83 | //"-vcodec" ,"mpeg4" ,"-ac" ,"1" ,"-ar", "16000" ,"-r" ,"13" ,"-ab", "32000", "-y"
84 |
85 | "-ss", "00:00:05.00"
86 | ,"-t" , "00:00:06.00"
87 | ,"-vcodec", "copy"
88 | ,"-acodec" ,"copy"
89 | , "-y", output
90 | });
91 |
92 | // videokit.setSize(640,480);
93 | // videokit.setFrameRate(5);
94 | //
95 | // videokit.encode();
96 | }
97 |
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/README.textile:
--------------------------------------------------------------------------------
1 | h1. Read me first (that's why I'm at the top!)
2 |
3 | I'm not going to be working on this at all for the forseeable future, and I won't have time to answer questions about why it doesn't compile on your platform or has something missing.
4 |
5 | h1. Android Videokit
6 |
7 | This is a repository to make it relatively simple to fetch and build the latest FFMPEG and libx264 to run on Android, using the Android NDK. It differs from most of the other NDK FFMPEG building packages in that it uses configure and make to build the libraries, and only a very small Android.mk file to pack it into a shared library, rather than a large handful of custom @Android.mk@ scripts (there are still a few script files but most of them are pretty small or are lists of configure options). Big custom @Android.mk@ files tend to break as soon as any files move around in the FFMPEG project, whereas here it should keep working with the latest libraries.
8 |
9 | There is a skeletal JNI interface - the version here simply supplies run(String[]) which is passed to ffmpeg.c's origin main() function. Once I've got this properly encoding again, I'll make some more convenient methods.
10 |
11 | h2. How to build it
12 |
13 | You'll need to git clone in order to have access to the submodules. You might be able to use a ZIP download but then you'll have to skip the init-submodules step and acquire FFmpeg and x264 from elsewhere; if you do this and something doesn't work then I really can't help you. Use git, it's good!
14 |
15 | First time stuff:
16 |
17 | # Run @./init-submodules.sh@ to pull in the FFMPEG and libx264 submodules.
18 | # @cd Project/jni@
19 | # do @export NDK=~/apps/android-ndk-r5c@ using the actual path of your Android NDK.
20 | # Run @./create_toolchain.sh@ to install a local copy of the standalone toolchain
21 |
22 | Each time the 3rd party libraries change:
23 |
24 | # Run @./config_make_everything.sh@ to configure and make libx264 and FFMPEG.
25 | # @ndk-build@ (make sure the NDK is in your $PATH)
26 | # If all is well, you should find @libs/armeabi/videokit.so@.
27 |
28 | You can edit @jni/videokit/jni_interface.c@ to do whatever you want with the built libraries - if you make any changes to it, just run ndk-build to link it together with the static libraries.
29 |
30 | h2. Options
31 |
32 | * @minimal_featureset@ - on by default - only compiles in a small number of codecs (specifically, JPEG decoding and x264 encoding). Change to 0 in @settings.sh@ if you want everything, or just add whatever configure flags you like to pick the codecs and things that are useful to you.
33 | * If you have already created a standalone toolchain, just edit @settings.sh@ to make the PATH point at its bin directory. Then you don't need to run @create_toolchain.sh@. If you have compilation problems, it's worth making a fresh toolchain in case you're using a slightly older version.
34 |
35 | h2. Updating submodules
36 |
37 | The FFMPEG and X264 submodules will initially be fetched from a specific commit. If you want to try building with the latest, just go into each of those submodules and do a `git pull origin master`. FFMPEG tends to change quite a lot so don't be surprised if it fails to build. If you get it working again with the latest, I'll gladly patch my project with your changes.
38 |
--------------------------------------------------------------------------------
/init-submodules.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | ls -A `dirname $0`/jni/ffmpeg/* 2>&1 1>/dev/null
4 |
5 | if [ $? == 0 ]; then
6 | echo "ffmpeg directory isn't empty. Have you already done this?"
7 | exit 1
8 | fi
9 |
10 |
11 | git submodule init
12 | git config -l
13 | git submodule update
14 |
15 |
--------------------------------------------------------------------------------