e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
1b10db2002-10-08Martin Nilsson 
7a64d32002-07-18Honza Petrous /* * Pike C glue module for FFMpeg library * * Creator: Honza Petrous <hop@unibase.cz> * */
e3c6862015-10-18Martin Nilsson #include "global.h"
7a64d32002-07-18Honza Petrous #include "config.h" #include "interpret.h" #include "svalue.h" #include "stralloc.h" #include "array.h" #include "object.h" #include "mapping.h" #include "pike_macros.h" #include "module_support.h" #include "builtin_functions.h"
d22b8f2002-08-06Xavier Beaudouin #ifdef HAVE_WORKING_LIBFFMPEG
7a64d32002-07-18Honza Petrous  #include <math.h>
8667142003-02-07Mirar (Pontus Hagland) /* ffmpeg includes typedef's these */
0dbe502003-12-24Mirar (Pontus Hagland) #ifdef FFMPEG_REDEFINES_UINT8
8667142003-02-07Mirar (Pontus Hagland) #ifdef INT64 #undef INT64 #endif #ifdef INT32 #undef INT32 #endif #ifdef INT16 #undef INT16 #endif #ifdef INT8 #undef INT8 #endif #ifdef UINT64 #undef UINT64 #endif #ifdef UINT32 #undef UINT32 #endif #ifdef UINT16 #undef UINT16 #endif #ifdef UINT8 #undef UINT8 #endif
0dbe502003-12-24Mirar (Pontus Hagland) #endif
8667142003-02-07Mirar (Pontus Hagland) 
1c0dc92011-10-30Henrik Grubbström (Grubba) #define FF_API_AUDIO_OLD 1 #define FF_API_VIDEO_OLD 1
ca2d252002-08-05Xavier Beaudouin #ifdef HAVE_LIBAVCODEC_AVCODEC_H #include <libavcodec/avcodec.h> #else
1c0dc92011-10-30Henrik Grubbström (Grubba) #ifdef HAVE_FFMPEG_AVCODEC_H #include <ffmpeg/avcodec.h> #else
7a64d32002-07-18Honza Petrous #ifdef HAVE_LIBFFMPEG_AVCODEC_H #include <libffmpeg/avcodec.h> #else #ifdef HAVE_AVCODEC_H #include <avcodec.h> #endif #endif
ca2d252002-08-05Xavier Beaudouin #endif
56af1b2002-12-06Mirar (Pontus Hagland) #endif
7a64d32002-07-18Honza Petrous 
5d43a72012-02-11Marcus Comstedt #ifdef HAVE_LIBAVUTIL_AVUTIL_H #include <libavutil/avutil.h>
ce4e5b2012-04-19Henrik Grubbström (Grubba) /* FIXME: The following test is an approximation. * 50.7.0 is known not to have the enum. * 50.43.0 is known to have it. */ #if (LIBAVUTIL_VERSION_MAJOR > 50) || (LIBAVUTIL_VERSION_MINOR > 7)
5d43a72012-02-11Marcus Comstedt #define USE_AVMEDIA_TYPE_ENUM 1 #endif
ce4e5b2012-04-19Henrik Grubbström (Grubba) #endif
5d43a72012-02-11Marcus Comstedt 
7a64d32002-07-18Honza Petrous #ifdef AV_MEM #define FF_ALLOC(x) av_mallocz(x) #define FF_FREE(x) av_free(x) #else #define FF_ALLOC(x) malloc(x) #define FF_FREE(x) free(x) #endif
0dbe502003-12-24Mirar (Pontus Hagland) #ifndef HAVE_UINT8_T #define uint8_t unsigned char #endif
7a64d32002-07-18Honza Petrous  static struct program *ffmpeg_program; typedef struct { AVCodec *codec; AVCodecContext codec_context; int encoder;
0dbe502003-12-24Mirar (Pontus Hagland)  uint8_t *outbuf;
7a64d32002-07-18Honza Petrous } ffmpeg_data; #define THIS ((ffmpeg_data *)Pike_fp->current_storage) int encoder_flg(AVCodec *codec) { int flg = -1;
6951522013-07-01Henrik Grubbström (Grubba)  if( codec-> #ifdef HAVE_AVCODEC_ENCODE2 encode2 #else encode #endif )
7a64d32002-07-18Honza Petrous  flg = 1; else if( codec->decode ) flg = 0; return(flg); }
677f032002-09-09Honza Petrous 
bbd8af2002-09-09Manual system /*! @module _Ffmpeg
677f032002-09-09Honza Petrous  */
ec59972002-07-22Martin Nilsson /*! @class ffmpeg
7a64d32002-07-18Honza Petrous  *! *! Implements support of all codecs from a nice project Ffmpeg.
ec59972002-07-22Martin Nilsson  *! You can find more info about it at @url{http://ffmpeg.sf.net/@}.
7a64d32002-07-18Honza Petrous  */
ec59972002-07-22Martin Nilsson /*! @decl void create(int codec_name, int encoder)
7a64d32002-07-18Honza Petrous  *! *! Create decoder or encoder object. *! *! @param codec_name
ec59972002-07-22Martin Nilsson  *! Internal number of codec, eg. @[CODEC_ID_MP2].
7a64d32002-07-18Honza Petrous  *! *! @param encoder
ec59972002-07-22Martin Nilsson  *! If @tt{true@}, encoder object will be created, decoder
7a64d32002-07-18Honza Petrous  *! object otherwise. *! */
80c1e22005-06-14Henrik Grubbström (Grubba) static void f_create(INT32 args) { AVCodec *codec = NULL;
7a64d32002-07-18Honza Petrous  int codec_id = CODEC_ID_MP2; int rate, wide, chns; if(THIS->codec != NULL) Pike_error("Create already called!\n"); switch(args) { case 5:
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != T_INT)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 5, expected int.\n"); chns = (u_short)Pike_sp[-1].u.integer; Pike_sp--; case 4:
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != T_INT)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 4, expected int.\n"); wide = (u_short)Pike_sp[-1].u.integer; Pike_sp--; case 3:
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != T_INT)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 3, expected int.\n"); rate = (u_short)Pike_sp[-1].u.integer; Pike_sp--; case 2:
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != T_INT)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 2, expected int.\n"); THIS->encoder = (u_short)Pike_sp[-1].u.integer; Pike_sp--;
6347502002-12-10Martin Nilsson  /* case 1: */
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != T_INT)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 1, expected int.\n"); codec_id = (u_short)Pike_sp[-1].u.integer; Pike_sp--; if(THIS->encoder) {
80c1e22005-06-14Henrik Grubbström (Grubba)  codec = avcodec_find_encoder(codec_id); if(!codec) Pike_error("Codec for encoder 0x%02x not found.\n", codec_id);
7a64d32002-07-18Honza Petrous  } else {
80c1e22005-06-14Henrik Grubbström (Grubba)  codec = avcodec_find_decoder(codec_id); if(!codec) Pike_error("Codec for decoder 0x%02x not found.\n", codec_id);
7a64d32002-07-18Honza Petrous  }
80c1e22005-06-14Henrik Grubbström (Grubba)  memset(&THIS->codec_context, 0, sizeof(THIS->codec_context));
e5925f2013-07-03Henrik Grubbström (Grubba)  /* FIXME: Use avcodec_open2() if available. */
80c1e22005-06-14Henrik Grubbström (Grubba)  if (avcodec_open(&THIS->codec_context, codec) < 0)
7a64d32002-07-18Honza Petrous  Pike_error("Could not open codec.\n");
80c1e22005-06-14Henrik Grubbström (Grubba)  THIS->codec = codec;
7a64d32002-07-18Honza Petrous  if(THIS->outbuf != NULL) FF_FREE(THIS->outbuf); THIS->outbuf = FF_ALLOC(AVCODEC_MAX_AUDIO_FRAME_SIZE); /* FIXME: we really want so big buffer? */ if(THIS->outbuf == NULL) Pike_error("Malloc of internal buffer failed.\n"); break; default: Pike_error("Invalid number of arguments to create().\n"); } } /* create */
ec59972002-07-22Martin Nilsson /*! @decl mapping|int get_codec_info()
7a64d32002-07-18Honza Petrous  *! *! Returns mapping with info of used codec. *! *! @seealso *! @[list_codecs()] */ static void f_codec_info(INT32 args) { pop_n_elems(args);
80c1e22005-06-14Henrik Grubbström (Grubba)  if(THIS->codec) {
5e9fc02015-08-18Per Hedbor  push_static_text("name"); push_text( THIS->codec->name );
6a932b2014-08-18Martin Nilsson  ref_push_string(literal_type_string); push_int( THIS->codec->type );
5e9fc02015-08-18Per Hedbor  push_static_text("id"); push_int( THIS->codec->id ); push_static_text("encoder_flg"); push_int( encoder_flg(THIS->codec) );
7a64d32002-07-18Honza Petrous  f_aggregate_mapping( 2*4 ); } else push_int(0); }
ec59972002-07-22Martin Nilsson /*! @decl int set_codec_param(string name, mixed value)
7a64d32002-07-18Honza Petrous  *! *! Sets one codec parameter *! *! @param name
cbe8c92003-04-07Martin Nilsson  *! The name of parameter. One of @expr{"sample_rate"@}, *! @expr{"bit_rate"@}, @expr{"channels"@}.
7a64d32002-07-18Honza Petrous  *! *! @returns *! Returns 1 on success, 0 otherwise (parameter not known). *! *! @seealso *! @[get_codec_params()] */ static void f_set_codec_param(INT32 args) { struct pike_string *pname; if(args != 2) Pike_error("Invalid number of arguments to set_codec_param().\n");
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-args]) != T_STRING)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 1, expected string.\n"); pname = Pike_sp[-args].u.string; /* bit_rate */ if(!strncmp(pname->str, "bit_rate", 8)) {
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != T_INT)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 2, expected integer.\n");
6347502002-12-10Martin Nilsson  /* FIXME: test correct value of bit rate argument */
80c1e22005-06-14Henrik Grubbström (Grubba)  THIS->codec_context.bit_rate = Pike_sp[-1].u.integer;
7a64d32002-07-18Honza Petrous  pop_n_elems(args); push_int(1); return; } /* sample_rate */ if(!strncmp(pname->str, "sample_rate", 11)) {
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != T_INT)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 2, expected integer.\n");
6347502002-12-10Martin Nilsson  /* FIXME: test correct value of bit rate argument */
80c1e22005-06-14Henrik Grubbström (Grubba)  THIS->codec_context.sample_rate = Pike_sp[-1].u.integer;
7a64d32002-07-18Honza Petrous  pop_n_elems(args); push_int(1); return; } /* channels */ if(!strncmp(pname->str, "channels", 8)) {
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != T_INT)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 2, expected integer.\n");
6347502002-12-10Martin Nilsson  /* FIXME: test correct value of bit rate argument */
80c1e22005-06-14Henrik Grubbström (Grubba)  THIS->codec_context.channels = (u_short)Pike_sp[-1].u.integer;
7a64d32002-07-18Honza Petrous  pop_n_elems(args); push_int(1); return; } pop_n_elems(args); push_int(0); }
ec59972002-07-22Martin Nilsson /*! @decl mapping|int get_codec_status()
7a64d32002-07-18Honza Petrous  *!
ec59972002-07-22Martin Nilsson  *! Returns a mapping with the actual codec parameters.
7a64d32002-07-18Honza Petrous  *! *! @seealso *! @[set_codec_param()] */ static void f_get_codec_status(INT32 args) { int cnt = 0; pop_n_elems(args);
80c1e22005-06-14Henrik Grubbström (Grubba)  if(THIS->codec == NULL) {
7a64d32002-07-18Honza Petrous  push_int(0);
80c1e22005-06-14Henrik Grubbström (Grubba)  return; }
13670c2015-05-25Martin Nilsson 
5e9fc02015-08-18Per Hedbor  push_static_text("name"); push_text( THIS->codec->name );
6a932b2014-08-18Martin Nilsson  ref_push_string(literal_type_string); push_int( THIS->codec->type );
5e9fc02015-08-18Per Hedbor  push_static_text("id"); push_int( THIS->codec->id ); push_static_text("encoder_flg"); push_int( encoder_flg(THIS->codec) ); push_static_text("flags"); push_int( THIS->codec_context.flags );
7a64d32002-07-18Honza Petrous  cnt = 5;
5d43a72012-02-11Marcus Comstedt #ifdef USE_AVMEDIA_TYPE_ENUM if(THIS->codec->type == AVMEDIA_TYPE_AUDIO) { #else
7a64d32002-07-18Honza Petrous  if(THIS->codec->type == CODEC_TYPE_AUDIO) {
5d43a72012-02-11Marcus Comstedt #endif
7a64d32002-07-18Honza Petrous  /* audio only */
5e9fc02015-08-18Per Hedbor  push_static_text("sample_rate"); push_int( THIS->codec_context.sample_rate ); push_static_text("channels"); push_int( THIS->codec_context.channels );
7a64d32002-07-18Honza Petrous  cnt += 2; }
5d43a72012-02-11Marcus Comstedt #ifdef USE_AVMEDIA_TYPE_ENUM if(THIS->codec->type == AVMEDIA_TYPE_VIDEO) { #else
7a64d32002-07-18Honza Petrous  if(THIS->codec->type == CODEC_TYPE_VIDEO) {
5d43a72012-02-11Marcus Comstedt #endif
7a64d32002-07-18Honza Petrous  /* video only */
5e9fc02015-08-18Per Hedbor  push_static_text("frame_rate");
80c1e22005-06-14Henrik Grubbström (Grubba) #ifdef HAVE_AVCODECCONTEXT_FRAME_RATE /* avcodec.h 1.392 (LIBAVCODEC_BUILD 4753) and earlier. */ push_int(THIS->codec_context.frame_rate); #else /* !HAVE_AVCODECCONTEXT_FRAME_RATE */ /* avcodec.h 1.393 (LIBAVCODEC_BUILD 4754) and later. */ push_int(THIS->codec_context.time_base.den/ THIS->codec_context.time_base.num); #endif /* HAVE_AVCODECCONTEXT_FRAME_RATE */
5e9fc02015-08-18Per Hedbor  push_static_text("width"); push_int( THIS->codec_context.width );
7a64d32002-07-18Honza Petrous  cnt += 2; } f_aggregate_mapping(2 * cnt); }
1c0dc92011-10-30Henrik Grubbström (Grubba) #ifndef HAVE_AVCODEC_DECODE_AUDIO #define avcodec_decode_audio avcodec_decode_audio2
5d43a72012-02-11Marcus Comstedt #ifndef HAVE_AVCODEC_DECODE_AUDIO2 #define avcodec_decode_audio2 avcodec_decode_audio3 #define USE_AVPACKET_FOR_DECODE
e5925f2013-07-03Henrik Grubbström (Grubba) #ifndef HAVE_AVCODEC_DECODE_AUDIO3 /* FIXME: Support avcodec_decode_audio4(). */ #endif
5d43a72012-02-11Marcus Comstedt #endif
1c0dc92011-10-30Henrik Grubbström (Grubba) #endif
ec59972002-07-22Martin Nilsson /*! @decl mapping|int decode(string data)
7a64d32002-07-18Honza Petrous  *!
ec59972002-07-22Martin Nilsson  *! Returns a mapping with the new decoded frame and lenght of
7a64d32002-07-18Honza Petrous  *! @[data] which was used for decoding. *!
ec59972002-07-22Martin Nilsson  *! @decl int decode(string data, function shuffler)
7a64d32002-07-18Honza Petrous  *! *! Decode all @[data] buffer and pass result to @[shuffler].
cbe8c92003-04-07Martin Nilsson  *! Returns @expr{1@} on success, @expr{0@} otherwise.
7a64d32002-07-18Honza Petrous  *! *! @note *! Shuffler variant isn't implemented, yet. *! *! @note *! Usable only in decoder. *! *! @seealso *! @[create()] */ static void f_decode(INT32 args) { struct pike_string *idata; int len, samples_size; struct svalue feeder;
5d43a72012-02-11Marcus Comstedt #ifdef USE_AVPACKET_FOR_DECODE AVPacket avp; #endif
7a64d32002-07-18Honza Petrous 
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-args]) != T_STRING)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 1, expected string.\n"); idata = Pike_sp[-args].u.string;
80c1e22005-06-14Henrik Grubbström (Grubba)  if(!THIS->codec)
7a64d32002-07-18Honza Petrous  Pike_error("Codec wasn't inited.\n");
80c1e22005-06-14Henrik Grubbström (Grubba)  if(!THIS->outbuf)
7a64d32002-07-18Honza Petrous  Pike_error("Low memory? Decoder buffer doesn't exist.\n"); if(!idata->len)
c870812004-04-14Henrik Grubbström (Grubba)  Pike_error("Encoded data is empty.\n"); if (idata->size_shift) Pike_error("Encoded data is wide.\n");
7a64d32002-07-18Honza Petrous  if(args > 1) { /* FIXME: shuffler part not implemented, yet */ #if NOT_IMPLEMENTED_YET
dd24fe2011-10-29Henrik Grubbström (Grubba)  if(TYPEOF(Pike_sp[-1]) != T_FUNCTION)
7a64d32002-07-18Honza Petrous  Pike_error("Invalid argument 2, expected function.\n"); feeder = Pike_sp[1-args].u.svalue; apply_svalue(&feeder, 0); /* we want more data */ #endif pop_n_elems(args); push_int(0);
80c1e22005-06-14Henrik Grubbström (Grubba)  return;
7a64d32002-07-18Honza Petrous  } /* one pass decoding */
5d43a72012-02-11Marcus Comstedt #ifdef USE_AVPACKET_FOR_DECODE av_init_packet(&avp); avp.data = STR0(idata); avp.size = idata->len; len = avcodec_decode_audio(&THIS->codec_context, (short *)THIS->outbuf, &samples_size, &avp); #else
80c1e22005-06-14Henrik Grubbström (Grubba)  len = avcodec_decode_audio(&THIS->codec_context, (short *)THIS->outbuf, &samples_size,
c870812004-04-14Henrik Grubbström (Grubba)  STR0(idata), idata->len);
5d43a72012-02-11Marcus Comstedt #endif
7a64d32002-07-18Honza Petrous  if(len < 0) Pike_error("Error while decoding.\n"); if(samples_size > 0) { /* frame was decoded */ pop_n_elems(args);
5e9fc02015-08-18Per Hedbor  push_static_text("data");
7a64d32002-07-18Honza Petrous  push_string(make_shared_binary_string((char *)THIS->outbuf, samples_size));
5e9fc02015-08-18Per Hedbor  push_static_text("decoded");
7a64d32002-07-18Honza Petrous  push_int(len); f_aggregate_mapping( 2*2 ); return; } pop_n_elems(args); push_int(len); }
ec59972002-07-22Martin Nilsson /* @decl mapping|int encode(string data)
7a64d32002-07-18Honza Petrous  * * Returns mapping with new encoded frame and lenght of * @[data] which was used for encoding. * * @decl int encode(string data, function shuffler);
cbe8c92003-04-07Martin Nilsson  * Returns @expr{1@} on success, @expr{0@} otherwise.
7a64d32002-07-18Honza Petrous  * * @note * Usable only in encoder * * @seealso * @[create()] */ static void f_encode(INT32 args) { pop_n_elems(args); push_int(0); } /*! @decl array(mapping) list_codecs() *! *! Gets all supported codecs. *! *! @returns *! Array of mapping with codec features. */ static void f_list_codecs(INT32 args) { int cnt = 0; AVCodec *codec; avcodec_register_all(); pop_n_elems(args);
1c0dc92011-10-30Henrik Grubbström (Grubba) #ifdef HAVE_AV_CODEC_NEXT codec = NULL; while ((codec = av_codec_next(codec))) { cnt++;
5e9fc02015-08-18Per Hedbor  push_static_text("name"); push_text( codec->name );
6a932b2014-08-18Martin Nilsson  ref_push_string(literal_type_string); push_int( codec->type );
5e9fc02015-08-18Per Hedbor  push_static_text("id"); push_int( codec->id ); push_static_text("encoder_flg"); push_int( encoder_flg(codec) );
1c0dc92011-10-30Henrik Grubbström (Grubba)  f_aggregate_mapping( 2*4 ); } #else /* !HAVE_AV_CODEC_NEXT */
7a64d32002-07-18Honza Petrous  if(first_avcodec == NULL) { push_int(0); return; } codec = first_avcodec; while(codec != NULL) { cnt++;
5e9fc02015-08-18Per Hedbor  push_static_text("name"); push_text( codec->name );
6a932b2014-08-18Martin Nilsson  ref_push_string(literal_type_string); push_int( codec->type );
5e9fc02015-08-18Per Hedbor  push_static_text("id"); push_int( codec->id ); push_static_text("encoder_flg"); push_int( encoder_flg(codec) );
7a64d32002-07-18Honza Petrous  codec = codec->next; f_aggregate_mapping( 2*4 ); }
1c0dc92011-10-30Henrik Grubbström (Grubba) #endif /* HAVE_AV_CODEC_NEXT */
7a64d32002-07-18Honza Petrous  push_array(aggregate_array( cnt )); }
ec59972002-07-22Martin Nilsson /*! @endclass
7a64d32002-07-18Honza Petrous  */
6388872002-09-10Honza Petrous /*! @decl constant CODEC_ID_NONE *! @decl constant CODEC_ID_AC3 *! @decl constant CODEC_ID_ADPCM_IMA_QT *! @decl constant CODEC_ID_ADPCM_IMA_WAV *! @decl constant CODEC_ID_ADPCM_MS *! @decl constant CODEC_ID_H263 *! @decl constant CODEC_ID_H263I *! @decl constant CODEC_ID_H263P *! @decl constant CODEC_ID_MJPEG *! @decl constant CODEC_ID_MPEG1VIDEO *! @decl constant CODEC_ID_MPEG4 *! @decl constant CODEC_ID_MP2 *! @decl constant CODEC_ID_MP3LAME *! @decl constant CODEC_ID_MSMPEG4V1 *! @decl constant CODEC_ID_MSMPEG4V2 *! @decl constant CODEC_ID_MSMPEG4V3 *! @decl constant CODEC_ID_PCM_ALAW *! @decl constant CODEC_ID_PCM_MULAW *! @decl constant CODEC_ID_PCM_S16BE *! @decl constant CODEC_ID_PCM_S16LE *! @decl constant CODEC_ID_PCM_S8 *! @decl constant CODEC_ID_PCM_U16BE *! @decl constant CODEC_ID_PCM_U16LE *! @decl constant CODEC_ID_PCM_U8 *! @decl constant CODEC_ID_RAWVIDEO *! @decl constant CODEC_ID_RV10 *! @decl constant CODEC_ID_SVQ1 *! @decl constant CODEC_ID_VORBIS *! @decl constant CODEC_ID_WMV1 *! @decl constant CODEC_ID_WMV2 *! *! Various audio and video codecs. *! *! @note *! The list of supported codecs depends on Ffmpeg library. *! *! @decl constant CODEC_TYPE_AUDIO *! @decl constant CODEC_TYPE_VIDEO *! *! Type of codec. */
677f032002-09-09Honza Petrous /*! @endmodule */
7a64d32002-07-18Honza Petrous static void init_ffmpeg_data(struct object *obj) { THIS->codec = NULL; THIS->outbuf = NULL; THIS->encoder = 0;
6347502002-12-10Martin Nilsson  avcodec_register_all(); /* FIXME: register only "interesting" codec ? */
7a64d32002-07-18Honza Petrous } static void exit_ffmpeg_data(struct object *obj) { if(THIS->codec != NULL)
80c1e22005-06-14Henrik Grubbström (Grubba)  avcodec_close(&THIS->codec_context);
7a64d32002-07-18Honza Petrous  if(THIS->outbuf != NULL) FF_FREE(THIS->outbuf); } /* * --------------------- * Pike module API * --------------------- */
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_INIT {
7a64d32002-07-18Honza Petrous 
362e4e2016-01-29Martin Nilsson  ADD_FUNCTION("list_codecs", f_list_codecs, tFunc(tVoid,tOr(tArray,tInt)), 0);
7a64d32002-07-18Honza Petrous 
d629172002-08-05Xavier Beaudouin #ifdef LIBAVCODEC_VERSION
8b3f922006-01-09Henrik Grubbström (Grubba)  add_string_constant("lib_version", DEFINETOSTR(LIBAVCODEC_VERSION), 0);
d629172002-08-05Xavier Beaudouin #else #ifdef FFMPEG_VERSION add_string_constant("lib_version", FFMPEG_VERSION, 0); #endif #endif
7a64d32002-07-18Honza Petrous #ifdef LIBAVCODEC_BUILD_STR add_string_constant("lib_build", LIBAVCODEC_BUILD_STR, 0); #endif /* * Internal codec constant name * */
56af1b2002-12-06Mirar (Pontus Hagland) #ifndef CODEC_ID_NONE /* the codecs are enum'ed */ #include "codecs_auto.h" #else
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_NONE", CODEC_ID_NONE, 0); add_integer_constant("CODEC_ID_MPEG1VIDEO", CODEC_ID_MPEG1VIDEO, 0); add_integer_constant("CODEC_ID_H263", CODEC_ID_H263, 0); add_integer_constant("CODEC_ID_RV10", CODEC_ID_RV10, 0); add_integer_constant("CODEC_ID_MP2", CODEC_ID_MP2, 0);
d629172002-08-05Xavier Beaudouin #ifdef CODEC_ID_MP3LAME
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_MP3LAME", CODEC_ID_MP3LAME, 0);
d629172002-08-05Xavier Beaudouin #endif
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_AC3", CODEC_ID_AC3, 0); add_integer_constant("CODEC_ID_MJPEG", CODEC_ID_MJPEG, 0); add_integer_constant("CODEC_ID_MPEG4", CODEC_ID_MPEG4, 0); add_integer_constant("CODEC_ID_RAWVIDEO", CODEC_ID_RAWVIDEO, 0);
d629172002-08-05Xavier Beaudouin #ifdef CODEC_ID_MSMPEG4V1
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_MSMPEG4V1", CODEC_ID_MSMPEG4V1, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_MSMPEG4V2
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_MSMPEG4V2", CODEC_ID_MSMPEG4V2, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_MSMPEG4V3
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_MSMPEG4V3", CODEC_ID_MSMPEG4V3, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_WMV1
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_WMV1", CODEC_ID_WMV1, 0);
d629172002-08-05Xavier Beaudouin #endif
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_H263I", CODEC_ID_H263I, 0); add_integer_constant("CODEC_ID_H263P", CODEC_ID_H263P, 0); #ifdef CODEC_ID_WMV2 add_integer_constant("CODEC_ID_WMV2", CODEC_ID_WMV2, 0); #endif #ifdef CODEC_ID_SVQ1 add_integer_constant("CODEC_ID_SVQ1", CODEC_ID_SVQ1, 0); #endif
d629172002-08-05Xavier Beaudouin #ifdef CODEC_ID_PCM_S16LE
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_PCM_S16LE", CODEC_ID_PCM_S16LE, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_PCM_S16BE
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_PCM_S16BE", CODEC_ID_PCM_S16BE, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_PCM_U16LE
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_PCM_U16LE", CODEC_ID_PCM_U16LE, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_PCM_U16BE
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_PCM_U16BE", CODEC_ID_PCM_U16BE, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_PCM_S8
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_PCM_S8", CODEC_ID_PCM_S8, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_PCM_U8
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_PCM_U8", CODEC_ID_PCM_U8, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_PCM_MULAW
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_PCM_MULAW", CODEC_ID_PCM_MULAW, 0);
d629172002-08-05Xavier Beaudouin #endif #ifdef CODEC_ID_PCM_ALAW
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_ID_PCM_ALAW", CODEC_ID_PCM_ALAW, 0);
d629172002-08-05Xavier Beaudouin #endif
7a64d32002-07-18Honza Petrous #ifdef CODEC_ID_ADPCM_MS add_integer_constant("CODEC_ID_ADPCM_MS", CODEC_ID_ADPCM_MS, 0); #endif #ifdef CODEC_ID_ADPCM_IMA_WAV add_integer_constant("CODEC_ID_ADPCM_IMA_WAV", CODEC_ID_ADPCM_IMA_WAV, 0); #endif #ifdef CODEC_ID_ADPCM_IMA_QT add_integer_constant("CODEC_ID_ADPCM_IMA_QT", CODEC_ID_ADPCM_IMA_QT, 0); #endif
677f032002-09-09Honza Petrous #ifdef CODEC_ID_VORBIS add_integer_constant("CODEC_ID_VORBIS", CODEC_ID_VORBIS, 0); #endif
7a64d32002-07-18Honza Petrous 
56af1b2002-12-06Mirar (Pontus Hagland) #endif
7a64d32002-07-18Honza Petrous  /* * Internal type of codec. * */
5d43a72012-02-11Marcus Comstedt #ifdef USE_AVMEDIA_TYPE_ENUM add_integer_constant("AVMEDIA_TYPE_UNKNOWN", AVMEDIA_TYPE_UNKNOWN, 0); add_integer_constant("AVMEDIA_TYPE_AUDIO", AVMEDIA_TYPE_AUDIO, 0); add_integer_constant("AVMEDIA_TYPE_VIDEO", AVMEDIA_TYPE_VIDEO, 0); add_integer_constant("AVMEDIA_TYPE_DATA", AVMEDIA_TYPE_DATA, 0); add_integer_constant("AVMEDIA_TYPE_SUBTITLE", AVMEDIA_TYPE_SUBTITLE, 0); add_integer_constant("AVMEDIA_TYPE_ATTACHMENT", AVMEDIA_TYPE_ATTACHMENT, 0); add_integer_constant("AVMEDIA_TYPE_NB", AVMEDIA_TYPE_NB, 0); #else
7a64d32002-07-18Honza Petrous  add_integer_constant("CODEC_TYPE_AUDIO", CODEC_TYPE_AUDIO, 0); add_integer_constant("CODEC_TYPE_VIDEO", CODEC_TYPE_VIDEO, 0);
56af1b2002-12-06Mirar (Pontus Hagland) #ifndef CODEC_ID_NONE /* it's enum'ed */ add_integer_constant("CODEC_TYPE_UNKNOWN", CODEC_TYPE_UNKNOWN, 0); #else
7a64d32002-07-18Honza Petrous #ifdef CODEC_TYPE_UNKNOWN add_integer_constant("CODEC_TYPE_UNKNOWN", CODEC_TYPE_UNKNOWN, 0); #endif
56af1b2002-12-06Mirar (Pontus Hagland) #endif
5d43a72012-02-11Marcus Comstedt #endif
7a64d32002-07-18Honza Petrous  start_new_program(); ADD_STORAGE(ffmpeg_data);
362e4e2016-01-29Martin Nilsson  ADD_FUNCTION("create", f_create, tFunc(tInt tInt tOr(tInt,tVoid) tOr(tInt,tVoid) tOr(tInt,tVoid), tVoid), 0); ADD_FUNCTION("get_codec_info", f_codec_info, tFunc(tVoid,tMapping), 0); ADD_FUNCTION("decode", f_decode, tFunc(tStr,tOr(tMapping,tInt)), 0); ADD_FUNCTION("encode", f_encode, tFunc(tStr,tOr(tMapping,tInt)), 0); ADD_FUNCTION("set_codec_param", f_set_codec_param, tFunc(tStr tMix,tInt), 0); ADD_FUNCTION("get_codec_status", f_get_codec_status, tFunc(tVoid,tOr(tMapping,tInt)), 0);
7a64d32002-07-18Honza Petrous  set_init_callback(init_ffmpeg_data); set_exit_callback(exit_ffmpeg_data); ffmpeg_program = end_program(); add_program_constant("ffmpeg", ffmpeg_program, 0);
51ef5c2002-10-21Marcus Comstedt } /* PIKE_MODULE_INIT */
7a64d32002-07-18Honza Petrous 
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_EXIT {
7a64d32002-07-18Honza Petrous  if(ffmpeg_program) { free_program(ffmpeg_program); ffmpeg_program = NULL; }
51ef5c2002-10-21Marcus Comstedt } /* PIKE_MODULE_EXIT */
7a64d32002-07-18Honza Petrous  #else
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_INIT {
7a64d32002-07-18Honza Petrous  }
51ef5c2002-10-21Marcus Comstedt PIKE_MODULE_EXIT {
7a64d32002-07-18Honza Petrous } #endif