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.iOS.Graphics.Video
11{
12 // ReSharper disable InconsistentNaming
13 public unsafe class IOSVideoDecoder : VideoDecoder
14 {
15 private const string dll_name = "__Internal";
16
17 [DllImport(dll_name)]
18 private static extern AVFrame* av_frame_alloc();
19
20 [DllImport(dll_name)]
21 private static extern void av_frame_free(AVFrame** frame);
22
23 [DllImport(dll_name)]
24 private static extern void av_frame_unref(AVFrame* frame);
25
26 [DllImport(dll_name)]
27 private static extern int av_frame_get_buffer(AVFrame* frame, int align);
28
29 [DllImport(dll_name)]
30 private static extern byte* av_strdup(string s);
31
32 [DllImport(dll_name)]
33 private static extern void* av_malloc(ulong size);
34
35 [DllImport(dll_name)]
36 private static extern AVPacket* av_packet_alloc();
37
38 [DllImport(dll_name)]
39 private static extern void av_packet_unref(AVPacket* pkt);
40
41 [DllImport(dll_name)]
42 private static extern void av_packet_free(AVPacket** pkt);
43
44 [DllImport(dll_name)]
45 private static extern int av_read_frame(AVFormatContext* s, AVPacket* pkt);
46
47 [DllImport(dll_name)]
48 private static extern int av_seek_frame(AVFormatContext* s, int stream_index, long timestamp, int flags);
49
50 [DllImport(dll_name)]
51 private static extern AVCodec* avcodec_find_decoder(AVCodecID id);
52
53 [DllImport(dll_name)]
54 private static extern int avcodec_open2(AVCodecContext* avctx, AVCodec* codec, AVDictionary** options);
55
56 [DllImport(dll_name)]
57 private static extern int avcodec_receive_frame(AVCodecContext* avctx, AVFrame* frame);
58
59 [DllImport(dll_name)]
60 private static extern int avcodec_send_packet(AVCodecContext* avctx, AVPacket* avpkt);
61
62 [DllImport(dll_name)]
63 private static extern AVFormatContext* avformat_alloc_context();
64
65 [DllImport(dll_name)]
66 private static extern void avformat_close_input(AVFormatContext** s);
67
68 [DllImport(dll_name)]
69 private static extern int avformat_find_stream_info(AVFormatContext* ic, AVDictionary** options);
70
71 [DllImport(dll_name)]
72 private static extern int avformat_open_input(AVFormatContext** ps, [MarshalAs((UnmanagedType)48)] string url, AVInputFormat* fmt, AVDictionary** options);
73
74 [DllImport(dll_name)]
75 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);
76
77 [DllImport(dll_name)]
78 private static extern void sws_freeContext(SwsContext* swsContext);
79
80 [DllImport(dll_name)]
81 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);
82
83 [DllImport(dll_name)]
84 private static extern int sws_scale(SwsContext* c, byte*[] srcSlice, int[] srcStride, int srcSliceY, int srcSliceH, byte*[] dst, int[] dstStride);
85
86 public IOSVideoDecoder(string filename)
87 : base(filename)
88 {
89 }
90
91 public IOSVideoDecoder(Stream videoStream)
92 : base(videoStream)
93 {
94 }
95
96 protected override FFmpegFuncs CreateFuncs() => new FFmpegFuncs
97 {
98 av_frame_alloc = av_frame_alloc,
99 av_frame_free = av_frame_free,
100 av_frame_unref = av_frame_unref,
101 av_frame_get_buffer = av_frame_get_buffer,
102 av_strdup = av_strdup,
103 av_malloc = av_malloc,
104 av_packet_alloc = av_packet_alloc,
105 av_packet_unref = av_packet_unref,
106 av_packet_free = av_packet_free,
107 av_read_frame = av_read_frame,
108 av_seek_frame = av_seek_frame,
109 avcodec_find_decoder = avcodec_find_decoder,
110 avcodec_open2 = avcodec_open2,
111 avcodec_receive_frame = avcodec_receive_frame,
112 avcodec_send_packet = avcodec_send_packet,
113 avformat_alloc_context = avformat_alloc_context,
114 avformat_close_input = avformat_close_input,
115 avformat_find_stream_info = avformat_find_stream_info,
116 avformat_open_input = avformat_open_input,
117 avio_alloc_context = avio_alloc_context,
118 sws_freeContext = sws_freeContext,
119 sws_getContext = sws_getContext,
120 sws_scale = sws_scale
121 };
122 }
123}