Recherche avancée

Médias (91)

Autres articles (53)

  • Personnaliser les catégories

    21 juin 2013, par

    Formulaire de création d’une catégorie
    Pour ceux qui connaissent bien SPIP, une catégorie peut être assimilée à une rubrique.
    Dans le cas d’un document de type catégorie, les champs proposés par défaut sont : Texte
    On peut modifier ce formulaire dans la partie :
    Administration > Configuration des masques de formulaire.
    Dans le cas d’un document de type média, les champs non affichés par défaut sont : Descriptif rapide
    Par ailleurs, c’est dans cette partie configuration qu’on peut indiquer le (...)

  • 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" ;

  • Installation en mode ferme

    4 février 2011, par

    Le mode ferme permet d’héberger plusieurs sites de type MediaSPIP en n’installant qu’une seule fois son noyau fonctionnel.
    C’est la méthode que nous utilisons sur cette même plateforme.
    L’utilisation en mode ferme nécessite de connaïtre un peu le mécanisme de SPIP contrairement à la version standalone qui ne nécessite pas réellement de connaissances spécifique puisque l’espace privé habituel de SPIP n’est plus utilisé.
    Dans un premier temps, vous devez avoir installé les mêmes fichiers que l’installation (...)

Sur d’autres sites (7222)

  • Basic Video Palette Conversion

    20 août 2011, par Multimedia Mike — General, Python

    How do you take a 24-bit RGB image and convert it to an 8-bit paletted image for the purpose of compression using a codec that requires 8-bit input images ? Seems simple enough and that’s what I’m tackling in this post.

    Ask FFmpeg/Libav To Do It
    Ideally, FFmpeg / Libav should be able to handle this automatically. Indeed, FFmpeg used to be able to, at least at the time I wrote this post about ZMBV and was unhappy with FFmpeg’s default results. Somewhere along the line, FFmpeg and Libav lost the ability to do this. I suspect it got removed during some swscale refactoring.

    Still, there’s no telling if the old system would have computed palettes correctly for QuickTime files.

    Distance Approach
    When I started writing my SMC video encoder, I needed to convert RGB (from PNG files) to PAL8 colorspace. The path of least resistance was to match the pixels in the input image to the default 256-color palette that QuickTime assumes (and is hardcoded into FFmpeg/Libav).

    How to perform the matching ? Find the palette entry that is closest to a given input pixel, where "closest" is the minimum distance as computed by the usual distance formula (square root of the sum of the squares of the diffs of all the components).



    That means for each pixel in an image, check the pixel against 256 palette entries (early termination is possible if an acceptable threshold is met). As you might imagine, this can be a bit time-consuming. I wondered about a faster approach...

    Lookup Table
    I think this is the approach that FFmpeg used to use, but I went and derived it for myself after studying the default QuickTime palette table. There’s a pattern there— all of the RGB entries are comprised of combinations of 6 values — 0x00, 0x33, 0x66, 0x99, 0xCC, and 0xFF. If you mix and match these for red, green, and blue values, you come up with 6 * 6 * 6 = 216 different colors. This happens to be identical to the web-safe color palette.

    The first (0th) entry in the table is (FF, FF, FF), followed by (FF, FF, CC), (FF, FF, 99), and on down to (FF, FF, 00) when the green component gets knocked down and step and the next color is (FF, CC, FF). The first 36 palette entries in the table all have a red component of 0xFF. Thus, if an input RGB pixel has a red color closest to 0xFF, it must map to one of those first 36 entries.

    I created a table which maps indices 0..215 to values from 5..0. Each of the R, G, and B components of an input pixel are used to index into this table and derive 3 indices ri, gi, and bi. Finally, the index into the palette table is given by :

      index = ri * 36 + gi * 6 + bi
    

    For example, the pixel (0xFE, 0xFE, 0x01) would yield ri, gi, and bi values of 0, 0, and 5. Therefore :

      index = 0 * 36 + 0 * 6 + 5
    

    The palette index is 5, which maps to color (0xFF, 0xFF, 0x00).

    Validation
    So I was pretty pleased with myself for coming up with that. Now, ideally, swapping out one algorithm for another in my SMC encoder should yield identical results. That wasn’t the case, initially.

    One problem is that the regulation QuickTime palette actually has 40 more entries above and beyond the typical 216-entry color cube (rounding out the grand total of 256 colors). Thus, using the distance approach with the full default table provides for a little more accuracy.

    However, there still seems to be a problem. Let’s check our old standby, the Big Buck Bunny logo image :



    Distance approach using the full 256-color QuickTime default palette


    Distance approach using the 216-color palette


    Table lookup approach using the 216-color palette

    I can’t quite account for that big red splotch there. That’s the most notable difference between images 1 and 2 and the only visible difference between images 2 and 3.

    To prove to myself that the distance approach is equivalent to the table approach, I wrote a Python script to iterate through all possible RGB combinations and verify the equivalence. If you’re not up on your base 2 math, that’s 224 or 16,777,216 colors to run through. I used Python’s multiprocessing module to great effect and really maximized a Core i7 CPU with 8 hardware threads.

    So I’m confident that the palette conversion techniques are sound. The red spot is probably attributable to a bug in my WIP SMC encoder.

    Source Code
    Update August 23, 2011 : Here’s the Python code I used for proving equivalence between the 2 approaches. In terms of leveraging multiple CPUs, it’s possibly the best program I have written to date.

    PYTHON :
    1. # !/usr/bin/python
    2.  
    3. from multiprocessing import Pool
    4.  
    5. palette = []
    6. pal8_table = []
    7.  
    8. def process_r(r) :
    9.  counts = []
    10.  
    11.  for i in xrange(216) :
    12.   counts.append(0)
    13.  
    14.  print "r = %d" % (r)
    15.  for g in xrange(256) :
    16.   for b in xrange(256) :
    17.    min_dsqrd = 0xFFFFFFFF
    18.    best_index = 0
    19.    for i in xrange(len(palette)) :
    20.     dr = palette[i][0] - r
    21.     dg = palette[i][1] - g
    22.     db = palette[i][2] - b
    23.     dsqrd = dr * dr + dg * dg + db * db
    24.     if dsqrd <min_dsqrd :
    25.      min_dsqrd = dsqrd
    26.      best_index = i
    27.    counts[best_index] += 1
    28.  
    29.    # check if the distance approach deviates from the table-based approach
    30.    i = best_index
    31.    r = palette[i][0]
    32.    g = palette[i][1]
    33.    b = palette[i][2]
    34.    ri = pal8_table[r]
    35.    gi = pal8_table[g]
    36.    bi = pal8_table[b]
    37.    table_index = ri * 36 + gi * 6 + bi ;
    38.    if table_index != best_index :
    39.     print "(0x%02X 0x%02X 0x%02X) : distance index = %d, table index = %d" % (r, g, b, best_index, table_index)
    40.  
    41.  return counts
    42.  
    43. if __name__ == ’__main__’ :
    44.  counts = []
    45.  for i in xrange(216) :
    46.   counts.append(0)
    47.  
    48.  # initialize reference palette
    49.  color_steps = [ 0xFF, 0xCC, 0x99, 0x66, 0x33, 0x00 ]
    50.  for r in color_steps :
    51.   for g in color_steps :
    52.    for b in color_steps :
    53.     palette.append([r, g, b])
    54.  
    55.  # initialize palette conversion table
    56.  for i in range(0, 26) :
    57.   pal8_table.append(5)
    58.  for i in range(26, 77) :
    59.   pal8_table.append(4)
    60.  for i in range(77, 128) :
    61.   pal8_table.append(3)
    62.  for i in range(128, 179) :
    63.   pal8_table.append(2)
    64.  for i in range(179, 230) :
    65.   pal8_table.append(1)
    66.  for i in range(230, 256) :
    67.   pal8_table.append(0)
    68.  
    69.  # create a pool of worker threads and break up the overall job
    70.  pool = Pool()
    71.  it = pool.imap_unordered(process_r, range(256))
    72.  try :
    73.   while 1 :
    74.    partial_counts = it.next()
    75.    for i in xrange(216) :
    76.     counts[i] += partial_counts[i]
    77.  except StopIteration :
    78.   pass
    79.  
    80.  print "index, count, red, green, blue"
    81.  for i in xrange(len(counts)) :
    82.   print "%d, %d, %d, %d, %d" % (i, counts[i], palette[i][0], palette[i][1], palette[i][2])
  • How do I write to a file in Golang using a pointer to the C data ?

    20 juillet 2020, par nevernew

    I'm writing an app for the windows platform using FFmpeg and it's golang wrapper goav, but I'm having trouble understanding how to use the C pointers to gain access to an array.

    &#xA;&#xA;

    I'm trying to write the frame data, pointed to by a uint8 pointer from C, to a .ppm file in golang.

    &#xA;&#xA;

    Once I have this done, for proof of concept that FFmpeg is doing what I expect it to, I want to set the frames to a texture in OpenGl to make a video player with cool transitions ; any pointers to do that nice and efficiently would be so very helpful ! I'm guessing I need to write some shader code to draw the ppm as a texture...

    &#xA;&#xA;

    The PPM file structure looks pretty simple just the header and then a byte of data for each red, green and blue value of each pixel in the frame from top left to bottom right

    &#xA;&#xA;

    I'm starting to understanding how to cast the pointers between C and Go types, but how can I access the data and write it in Go with the same result as C ? In C I just have to set the pointer offset for the data and state how much of it to write :

    &#xA;&#xA;

    for (y = 0; y &lt; height; y&#x2B;&#x2B;) {&#xA;    fwrite(pFrame->data[0]&#x2B;y*pFrame->linesize[0], 1, width*3, pFile);&#xA;}&#xA;

    &#xA;&#xA;

    I've stripped out all the relevant parts of the C code, the wrapper and my code, shown below :

    &#xA;&#xA;

    C code - libavutil/frame.h

    &#xA;&#xA;

    #include &#xA;&#xA;typedef struct AVFrame {&#xA;#define AV_NUM_DATA_POINTERS 8&#xA;    uint8_t *data[AV_NUM_DATA_POINTERS];&#xA;    int linesize[AV_NUM_DATA_POINTERS];&#xA;}&#xA;

    &#xA;&#xA;

    Golang goav wrapper

    &#xA;&#xA;

    package avutil&#xA;&#xA;/*&#xA;    #cgo pkg-config: libavutil&#xA;    #include <libavutil></libavutil>frame.h>&#xA;    #include &#xA;*/&#xA;import "C"&#xA;import (&#xA;    "unsafe"&#xA;)&#xA;&#xA;type Frame C.struct_AVFrame&#xA;&#xA;func Data(f *Frame) *uint8 {&#xA;    return (*uint8)(unsafe.Pointer((*C.uint8_t)(unsafe.Pointer(&amp;f.data))))&#xA;}&#xA;func Linesize(f *Frame) int {&#xA;    return int(*(*C.int)(unsafe.Pointer(&amp;f.linesize)))&#xA;}&#xA;

    &#xA;&#xA;

    My Golang code

    &#xA;&#xA;

    package main&#xA;&#xA;import "github.com/giorgisio/goav/avutil"&#xA;&#xA;func saveFrame(videoFrame *avutil.Frame, width int, height int, iFrame int) {&#xA;    var szFilename string&#xA;    var y int&#xA;    var file *os.File&#xA;    var err error&#xA;&#xA;    szFilename = ""&#xA;&#xA;    // Open file&#xA;    szFilename = fmt.Sprintf("frame%d.ppm", iFrame)&#xA;&#xA;    if file, err = os.Create(szFilename); err != nil {&#xA;        log.Println("Error Reading")&#xA;    }&#xA;&#xA;    // Write header&#xA;    fh := []byte(fmt.Sprintf("P6\n%d %d\n255\n", width, height))&#xA;    file.Write(fh)&#xA;    var b byte = 0&#xA;    // Write pixel data&#xA;    for y = 0; y &lt; height; y&#x2B;&#x2B; {&#xA;        d := avutil.Data(videoFrame) // d should be a pointer to the first byte of data&#xA;        l := avutil.Linesize(videoFrame)&#xA;&#xA;        // I&#x27;m basically lost trying to figure out how to correctly write&#xA;        // this to a file, the file is created, but when I open it in GIMP&#xA;        // the image is mostly black with glitchy fuzz - so it&#x27;s not being&#xA;        // written properly; the header seems to be ok, it knows the height&#xA;        // and width at least.&#xA;&#xA;        data := make([]byte, l*3)&#xA;&#xA;        ptr := unsafe.Pointer(d)&#xA;        for i := 0; i &lt; l; i&#x2B;&#x2B; {&#xA;            datum := (*uint8)(unsafe.Pointer(uintptr(ptr) &#x2B; (uintptr(i)&#x2B;(uintptr(y)*uintptr(l)))*unsafe.Sizeof(*d)))&#xA;            data = append(data, *datum)&#xA;            //fmt.Println(*datum)&#xA;        }&#xA;&#xA;        n, err := file.Write(data)&#xA;        if err != nil {&#xA;            log.Println("Error Writing:", szFilename, "-", n)&#xA;        }&#xA;    }&#xA;&#xA;    file.Close()&#xA;}&#xA;

    &#xA;&#xA;

    So, how can I write to a file using a pointer to the data, like you can do in C, and get the same result ?

    &#xA;&#xA;

    The first frame should be black so all 0's but I'm getting a glitchy fuzz, so it must be accessing some random data

    &#xA;&#xA;

    Update : My fix using a C function to save :

    &#xA;&#xA;

    package avutil&#xA;&#xA;/*&#xA;    #cgo pkg-config: libavutil&#xA;    #include <libavutil></libavutil>frame.h>&#xA;    #include &#xA;    #include &#xA;&#xA;    void SaveFrame(const char* location, AVFrame *pFrame, int width, int height) {&#xA;        FILE *pFile;&#xA;        int  y;&#xA;&#xA;        // Open file&#xA;        pFile=fopen(location, "wb");&#xA;        if(pFile==NULL)&#xA;            return;&#xA;&#xA;        // Write header&#xA;        fprintf(pFile, "P6\n%d %d\n255\n", width, height);&#xA;&#xA;        // Write pixel data&#xA;        for(y=0; ydata[0]&#x2B;y*pFrame->linesize[0], 1, width*3, pFile);&#xA;&#xA;        // Close file&#xA;        fclose(pFile);&#xA;    }&#xA;    uint8_t* GetData(AVFrame *pFrame) {&#xA;        return pFrame->data[0];&#xA;    }&#xA;*/&#xA;import "C"&#xA;

    &#xA;&#xA;

    I updated the avutil file, in the goav wrapper package, with this save function at the top, then pass it the frame context so it can get the data pointer from it. I also added this Go function to that avutil file to call the C function

    &#xA;&#xA;

    func SaveFrame(location string, f *Frame, width int, height int) {&#xA;    csLoc := C.CString(location)&#xA;    C.SaveFrame(csLoc, (*C.struct_AVFrame)(unsafe.Pointer(f)), C.int(width), C.int(height))&#xA;    C.free(unsafe.Pointer(csLoc))&#xA;}&#xA;

    &#xA;

  • how can I set GOP size to be a multiple of the input framerate ?

    3 septembre 2020, par agconti

    Question :

    &#xA;

    How can I set GOP size to be a multiple of the input framerate ?

    &#xA;

    Context :

    &#xA;

    My ideal GOP size formula is source_fps * 2. My source's framerate can be either 30 or 60 fps so I'm unable to set a static value that works for both. Is there a variable that ffmpeg exposes that I can use ? I know you can get metadata about the source input when using the -vf option with variables like iw or ih. Is there a similar method for FPS ?

    &#xA;

    Example command where GOP size and keyint_min is hardcoded :

    &#xA;

    ffmpeg -i <input /> -keyint_min 120 -g 120 -f dash /path/to/out.mpd&#xA;

    &#xA;

    Desired command where e GOP size and keyint_min are based on the source framerate :

    &#xA;

    ffmpeg -i <input /> -keyint_min source_framerate * 2 -g source_framerate * 2 -f dash /path/to/out.mpd&#xA;

    &#xA;