Giter Club home page Giter Club logo

alphaovervideo's Introduction

AlphaOverVideo

iOS alpha channel video player library implemented on top of Metal

Overview

This project implements a high performance interface to real-time rendering of alpha channel video as well as regular (opaque) video. The render layer is implemented using MetalKit and makes use of Metal shaders to decode and rescale video with the maximum possible performance on modern 64 bit iOS devices. As of iOS 12, the minimum device requirements are a 64 bit device that supports Metal. The MetalKit framework is leveraged to support rendering directly into a MTKView which can then be managed the same as any other UIView. Seamless looping and seamless transition from one video clip to another is supported.

Status

H.264 video encoded with BT.709 gamma as well as an advanced new sRGB gamma are supported. Note that BT.601 encoding is explicitly not supported, the long ongoing nightmare of broken or mis-detected BT.601 vs BT.709 encoding parameters must end!

Decoding Speed

The Metal implementation renders YCbCr data as RGB pixels and is able to execute quickly enough to run full speed at 30 FPS, even on the first 64 bit A7 devices! On an A8 iPhone device both RGB+A mixing and video rescaling executes in under 2 ms.

Implementation

See examples for source code that creates player objects with 24 BPP or 32 BPP videos.

CarSpinAlpha : Example iOS app, displays seamless looping car over changing backgrounds (Swift)

AlienEscape : Example iOS app, displays alpha channel video over starfield background. (Swift)

Fireworks : Example iOS app, display multiple alpha channel fireworks videos at the same time. (Objc)

Bandersnatch : Example iOS app, display video and audio in a short parody of Black Mirror episode. (Swift)

Bloom : Example iOS app, load super high def video from sliced Asset Catalog. (Objc)

AlphaOverVideo : Framework Implementation source code, example iOS and MacOSX app, tests

Encoding

To encode your own video, compile the srgb_to_bt709 command line target and use it to convert a series of PNG images to a .y4m file.

The following command line encodes a 24 BPP video with an Apple specific BT.709 gamma curve.

$ srgb_to_bt709 -gamma apple -frames F0001.png -fps 30 Example.y4m

Then encode with ffmpeg+x264 using the scripts in the FFMPEG directory. The following command line uses the default crf quality setting of 23 and the BT.709 specific script.

$ ext_ffmpeg_encode_bt709_crf.sh Example.y4m Example.m4v 23

To create an alpha channel video, use 32 BPP input PNG images and pass -alpha 1 on the command line. Note that the gamma setting is always srgb when creating alpha channel video.

$ srgb_to_bt709 -alpha 1 -frames F0001.png -fps 30 ExampleAlpha.y4m

An alpha channel video is written as ExampleAlpha.y4m and also a second file named Example_alpha.y4m is written. The RGB pixels are premultiplied and written with srgb gamma. The ALPHA pixels are written with linear gamma. Two encoding scripts invocations are needed to encode to M4V container format videos.

$ ext_ffmpeg_encode_srgb_crf.sh Example.y4m Example.m4v 23

$ ext_ffmpeg_encode_linear_crf.sh Example_alpha.y4m Example_alpha.m4v 23

The large temporary .y4m files can be deleted once compressed H.264 files have been encoded.

One would typically want to increase the crf value for more compression (smaller file size). The "right" crf level is subjective and depends on the input video. Useful cry ranges are typically 20 to 35. The more lossy, the smaller the output file, but the more the visual quality is reduced.

The H.264 files (as .m4v container format) can be played in QuicktimeX player.

Attach the output of this encoding process to the iOS application bundle, so that the files can be loaded in an iOS app.

alphaovervideo's People

Contributors

mdejong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

alphaovervideo's Issues

error convert y4m to m4v bt709

After i'm convert png sequence with command
srgb_to_bt709 -alpha 1 -frames F0001.png -fps 30 LightDance.y4m
I convert y4m to m4v use :
ext_ffmpeg_encode_srgb_crf.sh LightDance.y4m LightDance.m4v 23

Has issue :
ffmpeg -y -i LightDance.y4m -c:v libx264 -pix_fmt yuv420p -preset:v slow -profile:v main -tune:v animation -crf 23 -color_primaries bt709 -color_trc iec61966_2_1 -colorspace bt709 LightDance.m4v
ffmpeg version 1.2.3 Copyright (c) 2000-2013 the FFmpeg developers
built on Aug 29 2013 00:13:55 with llvm-gcc 4.2.1 (LLVM build 2336.11.00)
configuration: --prefix=/Users/mo/Development/FFMpegMac/install --arch=x86_64 --as=yasm --disable-shared --enable-static --enable-gpl --enable-pthreads --enable-version3 --enable-libx264 --enable-avfilter --enable-filters --enable-runtime-cpudetect
libavutil 52. 18.100 / 52. 18.100
libavcodec 54. 92.100 / 54. 92.100
libavformat 54. 63.104 / 54. 63.104
libavdevice 54. 3.103 / 54. 3.103
libavfilter 3. 42.103 / 3. 42.103
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
[yuv4mpegpipe @ 0x7fc3fa815c00] Estimating duration from bitrate, this may be inaccurate
Input #0, yuv4mpegpipe, from 'LightDance.y4m':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 1024x1024, SAR 1:1 DAR 1:1, 24 tbr, 24 tbn, 24 tbc
[NULL @ 0x7fc3fa826e00] [Eval @ 0x7ffedfd9ad60] Undefined constant or missing '(' in 'bt709'
[NULL @ 0x7fc3fa826e00] Unable to parse option value "bt709"
[NULL @ 0x7fc3fa826e00] Error setting option color_primaries to value bt709.
Output #0, ipod, to 'LightDance.m4v':
Stream #0:0: Video: none, yuv420p, 1024x1024 [SAR 1:1 DAR 1:1], q=-1--1, 90k tbn, 24 tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo -> libx264)
Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

Playback under MacOSX can fail with window is background

When a window that is playing ends up in the background, the playback logic will sometime fail and nil is returned for each frame. This can result in the next loop not being preloaded since the current time is not reported properly. Need to figure out how to deal with case of failure to return a frame while still executing next clip load logic even if itemTime is not valid.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.