static void init (GeglProperties *o) { Priv *p = (Priv*)o->user_data; static gint av_inited = 0; if (av_inited == 0) { av_register_all (); av_inited = 1; } if (p == NULL) { p = g_new0 (Priv, 1); o->user_data = (void*) p; } p->width = 320; p->height = 200; clear_audio_track (o); p->loadedfilename = g_strdup (""); ff_cleanup (o); av_log_set_level (AV_LOG_WARNING); }
static void finalize (GObject *object) { GeglProperties *o = GEGL_PROPERTIES (object); if (o->user_data) { Priv *p = (Priv*)o->user_data; ff_cleanup (o); g_free (p->loadedfilename); g_free (o->user_data); o->user_data = NULL; } G_OBJECT_CLASS (gegl_op_parent_class)->finalize (object); }
static void prepare (GeglOperation *operation) { GeglChantO *o = GEGL_CHANT_PROPERTIES (operation); Priv *p = (Priv*)o->chant_data; if (p == NULL) init (o); p = (Priv*)o->chant_data; g_assert (o->chant_data != NULL); gegl_operation_set_format (operation, "output", babl_format ("R'G'B'A u8")); if (!p->loadedfilename || strcmp (p->loadedfilename, o->path)) { gint i; gint err; ff_cleanup (o); err = av_open_input_file (&p->ic, o->path, NULL, 0, NULL); if (err < 0) { print_error (o->path, err); } err = av_find_stream_info (p->ic); if (err < 0) { g_warning ("ff-load: error finding stream info for %s", o->path); return; } for (i = 0; i< p->ic->nb_streams; i++) { AVCodecContext *c = p->ic->streams[i]->codec; #if LIBAVFORMAT_VERSION_MAJOR >= 53 if (c->codec_type == AVMEDIA_TYPE_VIDEO) #else if (c->codec_type == CODEC_TYPE_VIDEO) #endif { p->video_st = p->ic->streams[i]; p->video_stream = i; } } p->enc = p->video_st->codec; p->codec = avcodec_find_decoder (p->enc->codec_id); /* p->enc->error_resilience = 2; */ p->enc->error_concealment = 3; p->enc->workaround_bugs = FF_BUG_AUTODETECT; if (p->codec == NULL) { g_warning ("codec not found"); } if (p->codec->capabilities & CODEC_CAP_TRUNCATED) p->enc->flags |= CODEC_FLAG_TRUNCATED; if (avcodec_open (p->enc, p->codec) < 0) { g_warning ("error opening codec %s", p->enc->codec->name); return; } p->width = p->enc->width; p->height = p->enc->height; p->frames = 10000000; p->lavc_frame = avcodec_alloc_frame (); if (p->fourcc) g_free (p->fourcc); p->fourcc = g_strdup ("none"); p->fourcc[0] = (p->enc->codec_tag) & 0xff; p->fourcc[1] = (p->enc->codec_tag >> 8) & 0xff; p->fourcc[2] = (p->enc->codec_tag >> 16) & 0xff; p->fourcc[3] = (p->enc->codec_tag >> 24) & 0xff; if (p->codec_name) g_free (p->codec_name); if (p->codec->name) { p->codec_name = g_strdup (p->codec->name); } else { p->codec_name = g_strdup (""); } if (p->loadedfilename) g_free (p->loadedfilename); p->loadedfilename = g_strdup (o->path); p->prevframe = -1; p->coded_bytes = 0; p->coded_buf = NULL; }
static void prepare (GeglOperation *operation) { GeglProperties *o = GEGL_PROPERTIES (operation); Priv *p = (Priv*)o->user_data; if (p == NULL) init (o); p = (Priv*)o->user_data; g_assert (o->user_data != NULL); gegl_operation_set_format (operation, "output", babl_format ("R'G'B' u8")); if (!p->loadedfilename || strcmp (p->loadedfilename, o->path) || p->prevframe > o->frame /* a bit heavy handed, but improves consistency */ ) { gint i; gint err; ff_cleanup (o); err = avformat_open_input(&p->video_fcontext, o->path, NULL, 0); if (err < 0) { print_error (o->path, err); } err = avformat_find_stream_info (p->video_fcontext, NULL); if (err < 0) { g_warning ("ff-load: error finding stream info for %s", o->path); return; } err = avformat_open_input(&p->audio_fcontext, o->path, NULL, 0); if (err < 0) { print_error (o->path, err); } err = avformat_find_stream_info (p->audio_fcontext, NULL); if (err < 0) { g_warning ("ff-load: error finding stream info for %s", o->path); return; } for (i = 0; i< p->video_fcontext->nb_streams; i++) { AVCodecContext *c = p->video_fcontext->streams[i]->codec; if (c->codec_type == AVMEDIA_TYPE_VIDEO) { p->video_stream = p->video_fcontext->streams[i]; p->video_index = i; } if (c->codec_type == AVMEDIA_TYPE_AUDIO) { p->audio_stream = p->audio_fcontext->streams[i]; p->audio_index = i; } } p->video_codec = avcodec_find_decoder (p->video_stream->codec->codec_id); if (p->audio_stream) { p->audio_codec = avcodec_find_decoder (p->audio_stream->codec->codec_id); if (p->audio_codec == NULL) g_warning ("audio codec not found"); else if (avcodec_open2 (p->audio_stream->codec, p->audio_codec, NULL) < 0) { g_warning ("error opening codec %s", p->audio_stream->codec->codec->name); } else { o->audio_sample_rate = p->audio_stream->codec->sample_rate; o->audio_channels = MIN(p->audio_stream->codec->channels, GEGL_MAX_AUDIO_CHANNELS); } } p->video_stream->codec->err_recognition = AV_EF_IGNORE_ERR | AV_EF_BITSTREAM | AV_EF_BUFFER; p->video_stream->codec->workaround_bugs = FF_BUG_AUTODETECT; if (p->video_codec == NULL) g_warning ("video codec not found"); if (avcodec_open2 (p->video_stream->codec, p->video_codec, NULL) < 0) { g_warning ("error opening codec %s", p->video_stream->codec->codec->name); return; } p->width = p->video_stream->codec->width; p->height = p->video_stream->codec->height; p->lavc_frame = av_frame_alloc (); if (o->video_codec) g_free (o->video_codec); if (p->video_codec->name) o->video_codec = g_strdup (p->video_codec->name); else o->video_codec = g_strdup (""); if (o->audio_codec) g_free (o->audio_codec); if (p->audio_codec && p->audio_codec->name) o->audio_codec = g_strdup (p->audio_codec->name); else o->audio_codec = g_strdup (""); if (p->loadedfilename) g_free (p->loadedfilename); p->loadedfilename = g_strdup (o->path); p->prevframe = -1; p->a_prevframe = -1; o->frames = p->video_stream->nb_frames; o->frame_rate = av_q2d (av_guess_frame_rate (p->video_fcontext, p->video_stream, NULL)); if (!o->frames) { /* this is a guesstimate of frame-count */ o->frames = p->video_fcontext->duration * o->frame_rate / AV_TIME_BASE; /* make second guess for things like luxo */ if (o->frames < 1) o->frames = 23; } #if 0 { int m ,h; int s = o->frames / o->frame_rate; m = s / 60; s -= m * 60; h = m / 60; m -= h * 60; fprintf (stdout, "duration: %02i:%02i:%02i\n", h, m, s); } #endif p->codec_delay = p->video_stream->codec->delay; if (!strcmp (o->video_codec, "mpeg1video")) p->codec_delay = 1; else if (!strcmp (o->video_codec, "h264")) { if (strstr (p->video_fcontext->filename, ".mp4") || strstr (p->video_fcontext->filename, ".MP4")) /* XXX: too hacky, isn't there an avformat thing to use?, or perhaps we can measure this when decoding the first frame. */ p->codec_delay = 3; else p->codec_delay = 0; } clear_audio_track (o); } }