Recherche avancée

Médias (0)

Mot : - Tags -/presse-papier

Aucun média correspondant à vos critères n’est disponible sur le site.

Autres articles (7)

  • Emballe médias : à quoi cela sert ?

    4 février 2011, par

    Ce plugin vise à gérer des sites de mise en ligne de documents de tous types.
    Il crée des "médias", à savoir : un "média" est un article au sens SPIP créé automatiquement lors du téléversement d’un document qu’il soit audio, vidéo, image ou textuel ; un seul document ne peut être lié à un article dit "média" ;

  • Publier sur MédiaSpip

    13 juin 2013

    Puis-je poster des contenus à partir d’une tablette Ipad ?
    Oui, si votre Médiaspip installé est à la version 0.2 ou supérieure. Contacter au besoin l’administrateur de votre MédiaSpip pour le savoir

  • Menus personnalisés

    14 novembre 2010, par

    MediaSPIP utilise le plugin Menus pour gérer plusieurs menus configurables pour la navigation.
    Cela permet de laisser aux administrateurs de canaux la possibilité de configurer finement ces menus.
    Menus créés à l’initialisation du site
    Par défaut trois menus sont créés automatiquement à l’initialisation du site : Le menu principal ; Identifiant : barrenav ; Ce menu s’insère en général en haut de la page après le bloc d’entête, son identifiant le rend compatible avec les squelettes basés sur Zpip ; (...)

