diff --git a/CMakeLists.txt b/CMakeLists.txt
index 877ef0f..74da1be 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -26,6 +26,10 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(EVDEV REQUIRED libevdev)
pkg_check_modules(AVAHI REQUIRED avahi-client)
pkg_check_modules(UDEV REQUIRED libudev)
+pkg_check_modules(SDL sdl)
+pkg_check_modules(AVCODEC libavcodec)
+pkg_check_modules(AVUTIL libavutil)
+pkg_check_modules(SWSCALE libswscale)
if(BROADCOM_FOUND)
aux_source_directory(./ilclient SRC_LIST)
@@ -39,6 +43,11 @@ if(FREESCALE_FOUND)
list(APPEND MOONLIGHT_DEFINITIONS HAVE_IMX)
endif()
+if (AVCODEC_FOUND AND AVUTIL_FOUND AND SWSCALE_FOUND AND SDL_FOUND)
+ list(APPEND SRC_LIST ./src/video/ffmpeg.c ./src/video/sdl.c)
+ list(APPEND MOONLIGHT_DEFINITIONS HAVE_SDL)
+endif()
+
add_executable(moonlight ${SRC_LIST})
set_property(TARGET moonlight PROPERTY C_STANDARD 11)
@@ -53,6 +62,11 @@ if(FREESCALE_FOUND)
target_link_libraries (moonlight PUBLIC ${FREESCALE_LIBRARIES})
endif()
+if (AVCODEC_FOUND AND AVUTIL_FOUND AND SWSCALE_FOUND AND SDL_FOUND)
+ include_directories(${SDL_INCLUDE_DIRS} ${AVCODEC_INCLUDE_DIRS} ${AVUTIL_INCLUDE_DIRS} ${SWSCALE_INCLUDE_DIRS})
+ target_link_libraries(moonlight PUBLIC ${SDL_LIBRARIES} ${AVCODEC_LIBRARIES} ${AVUTIL_LIBRARIES} ${SWSCALE_LIBRARIES})
+endif()
+
set_property(TARGET moonlight PROPERTY COMPILE_DEFINITIONS ${MOONLIGHT_DEFINITIONS})
include_directories(./moonlight-common-c ${OPUS_INCLUDE_DIRS} ${EVDEV_INCLUDE_DIRS} ${AVAHI_INCLUDE_DIRS} ${UDEV_INCLUDE_DIRS})
target_link_libraries (moonlight PUBLIC ${CMAKE_THREAD_LIBS_INIT} ${CURL_LIBRARIES} ${OPENSSL_LIBRARIES} ${EXPAT_LIBRARIES} ${EVDEV_LIBRARIES} ${ALSA_LIBRARY} ${OPUS_LIBRARY} ${AVAHI_LIBRARIES} ${UDEV_LIBRARIES} ${CMAKE_DL_LIBS})
diff --git a/src/video.c b/src/video.c
index 530ebf1..857a945 100644
--- a/src/video.c
+++ b/src/video.c
@@ -32,7 +32,11 @@ char* decoder_output_name;
static int decoder_level;
void video_init() {
+ #ifdef HAVE_SDL
+ decoder_callbacks = &decoder_callbacks_sdl;
+ #else
decoder_callbacks = &decoder_callbacks_fake;
+ #endif
#ifdef HAVE_OMX
if (dlsym(RTLD_DEFAULT, "bcm_host_init") != NULL) {
decoder_callbacks = &decoder_callbacks_omx;
diff --git a/src/video.h b/src/video.h
index bd6ca69..169aa45 100644
--- a/src/video.h
+++ b/src/video.h
@@ -24,6 +24,9 @@ extern DECODER_RENDERER_CALLBACKS *decoder_callbacks;
void video_init();
extern DECODER_RENDERER_CALLBACKS decoder_callbacks_fake;
+#ifdef HAVE_SDL
+extern DECODER_RENDERER_CALLBACKS decoder_callbacks_sdl;
+#endif
#ifdef HAVE_OMX
extern DECODER_RENDERER_CALLBACKS decoder_callbacks_omx;
#endif
diff --git a/src/video/ffmpeg.c b/src/video/ffmpeg.c
new file mode 100644
index 0000000..29087f5
--- /dev/null
+++ b/src/video/ffmpeg.c
@@ -0,0 +1,160 @@
+/*
+ * This file is part of Moonlight Embedded.
+ *
+ * Based on Moonlight Pc implementation
+ *
+ * Moonlight is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Moonlight is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Moonlight; if not, see .
+ */
+
+#include "ffmpeg.h"
+
+#include
+#include
+#include
+#include
+
+// General decoder and renderer state
+static AVPacket pkt;
+static AVCodec* decoder;
+static AVCodecContext* decoder_ctx;
+static AVFrame* dec_frame;
+static struct SwsContext* scaler_ctx;
+
+#define BYTES_PER_PIXEL 4
+
+// This function must be called before
+// any other decoding functions
+int ffmpeg_init(int width, int height, int perf_lvl, int thread_count) {
+ // Initialize the avcodec library and register codecs
+ av_log_set_level(AV_LOG_QUIET);
+ avcodec_register_all();
+
+ av_init_packet(&pkt);
+
+ decoder = avcodec_find_decoder(AV_CODEC_ID_H264);
+ if (decoder == NULL) {
+ printf("Couldn't find H264 decoder");
+ return -1;
+ }
+
+ decoder_ctx = avcodec_alloc_context3(decoder);
+ if (decoder_ctx == NULL) {
+ printf("Couldn't allocate context");
+ return -1;
+ }
+
+ if (perf_lvl & DISABLE_LOOP_FILTER)
+ // Skip the loop filter for performance reasons
+ decoder_ctx->skip_loop_filter = AVDISCARD_ALL;
+
+ if (perf_lvl & LOW_LATENCY_DECODE)
+ // Use low delay single threaded encoding
+ decoder_ctx->flags |= CODEC_FLAG_LOW_DELAY;
+
+ if (perf_lvl & SLICE_THREADING)
+ decoder_ctx->thread_type = FF_THREAD_SLICE;
+ else
+ decoder_ctx->thread_type = FF_THREAD_FRAME;
+
+ decoder_ctx->thread_count = thread_count;
+
+ decoder_ctx->width = width;
+ decoder_ctx->height = height;
+ decoder_ctx->pix_fmt = PIX_FMT_YUV420P;
+
+ int err = avcodec_open2(decoder_ctx, decoder, NULL);
+ if (err < 0) {
+ printf("Couldn't open codec");
+ return err;
+ }
+
+ dec_frame = av_frame_alloc();
+ if (dec_frame == NULL) {
+ printf("Couldn't allocate frame");
+ return -1;
+ }
+
+ int filtering;
+ if (perf_lvl & FAST_BILINEAR_FILTERING)
+ filtering = SWS_FAST_BILINEAR;
+ else if (perf_lvl & BILINEAR_FILTERING)
+ filtering = SWS_BILINEAR;
+ else
+ filtering = SWS_BICUBIC;
+
+ scaler_ctx = sws_getContext(decoder_ctx->width, decoder_ctx->height, decoder_ctx->pix_fmt, decoder_ctx->width, decoder_ctx->height, PIX_FMT_YUV420P, filtering, NULL, NULL, NULL);
+ if (scaler_ctx == NULL) {
+ printf("Couldn't get scaler context");
+ return -1;
+ }
+
+ return 0;
+}
+
+// This function must be called after
+// decoding is finished
+void ffmpeg_destroy(void) {
+ if (decoder_ctx) {
+ avcodec_close(decoder_ctx);
+ av_free(decoder_ctx);
+ decoder_ctx = NULL;
+ }
+ if (scaler_ctx) {
+ sws_freeContext(scaler_ctx);
+ scaler_ctx = NULL;
+ }
+ if (dec_frame) {
+ av_frame_free(&dec_frame);
+ dec_frame = NULL;
+ }
+}
+
+int ffmpeg_draw_frame(AVPicture pict) {
+ int err = sws_scale(scaler_ctx, (const uint8_t* const*) dec_frame->data, dec_frame->linesize, 0, decoder_ctx->height, pict.data, pict.linesize);
+
+ if (err != decoder_ctx->height) {
+ printf("Scaling failed");
+ return 0;
+ }
+
+ return 1;
+}
+
+// packets must be decoded in order
+// indata must be inlen + FF_INPUT_BUFFER_PADDING_SIZE in length
+int ffmpeg_decode(unsigned char* indata, int inlen) {
+ int err;
+ int got_pic = 0;
+
+ pkt.data = indata;
+ pkt.size = inlen;
+
+ while (pkt.size > 0) {
+ got_pic = 0;
+ err = avcodec_decode_video2(decoder_ctx, dec_frame, &got_pic, &pkt);
+ if (err < 0) {
+ printf("Decode failed");
+ got_pic = 0;
+ break;
+ }
+
+ pkt.size -= err;
+ pkt.data += err;
+ }
+
+ if (got_pic)
+ return 1;
+
+ return err < 0 ? err : 0;
+}
diff --git a/src/video/ffmpeg.h b/src/video/ffmpeg.h
new file mode 100644
index 0000000..160eab1
--- /dev/null
+++ b/src/video/ffmpeg.h
@@ -0,0 +1,39 @@
+/*
+ * This file is part of Moonlight Embedded.
+ *
+ * Based on Moonlight Pc implementation
+ *
+ * Moonlight is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Moonlight is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Moonlight; if not, see .
+ */
+
+#include
+
+// Disables the deblocking filter at the cost of image quality
+#define DISABLE_LOOP_FILTER 0x1
+// Uses the low latency decode flag (disables multithreading)
+#define LOW_LATENCY_DECODE 0x2
+// Threads process each slice, rather than each frame
+#define SLICE_THREADING 0x4
+// Uses nonstandard speedup tricks
+#define FAST_DECODE 0x8
+// Uses bilinear filtering instead of bicubic
+#define BILINEAR_FILTERING 0x10
+// Uses a faster bilinear filtering with lower image quality
+#define FAST_BILINEAR_FILTERING 0x20
+
+int ffmpeg_init(int width, int height, int perf_lvl, int thread_count);
+void ffmpeg_destroy(void);
+
+int ffmpeg_draw_frame(AVPicture pict);
+int ffmpeg_decode(unsigned char* indata, int inlen);
diff --git a/src/video/sdl.c b/src/video/sdl.c
new file mode 100644
index 0000000..73df010
--- /dev/null
+++ b/src/video/sdl.c
@@ -0,0 +1,116 @@
+/*
+ * This file is part of Moonlight Embedded.
+ *
+ * Copyright (C) 2015 Iwan Timmer
+ *
+ * Moonlight is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Moonlight is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Moonlight; if not, see .
+ */
+
+#include "ffmpeg.h"
+
+#include "limelight-common/Limelight.h"
+
+#include
+#include
+
+#define DECODER_BUFFER_SIZE 92*1024
+
+static SDL_Surface *screen;
+static SDL_Overlay *bmp = NULL;
+static int screen_width, screen_height;
+static char* ffmpeg_buffer;
+
+static void sdl_setup(int width, int height, int redrawRate, void* context, int drFlags) {
+ if(SDL_Init(SDL_INIT_VIDEO)) {
+ fprintf(stderr, "Could not initialize SDL - %s\n", SDL_GetError());
+ exit(1);
+ }
+
+ screen = SDL_SetVideoMode(width, height, 0, 0);
+ if(!screen) {
+ fprintf(stderr, "SDL: could not set video mode - exiting\n");
+ exit(1);
+ }
+
+ bmp = SDL_CreateYUVOverlay(width, height, SDL_YV12_OVERLAY, screen);
+
+ int avc_flags = FAST_BILINEAR_FILTERING;
+ if (ffmpeg_init(width, height, 2, avc_flags) < 0) {
+ fprintf(stderr, "Couldn't initialize video decoding\n");
+ exit(1);
+ }
+
+ ffmpeg_buffer = malloc(DECODER_BUFFER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
+ if (ffmpeg_buffer == NULL) {
+ fprintf(stderr, "Not enough memory\n");
+ exit(1);
+ }
+
+ screen_width = width;
+ screen_height = height;
+}
+
+static void sdl_release() {
+ ffmpeg_destroy();
+}
+
+static int sdl_submit_decode_unit(PDECODE_UNIT decodeUnit) {
+ if (decodeUnit->fullLength < DECODER_BUFFER_SIZE) {
+ PLENTRY entry = decodeUnit->bufferList;
+ int length = 0;
+ while (entry != NULL) {
+ memcpy(ffmpeg_buffer+length, entry->data, entry->length);
+ length += entry->length;
+ entry = entry->next;
+ }
+
+ int ret = ffmpeg_decode(ffmpeg_buffer, length);
+ if (ret == 1) {
+ SDL_LockYUVOverlay(bmp);
+
+ AVPicture pict;
+ pict.data[0] = bmp->pixels[0];
+ pict.data[1] = bmp->pixels[2];
+ pict.data[2] = bmp->pixels[1];
+
+ pict.linesize[0] = bmp->pitches[0];
+ pict.linesize[1] = bmp->pitches[2];
+ pict.linesize[2] = bmp->pitches[1];
+
+ ffmpeg_draw_frame(pict);
+
+ SDL_UnlockYUVOverlay(bmp);
+
+ SDL_Rect rect;
+ rect.x = 0;
+ rect.y = 0;
+ rect.w = screen_width;
+ rect.h = screen_height;
+ SDL_DisplayYUVOverlay(bmp, &rect);
+ }
+ } else {
+ fprintf(stderr, "Video decode buffer too small");
+ exit(1);
+ }
+
+ return DR_OK;
+}
+
+DECODER_RENDERER_CALLBACKS decoder_callbacks_sdl = {
+ .setup = sdl_setup,
+ .start = NULL,
+ .stop = NULL,
+ .release = sdl_release,
+ .submitDecodeUnit = sdl_submit_decode_unit,
+};