Módulo para WebM

Started by josebita, May 19, 2010, 06:27:34 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

josebita

Al final, parece que Google ha liberado el codec para competir con H264.
¿Alguien se anima a añadir soporte para Bennu?.

Me aburría y he escrito un poco de código tonto que, por supuesto, no funciona basado en uno de los ejemplos que distribuyen:
[code language="c"]/*  WebM module for BennuGD
*  Copyright © 2010 Joseba García Etxebarria <joseba.gar@gmail.com>
*
*  This program 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.
*
*  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#include "vpx_decoder.h"
#include "vp8dx.h"
#define VPX_CODEC_DISABLE_COMPAT 1
#define IVF_FILE_HDR_SZ  (32)
#define IVF_FRAME_HDR_SZ (12)
#define interface (&vpx_codec_vp8_dx_algo)

/* BennuGD stuff */
#include <bgddl.h>
#include <xstrings.h>
#include <libgrbase.h>
#include <g_video.h>

static unsigned int mem_get_le32(const unsigned char *mem) {
   return (mem[3] << 24)|(mem[2] << 16)|(mem[1] << 8)|(mem[0]);
}

static FILE            *infile;
static int              playing_video = 0;
static vpx_codec_ctx_t  codec;
      int              frame_sz;
      vpx_codec_iter_t  iter = NULL;
      vpx_image_t      *img;

/*********************************************/
/* Plays the given video with WebM           */
/* Must be given:                            */
/*    filename to be played                  */
/*    width  of the video to render          */
/*    height of the video to render          */
/*********************************************/
static int video_play(INSTANCE *my, int * params)
{
   GRAPH           *gr = NULL;
   int              flags = 0, frame_cnt = 0;
   unsigned char    file_hdr[IVF_FILE_HDR_SZ];
   unsigned char    frame_hdr[IVF_FRAME_HDR_SZ];
   unsigned char    frame[256*1024];
   vpx_codec_err_t  res;
   
   (void)res;

   /* Ensure we're not playing a video already */
   if(playing_video == 1)
     return -1;
   
   /* Ensure we can read the given file */
   if(!(infile = fopen(string_get(params[0]), "rb")))
     return -1;
   
   /* Discard the file path, as we don't need it anymore */
   string_discard(params[0]);

   /* Start the graphics mode, if not initialized yet */
   if(! scr_initialized)
     gr_init(320, 240);

   /* Read file header */
   fread(file_hdr, 1, IVF_FILE_HDR_SZ, infile);
   if(!(file_hdr[0]=='D' && file_hdr[1]=='K' && file_hdr[2]=='I'
      && file_hdr[3]=='F'))
     return -1;

   /* Initialize codec */
   if(vpx_codec_dec_init(&codec, interface, NULL, flags))
     return -1;

   /* Create the graphic that will hold the video surface data */
   gr = bitmap_new(0, params[1], params[2], 16);
   gr->code = bitmap_next_code();
   grlib_add_map(0, gr);

   /* Render the first frame */
   fread(frame_hdr, 1, IVF_FRAME_HDR_SZ, infile);
   frame_sz = mem_get_le32(frame_hdr);
   frame_cnt++;
   if(frame_sz > sizeof(frame))
     return -1;
   if(fread(frame, 1, frame_sz, infile) != frame_sz)
     return -1;
   if(vpx_codec_decode(&codec, frame, frame_sz, NULL, 0))
     return -1;
   img = vpx_codec_get_frame(&codec, &iter);

   /* Lock the video playback */
   playing_video = 1;
   
   return gr->code;
}

/* Stop the currently being played video and release WebM codec */
static int video_stop(INSTANCE *my, int * params)
{
 if(! playing_video)
   return 0;

 /* Stop the decoding */
 if(vpx_codec_destroy(&codec))
   return -1;
 fclose(infile);

 /* Release the video playback lock */
 playing_video = 0;

 return 0;
}

/* Checks wether the current video is being played */
static int video_is_playing() {
 return playing_video;
}

DLSYSFUNCS __bgdexport( mod_webm, functions_exports )[] =
{
   {"VIDEO_PLAY"                  , "SII"  , TYPE_DWORD , video_play       },
   {"VIDEO_PLAYING"               , ""     , TYPE_DWORD , video_is_playing },
   {"VIDEO_STOP"                  , ""     , TYPE_DWORD , video_stop       },
   { NULL        , NULL , 0         , NULL              }
};

char * __bgdexport( mod_webm, modules_dependency )[] =
{
 "libgrbase",
 "libvideo",
 NULL
};

void __bgdexport( mod_webm, module_finalize )()
{
   video_stop(NULL, NULL);
}[/code]

Teneis el código fuente del códec con ejemplos aquí:
http://www.webmproject.org/

osk

Se avecinan tiempos de guerra!!