Sur d’autres sites (4336)

  • Developing A Shader-Based Video Codec

    22 juin 2013, par Multimedia Mike — Outlandish Brainstorms

    Early last month, this thing called ORBX.js was in the news. It ostensibly has something to do with streaming video and codec technology, which naturally catches my interest. The hype was kicked off by Mozilla honcho Brendan Eich when he posted an article asserting that HD video decoding could be entirely performed in JavaScript. We’ve seen this kind of thing before using Broadway– an H.264 decoder implemented entirely in JS. But that exposes some very obvious limitations (notably CPU usage).

    But this new video codec promises 1080p HD playback directly in JavaScript which is a lofty claim. How could it possibly do this ? I got the impression that performance was achieved using WebGL, an extension which allows JavaScript access to accelerated 3D graphics hardware. Browsing through the conversations surrounding the ORBX.js announcement, I found this confirmation from Eich himself :

    You’re right that WebGL does heavy lifting.

    As of this writing, ORBX.js remains some kind of private tech demo. If there were a public demo available, it would necessarily be easy to reverse engineer the downloadable JavaScript decoder.

    But the announcement was enough to make me wonder how it could be possible to create a video codec which effectively leverages 3D hardware.

    Prior Art
    In theorizing about this, it continually occurs to me that I can’t possibly be the first person to attempt to do this (or the ORBX.js people, for that matter). In googling on the matter, I found various forums and Q&A posts where people asked if it were possible to, e.g., accelerate JPEG decoding and presentation using 3D hardware, with no answers. I also found a blog post which describes a plan to use 3D hardware to accelerate VP8 video decoding. It was a project done under the banner of Google’s Summer of Code in 2011, though I’m not sure which open source group mentored the effort. The project did not end up producing the shader-based VP8 codec originally chartered but mentions that “The ‘client side’ of the VP8 VDPAU implementation is working and is currently being reviewed by the libvdpau maintainers.” I’m not sure what that means. Perhaps it includes modifications to the public API that supports VP8, but is waiting for the underlying hardware to actually implement VP8 decoding blocks in hardware.

    What’s So Hard About This ?
    Video decoding is a computationally intensive task. GPUs are known to be really awesome at chewing through computationally intensive tasks. So why aren’t GPUs a natural fit for decoding video codecs ?

    Generally, it boils down to parallelism, or lack of opportunities thereof. GPUs are really good at doing the exact same operations over lots of data at once. The problem is that decoding compressed video usually requires multiple phases that cannot be parallelized, and the individual phases often cannot be parallelized. In strictly mathematical terms, a compressed data stream will need to be decoded by applying a function f(x) over each data element, x0 .. xn. However, the function relies on having applied the function to the previous data element, i.e. :

    f(xn) = f(f(xn-1))
    

    What happens when you try to parallelize such an algorithm ? Temporal rifts in the space/time continuum, if you’re in a Star Trek episode. If you’re in the real world, you’ll get incorrect, unusuable data as the parallel computation is seeded with a bunch of invalid data at multiple points (which is illustrated in some of the pictures in the aforementioned blog post about accelerated VP8).

    Example : JPEG
    Let’s take a very general look at the various stages involved in decoding the ubiquitous JPEG format :


    High level JPEG decoding flow

    What are the opportunities to parallelize these various phases ?

    • Huffman decoding (run length decoding and zig-zag reordering is assumed to be rolled into this phase) : not many opportunities for parallelizing the various Huffman formats out there, including this one. Decoding most Huffman streams is necessarily a sequential operation. I once hypothesized that it would be possible to engineer a codec to achieve some parallelism during the entropy decoding phase, and later found that On2′s VP8 codec employs the scheme. However, such a scheme is unlikely to break down to such a fine level that WebGL would require.
    • Reverse DC prediction : JPEG — and many other codecs — doesn’t store full DC coefficients. It stores differences in successive DC coefficients. Reversing this process can’t be parallelized. See the discussion in the previous section.
    • Dequantize coefficients : This could be very parallelized. It should be noted that software decoders often don’t dequantize all coefficients. Many coefficients are 0 and it’s a waste of a multiplication operation to dequantize. Thus, this phase is sometimes rolled into the Huffman decoding phase.
    • Invert discrete cosine transform : This seems like it could be highly parallelizable. I will be exploring this further in this post.
    • Convert YUV -> RGB for final display : This is a well-established use case for 3D acceleration.

    Crash Course in 3D Shaders and Humility
    So I wanted to see if I could accelerate some parts of JPEG decoding using something called shaders. I made an effort to understand 3D programming and its associated math throughout the 1990s but 3D technology left me behind a very long time ago while I got mixed up in this multimedia stuff. So I plowed through a few books concerning WebGL (thanks to my new Safari Books Online subscription). After I learned enough about WebGL/JS to be dangerous and just enough about shader programming to be absolutely lethal, I set out to try my hand at optimizing IDCT using shaders.

    Here’s my extremely high level (and probably hopelessly naive) view of the modern GPU shader programming model :


    Basic WebGL rendering pipeline

    The WebGL program written in JavaScript drives the show. It sends a set of vertices into the WebGL system and each vertex is processed through a vertex shader. Then, each pixel that falls within a set of vertices is sent through a fragment shader to compute the final pixel attributes (R, G, B, and alpha value). Another consideration is textures : This is data that the program uploads to GPU memory which can be accessed programmatically by the shaders).

    These shaders (vertex and fragment) are key to the GPU’s programmability. How are they programmed ? Using a special C-like shading language. Thought I : “C-like language ? I know C ! I should be able to master this in short order !” So I charged forward with my assumptions and proceeded to get smacked down repeatedly by the overall programming paradigm. I came to recognize this as a variation of the scientific method : Develop a hypothesis– in my case, a mental model of how the system works ; develop an experiment (short program) to prove or disprove the model ; realize something fundamental that I was overlooking ; formulate new hypothesis and repeat.

    First Approach : Vertex Workhorse
    My first pitch goes like this :

    • Upload DCT coefficients to GPU memory in the form of textures
    • Program a vertex mesh that encapsulates 16×16 macroblocks
    • Distribute the IDCT effort among multiple vertex shaders
    • Pass transformed Y, U, and V blocks to fragment shader which will convert the samples to RGB

    So the idea is that decoding of 16×16 macroblocks is parallelized. A macroblock embodies 6 blocks :


    JPEG macroblocks

    It would be nice to process one of these 6 blocks in each vertex. But that means drawing a square with 6 vertices. How do you do that ? I eventually realized that drawing a square with 6 vertices is the recommended method for drawing a square on 3D hardware. Using 2 triangles, each with 3 vertices (0, 1, 2 ; 3, 4, 5) :


    2 triangles make a square

    A vertex shader knows which (x, y) coordinates it has been assigned, so it could figure out which sections of coefficients it needs to access within the textures. But how would a vertex shader know which of the 6 blocks it should process ? Solution : Misappropriate the vertex’s z coordinate. It’s not used for anything else in this case.

    So I set all of that up. Then I hit a new roadblock : How to get the reconstructed Y, U, and V samples transported to the fragment shader ? I have found that communicating between shaders is quite difficult. Texture memory ? WebGL doesn’t allow shaders to write back to texture memory ; shaders can only read it. The standard way to communicate data from a vertex shader to a fragment shader is to declare variables as “varying”. Up until this point, I knew about varying variables but there was something I didn’t quite understand about them and it nagged at me : If 3 different executions of a vertex shader set 3 different values to a varying variable, what value is passed to the fragment shader ?

    It turns out that the varying variable varies, which means that the GPU passes interpolated values to each fragment shader invocation. This completely destroys this idea.

    Second Idea : Vertex Workhorse, Take 2
    The revised pitch is to work around the interpolation issue by just having each vertex shader invocation performs all 6 block transforms. That seems like a lot of redundant. However, I figured out that I can draw a square with only 4 vertices by arranging them in an ‘N’ pattern and asking WebGL to draw a TRIANGLE_STRIP instead of TRIANGLES. Now it’s only doing the 4x the extra work, and not 6x. GPUs are supposed to be great at this type of work, so it shouldn’t matter, right ?

    I wired up an experiment and then ran into a new problem : While I was able to transform a block (or at least pretend to), and load up a varying array (that wouldn’t vary since all vertex shaders wrote the same values) to transmit to the fragment shader, the fragment shader can’t access specific values within the varying block. To clarify, a WebGL shader can use a constant value — or a value that can be evaluated as a constant at compile time — to index into arrays ; a WebGL shader can not compute an index into an array. Per my reading, this is a WebGL security consideration and the limitation may not be present in other OpenGL(-ES) implementations.

    Not Giving Up Yet : Choking The Fragment Shader
    You might want to be sitting down for this pitch :

    • Vertex shader only interpolates texture coordinates to transmit to fragment shader
    • Fragment shader performs IDCT for a single Y sample, U sample, and V sample
    • Fragment shader converts YUV -> RGB

    Seems straightforward enough. However, that step concerning IDCT for Y, U, and V entails a gargantuan number of operations. When computing the IDCT for an entire block of samples, it’s possible to leverage a lot of redundancy in the math which equates to far fewer overall operations. If you absolutely have to compute each sample individually, for an 8×8 block, that requires 64 multiplication/accumulation (MAC) operations per sample. For 3 color planes, and including a few extra multiplications involved in the RGB conversion, that tallies up to about 200 MACs per pixel. Then there’s the fact that this approach means a 4x redundant operations on the color planes.

    It’s crazy, but I just want to see if it can be done. My approach is to pre-compute a pile of IDCT constants in the JavaScript and transmit them to the fragment shader via uniform variables. For a first order optimization, the IDCT constants are formatted as 4-element vectors. This allows computing 16 dot products rather than 64 individual multiplication/addition operations. Ideally, GPU hardware executes the dot products faster (and there is also the possibility of lining these calculations up as matrices).

    I can report that I actually got a sample correctly transformed using this approach. Just one sample, through. Then I ran into some new problems :

    Problem #1 : Computing sample #1 vs. sample #0 requires a different table of 64 IDCT constants. Okay, so create a long table of 64 * 64 IDCT constants. However, this suffers from the same problem as seen in the previous approach : I can’t dynamically compute the index into this array. What’s the alternative ? Maintain 64 separate named arrays and implement 64 branches, when branching of any kind is ill-advised in shader programming to begin with ? I started to go down this path until I ran into…

    Problem #2 : Shaders can only be so large. 64 * 64 floats (4 bytes each) requires 16 kbytes of data and this well exceeds the amount of shader storage that I can assume is allowed. That brings this path of exploration to a screeching halt.

    Further Brainstorming
    I suppose I could forgo pre-computing the constants and directly compute the IDCT for each sample which would entail lots more multiplications as well as 128 cosine calculations per sample (384 considering all 3 color planes). I’m a little stuck with the transform idea right now. Maybe there are some other transforms I could try.

    Another idea would be vector quantization. What little ORBX.js literature is available indicates that there is a method to allow real-time streaming but that it requires GPU assistance to yield enough horsepower to make it feasible. When I think of such severe asymmetry between compression and decompression, my mind drifts towards VQ algorithms. As I come to understand the benefits and limitations of GPU acceleration, I think I can envision a way that something similar to SVQ1, with its copious, hierarchical vector tables stored as textures, could be implemented using shaders.

    So far, this all pertains to intra-coded video frames. What about opportunities for inter-coded frames ? The only approach that I can envision here is to use WebGL’s readPixels() function to fetch the rasterized frame out of the GPU, and then upload it again as a new texture which a new frame processing pipeline could reference. Whether this idea is plausible would require some profiling.

    Using interframes in such a manner seems to imply that the entire codec would need to operate in RGB space and not YUV.

    Conclusions
    The people behind ORBX.js have apparently figured out a way to create a shader-based video codec. I have yet to even begin to reason out a plausible approach. However, I’m glad I did this exercise since I have finally broken through my ignorance regarding modern GPU shader programming. It’s nice to have a topic like multimedia that allows me a jumping-off point to explore other areas.

  • How do I use native C libraries in Android Studio

    11 mars 2015, par Nicholas

    I created a problem some years back based on https://ikaruga2.wordpress.com/2011/06/15/video-live-wallpaper-part-1/. My project was built in the version of Eclipse provided directly by Google at the time and worked fine with a copy of the compiled ffmpeg libraries created with my app name.

    Now I’m trying to create a new app based on my old app. As Google no longer supports Eclipse I downloaded Android Studio and imported my project. With a few tweaks, I was able to successfully compile the old version of the project. So I modified the name, copied a new set of ".so" files into app\src\main\jniLibs\armeabi (where I assumed they should go) and tried running the application on my phone again with absolutely no other changes.

    The NDK throws no errors. Gradle compiles the file without errors and installs it on my phone. The app appears in my live wallpapers list and I can click it to bring up the preview. But instead of a video appearing I receive and error and logCat reports :

    02-26 21:50:31.164  18757-18757/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ExceptionInInitializerError
           at com.nightscapecreations.anim3free.VideoLiveWallpaper.onSharedPreferenceChanged(VideoLiveWallpaper.java:165)
           at com.nightscapecreations.anim3free.VideoLiveWallpaper.onCreate(VideoLiveWallpaper.java:81)
           at android.app.ActivityThread.handleCreateService(ActivityThread.java:2273)
           at android.app.ActivityThread.access$1600(ActivityThread.java:127)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
           at android.os.Handler.dispatchMessage(Handler.java:99)
           at android.os.Looper.loop(Looper.java:137)
           at android.app.ActivityThread.main(ActivityThread.java:4441)
           at java.lang.reflect.Method.invokeNative(Native Method)
           at java.lang.reflect.Method.invoke(Method.java:511)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
           at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.UnsatisfiedLinkError: Cannot load library: link_image[1936]:   144 could not load needed library '/data/data/com.nightscapecreations.anim1free/lib/libavutil.so' for 'libavcore.so' (load_library[1091]: Library '/data/data/com.nightscapecreations.anim1free/lib/libavutil.so' not found)
           at java.lang.Runtime.loadLibrary(Runtime.java:370)
           at java.lang.System.loadLibrary(System.java:535)
           at com.nightscapecreations.anim3free.NativeCalls.<clinit>(NativeCalls.java:64)
           at com.nightscapecreations.anim3free.VideoLiveWallpaper.onSharedPreferenceChanged(VideoLiveWallpaper.java:165)
           at com.nightscapecreations.anim3free.VideoLiveWallpaper.onCreate(VideoLiveWallpaper.java:81)
           at android.app.ActivityThread.handleCreateService(ActivityThread.java:2273)
           at android.app.ActivityThread.access$1600(ActivityThread.java:127)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
           at android.os.Handler.dispatchMessage(Handler.java:99)
           at android.os.Looper.loop(Looper.java:137)
           at android.app.ActivityThread.main(ActivityThread.java:4441)
           at java.lang.reflect.Method.invokeNative(Native Method)
           at java.lang.reflect.Method.invoke(Method.java:511)
    </clinit>

    I’m a novice Android/Java/C++ developer and am not sure what this error means, but Google leads me to believe that my new libraries are not being found. In my Eclipse project I had this set of libraries in "libs\armeabi", and another copy of them in a more complicated folder structure at "jni\ffmpeg-android\build\ffmpeg\armeabi\lib". Android Studio appears to have kept everything the same, other than renaming "libs" to "jniLibs", but I’m hitting a brick wall with this error and am unsure how to proceed.

    How can I compile this new app with the new name using Android Studio ?

    In case it helps here is my Android.mk file :

       LOCAL_PATH := $(call my-dir)

       include $(CLEAR_VARS)
       MY_LIB_PATH := ffmpeg-android/build/ffmpeg/armeabi/lib
       LOCAL_MODULE := bambuser-libavcore
       LOCAL_SRC_FILES := $(MY_LIB_PATH)/libavcore.so
       include $(PREBUILT_SHARED_LIBRARY)

       include $(CLEAR_VARS)
       LOCAL_MODULE := bambuser-libavformat
       LOCAL_SRC_FILES := $(MY_LIB_PATH)/libavformat.so
       include $(PREBUILT_SHARED_LIBRARY)

       include $(CLEAR_VARS)
       LOCAL_MODULE := bambuser-libavcodec
       LOCAL_SRC_FILES := $(MY_LIB_PATH)/libavcodec.so
       include $(PREBUILT_SHARED_LIBRARY)

       include $(CLEAR_VARS)
       LOCAL_MODULE := bambuser-libavfilter
       LOCAL_SRC_FILES := $(MY_LIB_PATH)/libavfilter.so
       include $(PREBUILT_SHARED_LIBRARY)

       include $(CLEAR_VARS)
       LOCAL_MODULE := bambuser-libavutil
       LOCAL_SRC_FILES := $(MY_LIB_PATH)/libavutil.so
       include $(PREBUILT_SHARED_LIBRARY)

       include $(CLEAR_VARS)
       LOCAL_MODULE := bambuser-libswscale
       LOCAL_SRC_FILES := $(MY_LIB_PATH)/libswscale.so
       include $(PREBUILT_SHARED_LIBRARY)

       #local_PATH := $(call my-dir)

       include $(CLEAR_VARS)

       LOCAL_CFLAGS := -DANDROID_NDK \
                       -DDISABLE_IMPORTGL

       LOCAL_MODULE    := video
       LOCAL_SRC_FILES := video.c

       LOCAL_C_INCLUDES := \
           $(LOCAL_PATH)/include \
           $(LOCAL_PATH)/ffmpeg-android/ffmpeg \
           $(LOCAL_PATH)/freetype/include/freetype2 \
           $(LOCAL_PATH)/freetype/include \
           $(LOCAL_PATH)/ftgl/src \
           $(LOCAL_PATH)/ftgl
       LOCAL_LDLIBS := -L$(NDK_PLATFORMS_ROOT)/$(TARGET_PLATFORM)/arch-arm/usr/lib -L$(LOCAL_PATH) -L$(LOCAL_PATH)/ffmpeg-android/build/ffmpeg/armeabi/lib/ -lGLESv1_CM -ldl -lavformat -lavcodec -lavfilter -lavutil -lswscale -llog -lz -lm

       include $(BUILD_SHARED_LIBRARY)

    And here is my NativeCalls.java :

       package com.nightscapecreations.anim3free;

       public class NativeCalls {
           //ffmpeg
           public static native void initVideo();
           public static native void loadVideo(String fileName); //
           public static native void prepareStorageFrame();
           public static native void getFrame(); //
           public static native void freeConversionStorage();
           public static native void closeVideo();//
           public static native void freeVideo();//
           //opengl
           public static native void initPreOpenGL(); //
           public static native void initOpenGL(); //
           public static native void drawFrame(); //
           public static native void closeOpenGL(); //
           public static native void closePostOpenGL();//
           //wallpaper
           public static native void updateVideoPosition();
           public static native void setSpanVideo(boolean b);
           //getters
           public static native int getVideoHeight();
           public static native int getVideoWidth();
           //setters
           public static native void setWallVideoDimensions(int w,int h);
           public static native void setWallDimensions(int w,int h);
           public static native void setScreenPadding(int w,int h);
           public static native void setVideoMargins(int w,int h);
           public static native void setDrawDimensions(int drawWidth,int drawHeight);
           public static native void setOffsets(int x,int y);
           public static native void setSteps(int xs,int ys);
           public static native void setScreenDimensions(int w, int h);
           public static native void setTextureDimensions(int tx,
                                  int ty );
           public static native void setOrientation(boolean b);
           public static native void setPreviewMode(boolean b);
           public static native void setTonality(int t);
           public static native void toggleGetFrame(boolean b);
           //fps
           public static native void setLoopVideo(boolean b);

           static {
           System.loadLibrary("avcore");
           System.loadLibrary("avformat");
           System.loadLibrary("avcodec");
           //System.loadLibrary("avdevice");
           System.loadLibrary("avfilter");
           System.loadLibrary("avutil");
           System.loadLibrary("swscale");
           System.loadLibrary("video");
           }

       }

    EDIT

    This is the first part of my video.c file :

       #include <gles></gles>gl.h>
       #include <gles></gles>glext.h>

       #include <gles2></gles2>gl2.h>
       #include <gles2></gles2>gl2ext.h>

       #include
       #include

       #include <libavcodec></libavcodec>avcodec.h>
       #include <libavformat></libavformat>avformat.h>
       #include <libswscale></libswscale>swscale.h>

       #include  
       #include  
       #include
       #include <android></android>log.h>

       //#include <ftgl></ftgl>ftgl.h>

       //ffmpeg video variables
       int      initializedVideo=0;
       int      initializedFrame=0;
       AVFormatContext *pFormatCtx=NULL;
       int             videoStream;
       AVCodecContext  *pCodecCtx=NULL;
       AVCodec         *pCodec=NULL;
       AVFrame         *pFrame=NULL;
       AVPacket        packet;
       int             frameFinished;
       float           aspect_ratio;

       //ffmpeg video conversion variables
       AVFrame         *pFrameConverted=NULL;
       int             numBytes;
       uint8_t         *bufferConverted=NULL;

       //opengl
       int textureFormat=PIX_FMT_RGBA; // PIX_FMT_RGBA   PIX_FMT_RGB24
       int GL_colorFormat=GL_RGBA; // Must match the colorspace specified for textureFormat
       int textureWidth=256;
       int textureHeight=256;
       int nTextureHeight=-256;
       int textureL=0, textureR=0, textureW=0;
       int frameTonality;

       //GLuint textureConverted=0;
       GLuint texturesConverted[2] = { 0,1 };
       GLuint dummyTex = 2;
       static int len=0;


       static const char* BWVertexSrc =
                "attribute vec4 InVertex;\n"
                "attribute vec2 InTexCoord0;\n"
                "attribute vec2 InTexCoord1;\n"
                "uniform mat4 ProjectionModelviewMatrix;\n"
                "varying vec2 TexCoord0;\n"
                "varying vec2 TexCoord1;\n"

                "void main()\n"
                "{\n"
                "  gl_Position = ProjectionModelviewMatrix * InVertex;\n"
                "  TexCoord0 = InTexCoord0;\n"
                "  TexCoord1 = InTexCoord1;\n"
                "}\n";
       static const char* BWFragmentSrc  =

                "#version 110\n"
                "uniform sampler2D Texture0;\n"
                "uniform sampler2D Texture1;\n"

                "varying vec2 TexCoord0;\n"
                "varying vec2 TexCoord1;\n"

                "void main()\n"
                "{\n"
               "   vec3 color = texture2D(m_Texture, texCoord).rgb;\n"
               "   float gray = (color.r + color.g + color.b) / 3.0;\n"
               "   vec3 grayscale = vec3(gray);\n"

               "   gl_FragColor = vec4(grayscale, 1.0);\n"
                "}";
       static GLuint shaderProgram;


       //// Create a pixmap font from a TrueType file.
       //FTGLPixmapFont font("/home/user/Arial.ttf");
       //// Set the font size and render a small text.
       //font.FaceSize(72);
       //font.Render("Hello World!");

       //screen dimensions
       int screenWidth = 50;
       int screenHeight= 50;
       int screenL=0, screenR=0, screenW=0;
       int dPaddingX=0,dPaddingY=0;
       int drawWidth=50,drawHeight=50;

       //wallpaper
       int wallWidth = 50;
       int wallHeight = 50;
       int xOffSet, yOffSet;
       int xStep, yStep;
       jboolean spanVideo = JNI_TRUE;

       //video dimensions
       int wallVideoWidth = 0;
       int wallVideoHeight = 0;
       int marginX, marginY;
       jboolean isScreenPortrait = JNI_TRUE;
       jboolean isPreview = JNI_TRUE;
       jboolean loopVideo = JNI_TRUE;
       jboolean isGetFrame = JNI_TRUE;

       //file
       const char * szFileName;

       #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
       #define min( a, b ) ( ((a) &lt; (b)) ? (a) : (b) )

       //test variables
       #define RGBA8(r, g, b)  (((r) &lt;&lt; (24)) | ((g) &lt;&lt; (16)) | ((b) &lt;&lt; (8)) | 255)
       int sPixelsInited=JNI_FALSE;
       uint32_t *s_pixels=NULL;

       int s_pixels_size() {
         return (sizeof(uint32_t) * textureWidth * textureHeight * 5);
       }

       void render_pixels1(uint32_t *pixels, uint32_t c) {
           int x, y;
           /* fill in a square of 5 x 5 at s_x, s_y */
           for (y = 0; y &lt; textureHeight; y++) {
               for (x = 0; x &lt; textureWidth; x++) {
                   int idx = x + y * textureWidth;
                   pixels[idx++] = RGBA8(255, 255, 0);
               }
           }
       }

       void render_pixels2(uint32_t *pixels, uint32_t c) {
           int x, y;
           /* fill in a square of 5 x 5 at s_x, s_y */
           for (y = 0; y &lt; textureHeight; y++) {
               for (x = 0; x &lt; textureWidth; x++) {
                   int idx = x + y * textureWidth;
                   pixels[idx++] = RGBA8(0, 0, 255);
               }
           }
       }

       void Java_com_nightscapecreations_anim3free_NativeCalls_initVideo (JNIEnv * env, jobject this) {
           initializedVideo = 0;
           initializedFrame = 0;
       }

       /* list of things that get loaded: */
       /* buffer */
       /* pFrameConverted */
       /* pFrame */
       /* pCodecCtx */
       /* pFormatCtx */
       void Java_com_nightscapecreations_anim3free_NativeCalls_loadVideo (JNIEnv * env, jobject this, jstring fileName)  {
           jboolean isCopy;
           szFileName = (*env)->GetStringUTFChars(env, fileName, &amp;isCopy);
           //debug
           __android_log_print(ANDROID_LOG_DEBUG, "NDK: ", "NDK:LC: [%s]", szFileName);
           // Register all formats and codecs
           av_register_all();
           // Open video file
           if(av_open_input_file(&amp;pFormatCtx, szFileName, NULL, 0, NULL)!=0) {
           __android_log_print(ANDROID_LOG_DEBUG, "video.c", "NDK: Couldn't open file");
           return;
           }
           __android_log_print(ANDROID_LOG_DEBUG, "video.c", "NDK: Succesfully loaded file");
           // Retrieve stream information */
           if(av_find_stream_info(pFormatCtx)&lt;0) {
           __android_log_print(ANDROID_LOG_DEBUG, "video.c", "NDK: Couldn't find stream information");
           return;
           }
           __android_log_print(ANDROID_LOG_DEBUG, "video.c", "NDK: Found stream info");
           // Find the first video stream
           videoStream=-1;
           int i;
           for(i=0; inb_streams; i++)
               if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
                   videoStream=i;
                   break;
               }
           if(videoStream==-1) {
               __android_log_print(ANDROID_LOG_DEBUG, "video.c", "NDK: Didn't find a video stream");
               return;
           }
           __android_log_print(ANDROID_LOG_DEBUG, "video.c", "NDK: Found video stream");
           // Get a pointer to the codec contetx for the video stream
           pCodecCtx=pFormatCtx->streams[videoStream]->codec;
           // Find the decoder for the video stream
           pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
           if(pCodec==NULL) {
               __android_log_print(ANDROID_LOG_DEBUG, "video.c", "NDK: Unsupported codec");
               return;
           }
           // Open codec
           if(avcodec_open(pCodecCtx, pCodec)&lt;0) {
               __android_log_print(ANDROID_LOG_DEBUG, "video.c", "NDK: Could not open codec");
               return;
           }
           // Allocate video frame (decoded pre-conversion frame)
           pFrame=avcodec_alloc_frame();
           // keep track of initialization
           initializedVideo = 1;
           __android_log_print(ANDROID_LOG_DEBUG, "video.c", "NDK: Finished loading video");
       }

       //for this to work, you need to set the scaled video dimensions first
       void Java_com_nightscapecreations_anim3free_NativeCalls_prepareStorageFrame (JNIEnv * env, jobject this)  {
           // Allocate an AVFrame structure
           pFrameConverted=avcodec_alloc_frame();
           // Determine required buffer size and allocate buffer
           numBytes=avpicture_get_size(textureFormat, textureWidth, textureHeight);
           bufferConverted=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
           if ( pFrameConverted == NULL || bufferConverted == NULL )
               __android_log_print(ANDROID_LOG_DEBUG, "prepareStorage>>>>", "Out of memory");
           // Assign appropriate parts of buffer to image planes in pFrameRGB
           // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
           // of AVPicture
           avpicture_fill((AVPicture *)pFrameConverted, bufferConverted, textureFormat, textureWidth, textureHeight);
           __android_log_print(ANDROID_LOG_DEBUG, "prepareStorage>>>>", "Created frame");
           __android_log_print(ANDROID_LOG_DEBUG, "prepareStorage>>>>", "texture dimensions: %dx%d", textureWidth, textureHeight);
           initializedFrame = 1;
       }

       jint Java_com_nightscapecreations_anim3free_NativeCalls_getVideoWidth (JNIEnv * env, jobject this)  {
           return pCodecCtx->width;
       }

       jint Java_com_nightscapecreations_anim3free_NativeCalls_getVideoHeight (JNIEnv * env, jobject this)  {
           return pCodecCtx->height;
       }

       void Java_com_nightscapecreations_anim3free_NativeCalls_getFrame (JNIEnv * env, jobject this)  {
           // keep reading packets until we hit the end or find a video packet
           while(av_read_frame(pFormatCtx, &amp;packet)>=0) {
               static struct SwsContext *img_convert_ctx;
               // Is this a packet from the video stream?
               if(packet.stream_index==videoStream) {
                   // Decode video frame
                   /* __android_log_print(ANDROID_LOG_DEBUG,  */
                   /*            "video.c",  */
                   /*            "getFrame: Try to decode frame" */
                   /*            ); */
                   avcodec_decode_video(pCodecCtx, pFrame, &amp;frameFinished, packet.data, packet.size);
                   // Did we get a video frame?
                   if(frameFinished) {
                       if(img_convert_ctx == NULL) {
                           /* get/set the scaling context */
                           int w = pCodecCtx->width;
                           int h = pCodecCtx->height;
                           img_convert_ctx = sws_getContext(w, h, pCodecCtx->pix_fmt, textureWidth,textureHeight, textureFormat, SWS_FAST_BILINEAR, NULL, NULL, NULL);
                           if(img_convert_ctx == NULL) {
                               return;
                           }
                       }
                       /* if img convert null */
                       /* finally scale the image */
                       /* __android_log_print(ANDROID_LOG_DEBUG,  */
                       /*          "video.c",  */
                       /*          "getFrame: Try to scale the image" */
                       /*          ); */

                       //pFrameConverted = pFrame;
                       sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameConverted->data, pFrameConverted->linesize);
                       //av_picture_crop(pFrameConverted->data, pFrame->data, 1, pCodecCtx->height, pCodecCtx->width);
                       //av_picture_crop();
                       //avfilter_vf_crop();

                       /* do something with pFrameConverted */
                       /* ... see drawFrame() */
                       /* We found a video frame, did something with it, now free up
                          packet and return */
                       av_free_packet(&amp;packet);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrame.age: %d", pFrame->age);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrame.buffer_hints: %d", pFrame->buffer_hints);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrame.display_picture_number: %d", pFrame->display_picture_number);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrame.hwaccel_picture_private: %d", pFrame->hwaccel_picture_private);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrame.key_frame: %d", pFrame->key_frame);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrame.palette_has_changed: %d", pFrame->palette_has_changed);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrame.pict_type: %d", pFrame->pict_type);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrame.qscale_type: %d", pFrame->qscale_type);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrameConverted.age: %d", pFrameConverted->age);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrameConverted.buffer_hints: %d", pFrameConverted->buffer_hints);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrameConverted.display_picture_number: %d", pFrameConverted->display_picture_number);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrameConverted.hwaccel_picture_private: %d", pFrameConverted->hwaccel_picture_private);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrameConverted.key_frame: %d", pFrameConverted->key_frame);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrameConverted.palette_has_changed: %d", pFrameConverted->palette_has_changed);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrameConverted.pict_type: %d", pFrameConverted->pict_type);
       //              __android_log_print(ANDROID_LOG_INFO, "Droid Debug", "pFrameConverted.qscale_type: %d", pFrameConverted->qscale_type);
                       return;
                   } /* if frame finished */
               } /* if packet video stream */
               // Free the packet that was allocated by av_read_frame
               av_free_packet(&amp;packet);
           } /* while */
           //reload video when you get to the end
           av_seek_frame(pFormatCtx,videoStream,0,AVSEEK_FLAG_ANY);
       }

       void Java_com_nightscapecreations_anim3free_NativeCalls_setLoopVideo (JNIEnv * env, jobject this, jboolean b) {
           loopVideo = b;
       }

       void Java_com_nightscapecreations_anim3free_NativeCalls_closeVideo (JNIEnv * env, jobject this) {
           if ( initializedFrame == 1 ) {
               // Free the converted image
               av_free(bufferConverted);
               av_free(pFrameConverted);
               initializedFrame = 0;
               __android_log_print(ANDROID_LOG_DEBUG, "closeVideo>>>>", "Freed converted image");
           }
           if ( initializedVideo == 1 ) {
               /* // Free the YUV frame */
               av_free(pFrame);
               /* // Close the codec */
               avcodec_close(pCodecCtx);
               // Close the video file
               av_close_input_file(pFormatCtx);
               initializedVideo = 0;
               __android_log_print(ANDROID_LOG_DEBUG, "closeVideo>>>>", "Freed video structures");
           }
       }

       void Java_com_nightscapecreations_anim3free_NativeCalls_freeVideo (JNIEnv * env, jobject this) {
           if ( initializedVideo == 1 ) {
               /* // Free the YUV frame */
               av_free(pFrame);
               /* // Close the codec */
               avcodec_close(pCodecCtx);
               // Close the video file
               av_close_input_file(pFormatCtx);
               __android_log_print(ANDROID_LOG_DEBUG, "closeVideo>>>>", "Freed video structures");
               initializedVideo = 0;
           }
       }

       void Java_com_nightscapecreations_anim3free_NativeCalls_freeConversionStorage (JNIEnv * env, jobject this) {
           if ( initializedFrame == 1 ) {
               // Free the converted image
               av_free(bufferConverted);
               av_freep(pFrameConverted);
               initializedFrame = 0;
           }
       }

       /*--- END OF VIDEO ----*/

       /* disable these capabilities. */
       static GLuint s_disable_options[] = {
           GL_FOG,
           GL_LIGHTING,
           GL_CULL_FACE,
           GL_ALPHA_TEST,
           GL_BLEND,
           GL_COLOR_LOGIC_OP,
           GL_DITHER,
           GL_STENCIL_TEST,
           GL_DEPTH_TEST,
           GL_COLOR_MATERIAL,
           0
       };

       // For stuff that opengl needs to work with,
       // like the bitmap containing the texture
       void Java_com_nightscapecreations_anim3free_NativeCalls_initPreOpenGL (JNIEnv * env, jobject this)  {

       }
       ...
  • trying to make OpenCV 3.2.0 work with virtualenv

    24 juillet 2017, par lollercoaster

    I’m on Ubuntu 16.04 with Python 2.7 and virtualenv & virtualenvwrapper.

    By following this guide I managed to get the following script working with my system Python2.7 which has cv2 globally installed.

    I used this script to install it :

    ######################################
    # INSTALL OPENCV ON UBUNTU OR DEBIAN #
    ######################################

    # |         THIS SCRIPT IS TESTED CORRECTLY ON         |
    # |----------------------------------------------------|
    # | OS             | OpenCV       | Test | Last test   |
    # |----------------|--------------|------|-------------|
    # | Ubuntu 16.04.2 | OpenCV 3.2.0 | OK   | 20 May 2017 |
    # | Debian 8.8     | OpenCV 3.2.0 | OK   | 20 May 2017 |
    # | Debian 9.0     | OpenCV 3.2.0 | OK   | 25 Jun 2017 |

    # 1. KEEP UBUNTU OR DEBIAN UP TO DATE

    sudo apt-get -y update
    sudo apt-get -y upgrade
    sudo apt-get -y dist-upgrade
    sudo apt-get -y autoremove


    # 2. INSTALL THE DEPENDENCIES

    # Build tools:
    sudo apt-get install -y build-essential cmake

    # GUI (if you want to use GTK instead of Qt, replace 'qt5-default' with 'libgtkglext1-dev' and remove '-DWITH_QT=ON' option in CMake):
    sudo apt-get install -y qt5-default libvtk6-dev

    # Media I/O:
    sudo apt-get install -y zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libjasper-dev libopenexr-dev libgdal-dev

    # Video I/O:
    sudo apt-get install -y libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev

    # Parallelism and linear algebra libraries:
    sudo apt-get install -y libtbb-dev libeigen3-dev

    # Python:
    sudo apt-get install -y python-dev python-tk python-numpy python3-dev python3-tk python3-numpy

    # Documentation:
    sudo apt-get install -y doxygen

    # UI stuff
    sudo apt-get install libgtk-3-dev libatlas-base-dev gfortran


    # 3. INSTALL THE LIBRARY (YOU CAN CHANGE '3.2.0' FOR THE LAST STABLE VERSION)
    sudo apt-get install -y unzip wget

    # opencv contrib
    wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip -O opencv_contrib-3.2.0.zip
    unzip opencv_contrib-3.2.0.zip
    rm opencv_contrib-3.2.0.zip

    # opencv
    wget https://github.com/opencv/opencv/archive/3.2.0.zip
    unzip 3.2.0.zip
    rm 3.2.0.zip
    mv opencv-3.2.0 OpenCV-3.2.0
    cd OpenCV-3.2.0

    mkdir build
    cd build
    cmake -D WITH_QT=ON \
       -D WITH_OPENGL=ON \
       -D FORCE_VTK=ON \
       -D WITH_TBB=ON \
       -D WITH_GDAL=ON \
       -D WITH_XINE=ON \
       -D BUILD_EXAMPLES=ON \
       -D INSTALL_PYTHON_EXAMPLES=ON \
       -D ENABLE_PRECOMPILED_HEADERS=OFF \
       -D BUILD_NEW_PYTHON_SUPPORT=ON \
       ..

    make -j4
    sudo make install
    sudo ldconfig


    # 4. EXECUTE SOME OPENCV EXAMPLES AND COMPILE A DEMONSTRATION

    # To complete this step, please visit 'http://milq.github.io/install-opencv-ubuntu-debian'.

    The following script below works great with that system-wide installation :

    import cv2

    img = cv2.imread('some_img.jpg')

    Though this one doesn’t - even the system Python can’t read videos for some reason...

    import cv2

    video_capture = cv2.VideoCapture(0)
    ret, frame = video_capture.read()
    print ret  # always False

    but I want it to work with my virtualenv. So I recompiled OpenCV with :

    cmake -D WITH_QT=ON \
       -D WITH_OPENGL=ON \
       -D FORCE_VTK=ON \
       -D WITH_TBB=ON \
       -D WITH_GDAL=ON \
       -D WITH_XINE=ON \
       -D BUILD_EXAMPLES=ON \
       -D INSTALL_PYTHON_EXAMPLES=ON \
       -D ENABLE_PRECOMPILED_HEADERS=OFF \
       -D BUILD_NEW_PYTHON_SUPPORT=ON \
       -D OPENCV_EXTRA_MODULES_PATH=/home/me/code/myproject/opencv_contrib-3.2.0/modules \
       -D PYTHON_EXECUTABLE=~/.envs/myenv/bin/python \
       ..

    make -j4
    sudo make install
    sudo ldconfig

    Here’s the CMake log :

    -- Found VTK ver. 6.2.0 (usefile: /usr/lib/cmake/vtk-6.2/UseVTK.cmake)
    -- Caffe:   NO
    -- Protobuf:   YES
    -- Glog:   NO
    -- freetype2:   YES
    -- harfbuzz:    YES
    -- Module opencv_sfm disabled because the following dependencies are not found: Glog/Gflags
    -- freetype2:   YES
    -- harfbuzz:    YES
    -- Checking for modules 'tesseract;lept'
    --   No package 'tesseract' found
    --   No package 'lept' found
    -- Tesseract:   NO
    -- Check contents of vgg_generated_48.i ...
    -- Check contents of vgg_generated_64.i ...
    -- Check contents of vgg_generated_80.i ...
    -- Check contents of vgg_generated_120.i ...
    -- Check contents of boostdesc_bgm.i ...
    -- Check contents of boostdesc_bgm_bi.i ...
    -- Check contents of boostdesc_bgm_hd.i ...
    -- Check contents of boostdesc_binboost_064.i ...
    -- Check contents of boostdesc_binboost_128.i ...
    -- Check contents of boostdesc_binboost_256.i ...
    -- Check contents of boostdesc_lbgm.i ...
    --
    -- General configuration for OpenCV 3.2.0 =====================================
    --   Version control:               817bd7b-dirty
    --
    --   Extra modules:
    --     Location (extra):            /home/me/code/myproject/opencv_contrib-3.2.0/modules
    --     Version control (extra):     817bd7b-dirty
    --
    --   Platform:
    --     Timestamp:                   2017-07-20T18:25:26Z
    --     Host:                        Linux 4.8.0-58-generic x86_64
    --     CMake:                       3.5.1
    --     CMake generator:             Unix Makefiles
    --     CMake build tool:            /usr/bin/make
    --     Configuration:               Release
    --
    --   C/C++:
    --     Built as dynamic libs?:      YES
    --     C++ Compiler:                /usr/bin/c++  (ver 5.4.0)
    --     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    --     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    --     C Compiler:                  /usr/bin/cc
    --     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
    --     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -msse -msse2 -mno-avx -msse3 -mno-ssse3 -mno-sse4.1 -mno-sse4.2 -ffunction-sections -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
    --     Linker flags (Release):
    --     Linker flags (Debug):
    --     ccache:                      NO
    --     Precompiled headers:         NO
    --     Extra dependencies:          Qt5::Test Qt5::Concurrent Qt5::OpenGL /usr/lib/x86_64-linux-gnu/libwebp.so /usr/lib/x86_64-linux-gnu/libjasper.so /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so /usr/lib/libgdal.so dc1394 xine avcodec-ffmpeg avformat-ffmpeg avutil-ffmpeg swscale-ffmpeg Qt5::Core Qt5::Gui Qt5::Widgets /usr/lib/x86_64-linux-gnu/hdf5/serial/lib/libhdf5.so /usr/lib/x86_64-linux-gnu/libpthread.so /usr/lib/x86_64-linux-gnu/libsz.so /usr/lib/x86_64-linux-gnu/libdl.so /usr/lib/x86_64-linux-gnu/libm.so vtkRenderingOpenGL vtkImagingHybrid vtkIOImage vtkCommonDataModel vtkCommonMath vtkCommonCore vtksys vtkCommonMisc vtkCommonSystem vtkCommonTransforms vtkCommonExecutionModel vtkDICOMParser vtkIOCore /usr/lib/x86_64-linux-gnu/libz.so vtkmetaio /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/x86_64-linux-gnu/libtiff.so vtkImagingCore vtkRenderingCore vtkCommonColor vtkFiltersExtraction vtkFiltersCore vtkFiltersGeneral vtkCommonComputationalGeometry vtkFiltersStatistics vtkImagingFourier vtkalglib vtkFiltersGeometry vtkFiltersSources vtkInteractionStyle vtkRenderingLOD vtkFiltersModeling vtkIOPLY vtkIOGeometry /usr/lib/x86_64-linux-gnu/libjsoncpp.so vtkFiltersTexture vtkRenderingFreeType /usr/lib/x86_64-linux-gnu/libfreetype.so vtkftgl vtkIOExport vtkRenderingAnnotation vtkImagingColor vtkRenderingContext2D vtkRenderingGL2PS vtkRenderingContextOpenGL /usr/lib/libgl2ps.so vtkRenderingLabel dl m pthread rt /usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGL.so tbb
    --     3rdparty dependencies:       libprotobuf
    --
    --   OpenCV modules:
    --     To be built:                 core flann hdf imgproc ml photo reg surface_matching video viz dnn freetype fuzzy imgcodecs shape videoio highgui objdetect plot superres ts xobjdetect xphoto bgsegm bioinspired dpm face features2d line_descriptor saliency text calib3d ccalib cvv datasets rgbd stereo tracking videostab xfeatures2d ximgproc aruco optflow phase_unwrapping stitching structured_light java python2 python3
    --     Disabled:                    world contrib_world
    --     Disabled by dependency:      -
    --     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cnn_3dobj matlab sfm
    --
    --   GUI:
    --     QT 5.x:                      YES (ver 5.5.1)
    --     QT OpenGL support:           YES (Qt5::OpenGL 5.5.1)
    --     OpenGL support:              YES (/usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib/x86_64-linux-gnu/libGL.so)
    --     VTK support:                 YES (ver 6.2.0)
    --
    --   Media I/O:
    --     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.8)
    --     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver )
    --     WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver encoder: 0x0202)
    --     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.2.54)
    --     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 - 4.0.6)
    --     JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
    --     OpenEXR:                     /usr/lib/x86_64-linux-gnu/libImath.so /usr/lib/x86_64-linux-gnu/libIlmImf.so /usr/lib/x86_64-linux-gnu/libIex.so /usr/lib/x86_64-linux-gnu/libHalf.so /usr/lib/x86_64-linux-gnu/libIlmThread.so (ver 2.2.0)
    --     GDAL:                        /usr/lib/libgdal.so
    --     GDCM:                        NO
    --
    --   Video I/O:
    --     DC1394 1.x:                  NO
    --     DC1394 2.x:                  YES (ver 2.2.4)
    --     FFMPEG:                      YES
    --       avcodec:                   YES (ver 56.60.100)
    --       avformat:                  YES (ver 56.40.101)
    --       avutil:                    YES (ver 54.31.100)
    --       swscale:                   YES (ver 3.1.101)
    --       avresample:                NO
    --     GStreamer:                   NO
    --     OpenNI:                      NO
    --     OpenNI PrimeSensor Modules:  NO
    --     OpenNI2:                     NO
    --     PvAPI:                       NO
    --     GigEVisionSDK:               NO
    --     Aravis SDK:                  NO
    --     UniCap:                      NO
    --     UniCap ucil:                 NO
    --     V4L/V4L2:                    NO/YES
    --     XIMEA:                       NO
    --     Xine:                        YES (ver 1.2.6)
    --     gPhoto2:                     NO
    --
    --   Parallel framework:            TBB (ver 4.4 interface 9002)
    --
    --   Other third-party libraries:
    --     Use IPP:                     9.0.1 [9.0.1]
    --          at:                     /home/me/code/myproject/OpenCV-3.2.0/build/3rdparty/ippicv/ippicv_lnx
    --     Use IPP Async:               NO
    --     Use VA:                      NO
    --     Use Intel VA-API/OpenCL:     NO
    --     Use Lapack:                  NO
    --     Use Eigen:                   YES (ver 3.2.92)
    --     Use Cuda:                    NO
    --     Use OpenCL:                  YES
    --     Use OpenVX:                  NO
    --     Use custom HAL:              NO
    --
    --   OpenCL:                        <dynamic loading="loading" of="of" opencl="opencl" library="library">
    --     Include path:                /home/me/code/myproject/OpenCV-3.2.0/3rdparty/include/opencl/1.2
    --     Use AMDFFT:                  NO
    --     Use AMDBLAS:                 NO
    --
    --   Python 2:
    --     Interpreter:                 /home/me/.envs/myenv/bin/python (ver 2.7.12)
    --     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12)
    --     numpy:                       /home/me/.envs/myenv/local/lib/python2.7/site-packages/numpy/core/include (ver 1.13.1)
    --     packages path:               lib/python2.7/site-packages
    --
    --   Python 3:
    --     Interpreter:                 /usr/bin/python3 (ver 3.5.2)
    --     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.2)
    --     numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.11.0)
    --     packages path:               lib/python3.5/dist-packages
    --
    --   Python (for build):            /home/me/.envs/myenv/bin/python
    --
    --   Java:
    --     ant:                         /usr/bin/ant (ver 1.9.6)
    --     JNI:                         /usr/lib/jvm/default-java/include /usr/lib/jvm/default-java/include/linux /usr/lib/jvm/default-java/include
    --     Java wrappers:               YES
    --     Java tests:                  YES
    --
    --   Matlab:                        Matlab not found or implicitly disabled
    --
    --   Documentation:
    --     Doxygen:                     /usr/bin/doxygen (ver 1.8.11)
    --
    --   Tests and samples:
    --     Tests:                       YES
    --     Performance tests:           YES
    --     C/C++ Examples:              YES
    --
    --   Install path:                  /usr/local
    --
    --   cvconfig.h is in:              /home/me/code/myproject/OpenCV-3.2.0/build
    -- -----------------------------------------------------------------
    --
    </dynamic>

    Unfortunately, while this works and I can import cv2 in the shell, it cannot read video using the above script, probably due to incorrect compilation or linking of ffmpeg ? The confusing part is the system-wide installation of OpenCV works fine, even without ffmpeg installed !

    What am I doing wrong ? How can I get OpenCV working with a virtualenv ?

    ====

    EDIT : Running the C++ video writing example has this result :

    $ cd /home/me/code/myproject/OpenCV-3.2.0/build/bin
    $ ./cpp-tutorial-video-write ../../samples/data/vtest.avi R Y
    ------------------------------------------------------------------------------
    This program shows how to write video files.
    You can extract the R or G or B color channel of the input video.
    Usage:
    ./video-write  [ R | G | B] [Y | N]
    ------------------------------------------------------------------------------

    OpenCV: FFMPEG: tag 0xffffffff/'����' is not found (format 'avi / AVI (Audio Video Interleaved)')'

    (cpp-tutorial-video-write:19523): GStreamer-CRITICAL **: gst_element_make_from_uri: assertion 'gst_uri_is_valid (uri)' failed
    OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend does not support this codec.) in CvVideoWriter_GStreamer::open, file /home/me/code/myproject/OpenCV-3.2.0/modules/videoio/src/cap_gstreamer.cpp, line 1388
    VIDEOIO(cvCreateVideoWriter_GStreamer(filename, fourcc, fps, frameSize, is_color)): raised OpenCV exception:

    /home/me/code/myproject/OpenCV-3.2.0/modules/videoio/src/cap_gstreamer.cpp:1388: error: (-210) Gstreamer Opencv backend does not support this codec. in function CvVideoWriter_GStreamer::open

    Could not open the output video for write: ../../samples/data/vtest.avi

    And the opencv_test_videoio unit test reports the following : https://pastebin.com/q4mf224Q

    However, running the c++ video starter example DOES work, with the following command and output, I can see the webcam working and streaming video in the highgui interface :

    $ ./cpp-example-videocapture_starter 0
    VIDEOIO ERROR: V4L: device 0: Unable to query number of channels
    (ERROR)icvOpenAVI_XINE(): Unable to initialize video driver.
    GStreamer: Error opening bin: no element "0"
    press space to save a picture. q or esc to quit
    init done
    opengl support available