A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System.IO;
5using System.Runtime.InteropServices;
6using FFmpeg.AutoGen;
7using osu.Framework.Graphics.Video;
8using osu.Framework.Threading;
9
10namespace osu.Framework.Android.Graphics.Video
11{
12 // ReSharper disable InconsistentNaming
13 public unsafe class AndroidVideoDecoder : VideoDecoder
14 {
15 private const string lib_avutil = "libavutil.so";
16 private const string lib_avcodec = "libavcodec.so";
17 private const string lib_avformat = "libavformat.so";
18 private const string lib_swscale = "libswscale.so";
19 private const string lib_avfilter = "libavfilter.so";
20
21 [DllImport(lib_avutil)]
22 private static extern AVFrame* av_frame_alloc();
23
24 [DllImport(lib_avutil)]
25 private static extern void av_frame_free(AVFrame** frame);
26
27 [DllImport(lib_avutil)]
28 private static extern void av_frame_unref(AVFrame* frame);
29
30 [DllImport(lib_avcodec)]
31 private static extern int av_frame_get_buffer(AVFrame* frame, int align);
32
33 [DllImport(lib_avutil)]
34 private static extern byte* av_strdup(string s);
35
36 [DllImport(lib_avutil)]
37 private static extern void* av_malloc(ulong size);
38
39 [DllImport(lib_avcodec)]
40 private static extern AVPacket* av_packet_alloc();
41
42 [DllImport(lib_avcodec)]
43 private static extern void av_packet_unref(AVPacket* pkt);
44
45 [DllImport(lib_avcodec)]
46 private static extern void av_packet_free(AVPacket** pkt);
47
48 [DllImport(lib_avformat)]
49 private static extern int av_read_frame(AVFormatContext* s, AVPacket* pkt);
50
51 [DllImport(lib_avformat)]
52 private static extern int av_seek_frame(AVFormatContext* s, int stream_index, long timestamp, int flags);
53
54 [DllImport(lib_avcodec)]
55 private static extern AVCodec* avcodec_find_decoder(AVCodecID id);
56
57 [DllImport(lib_avcodec)]
58 private static extern int avcodec_open2(AVCodecContext* avctx, AVCodec* codec, AVDictionary** options);
59
60 [DllImport(lib_avcodec)]
61 private static extern int avcodec_receive_frame(AVCodecContext* avctx, AVFrame* frame);
62
63 [DllImport(lib_avcodec)]
64 private static extern int avcodec_send_packet(AVCodecContext* avctx, AVPacket* avpkt);
65
66 [DllImport(lib_avformat)]
67 private static extern AVFormatContext* avformat_alloc_context();
68
69 [DllImport(lib_avformat)]
70 private static extern void avformat_close_input(AVFormatContext** s);
71
72 [DllImport(lib_avformat)]
73 private static extern int avformat_find_stream_info(AVFormatContext* ic, AVDictionary** options);
74
75 [DllImport(lib_avformat)]
76 private static extern int avformat_open_input(AVFormatContext** ps, [MarshalAs((UnmanagedType)48)] string url, AVInputFormat* fmt, AVDictionary** options);
77
78 [DllImport(lib_avformat)]
79 private static extern AVIOContext* avio_alloc_context(byte* buffer, int buffer_size, int write_flag, void* opaque, avio_alloc_context_read_packet_func read_packet, avio_alloc_context_write_packet_func write_packet, avio_alloc_context_seek_func seek);
80
81 [DllImport(lib_swscale)]
82 private static extern void sws_freeContext(SwsContext* swsContext);
83
84 [DllImport(lib_swscale)]
85 private static extern SwsContext* sws_getContext(int srcW, int srcH, AVPixelFormat srcFormat, int dstW, int dstH, AVPixelFormat dstFormat, int flags, SwsFilter* srcFilter, SwsFilter* dstFilter, double* param);
86
87 [DllImport(lib_swscale)]
88 private static extern int sws_scale(SwsContext* c, byte*[] srcSlice, int[] srcStride, int srcSliceY, int srcSliceH, byte*[] dst, int[] dstStride);
89
90 public AndroidVideoDecoder(string filename)
91 : base(filename)
92 {
93 }
94
95 public AndroidVideoDecoder(Stream videoStream)
96 : base(videoStream)
97 {
98 }
99
100 protected override FFmpegFuncs CreateFuncs() => new FFmpegFuncs
101 {
102 av_frame_alloc = av_frame_alloc,
103 av_frame_free = av_frame_free,
104 av_frame_unref = av_frame_unref,
105 av_frame_get_buffer = av_frame_get_buffer,
106 av_strdup = av_strdup,
107 av_malloc = av_malloc,
108 av_packet_alloc = av_packet_alloc,
109 av_packet_unref = av_packet_unref,
110 av_packet_free = av_packet_free,
111 av_read_frame = av_read_frame,
112 av_seek_frame = av_seek_frame,
113 avcodec_find_decoder = avcodec_find_decoder,
114 avcodec_open2 = avcodec_open2,
115 avcodec_receive_frame = avcodec_receive_frame,
116 avcodec_send_packet = avcodec_send_packet,
117 avformat_alloc_context = avformat_alloc_context,
118 avformat_close_input = avformat_close_input,
119 avformat_find_stream_info = avformat_find_stream_info,
120 avformat_open_input = avformat_open_input,
121 avio_alloc_context = avio_alloc_context,
122 sws_freeContext = sws_freeContext,
123 sws_getContext = sws_getContext,
124 sws_scale = sws_scale
125 };
126 }
127}