Advertisement
Guest User

Untitled

a guest
Dec 4th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include "ffmpeg4r_avformat.h"
  2.  
  3. VALUE r_av_register_all( void ) {
  4.     av_register_all();
  5.     return Qnil;
  6. };
  7.  
  8. VALUE r_avformat_context_allocate(VALUE klass) {
  9.     AVFormatContext *ctx = avformat_alloc_context();
  10.  
  11.     return  Data_Wrap_Struct(klass, NULL, r_avformat_context_free, ctx);
  12. }
  13.  
  14. void r_avformat_context_mark(void *p){
  15.    
  16. };
  17.  
  18. void  r_avformat_context_free(void *p) {
  19.     AVFormatContext *ctx = (AVFormatContext *) p;
  20.     if (ctx) {
  21.         avformat_free_context(ctx);
  22.     }
  23. };
  24.  
  25. VALUE r_avformat_open_input_file(VALUE self, VALUE context, VALUE filename) {  
  26.     AVFormatContext *ctx;
  27.     char* fn;
  28.     int res = -1;
  29.     Check_Type(filename, T_STRING);
  30.     Data_Get_Struct(context, AVFormatContext, ctx);
  31.  
  32.     fn = StringValuePtr(filename);
  33.     res = avformat_open_input(&ctx, fn, NULL, NULL);
  34.     return INT2NUM(res);
  35. };
  36.  
  37. VALUE r_avformat_close_input_file(VALUE self, VALUE context) {
  38.     AVFormatContext *ctx;
  39.     Data_Get_Struct(context, AVFormatContext, ctx);
  40.  
  41.     avformat_close_input(&ctx);
  42.     ctx = NULL;
  43.     return Qnil;
  44. };
  45.  
  46. VALUE r_avformat_context_metadata(VALUE self) {
  47.     AVFormatContext *ctx;
  48.     AVDictionaryEntry *t = NULL;
  49.     VALUE result = rb_hash_new();
  50.     Data_Get_Struct(self, AVFormatContext, ctx);
  51.  
  52.     while (t = av_dict_get(ctx->metadata, "", t, AV_DICT_IGNORE_SUFFIX)) {
  53.         rb_hash_aset(result, rb_str_new2(t->key), rb_str_new2(t->value));
  54.     }
  55.     return result;
  56. };
  57.  
  58.  
  59.  
  60. VALUE r_avformat_context_initialize(VALUE self) {
  61.     return self;
  62. }
  63.  
  64. void Init_ffmpeg4r_avformat(VALUE module) {
  65.     mAVFormat = rb_define_module_under(module, "AVFormat");
  66.     rb_define_module_function(mAVFormat, "av_register_all", r_av_register_all, 0);
  67.     rb_define_module_function(mAVFormat, "avformat_open_input_file", r_avformat_open_input_file, 2);
  68.     rb_define_module_function(mAVFormat, "avformat_close_input_file", r_avformat_close_input_file, 1);
  69.     // FFmpeg4r::AVFormat::AVFormatContext
  70.     cAVFormatContext = rb_define_class_under(mAVFormat, "AVFormatContext", rb_cObject);
  71.     rb_define_alloc_func(cAVFormatContext, r_avformat_context_allocate);
  72.     rb_define_method(cAVFormatContext, "initialize", r_avformat_context_initialize, 0);
  73.  
  74.     rb_define_method(cAVFormatContext, "metadata", r_avformat_context_metadata, 0);
  75. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement