static gboolean gst_ffmpegdeinterlace_sink_setcaps (GstPad * pad, GstCaps * caps) { GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (gst_pad_get_parent (pad)); GstStructure *structure = gst_caps_get_structure (caps, 0); AVCodecContext *ctx; if (!gst_structure_get_int (structure, "width", &deinterlace->width)) return FALSE; if (!gst_structure_get_int (structure, "height", &deinterlace->height)) return FALSE; ctx = avcodec_alloc_context (); ctx->width = deinterlace->width; ctx->height = deinterlace->height; ctx->pix_fmt = PIX_FMT_NB; gst_ffmpeg_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx); if (ctx->pix_fmt == PIX_FMT_NB) { av_free (ctx); return FALSE; } deinterlace->pixfmt = ctx->pix_fmt; av_free (ctx); deinterlace->to_size = avpicture_get_size (deinterlace->pixfmt, deinterlace->width, deinterlace->height); return gst_pad_set_caps (deinterlace->srcpad, caps); }
static gboolean gst_ffmpegscale_get_unit_size (GstBaseTransform * trans, GstCaps * caps, guint * size) { GstStructure *structure = gst_caps_get_structure (caps, 0); gint width, height; AVCodecContext *ctx; if (!gst_structure_get_int (structure, "width", &width)) return FALSE; if (!gst_structure_get_int (structure, "height", &height)) return FALSE; ctx = avcodec_alloc_context (); ctx->width = width; ctx->height = height; ctx->pix_fmt = PIX_FMT_NB; gst_ffmpeg_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx); if (ctx->pix_fmt == PIX_FMT_NB) { av_free (ctx); return FALSE; } *size = (guint) avpicture_get_size (ctx->pix_fmt, ctx->width, ctx->height); av_free (ctx); return TRUE; }
static gboolean gst_ffmpegscale_set_caps (GstBaseTransform * trans, GstCaps * incaps, GstCaps * outcaps) { GstFFMpegScale *scale = GST_FFMPEGSCALE (trans); GstStructure *instructure = gst_caps_get_structure (incaps, 0); GstStructure *outstructure = gst_caps_get_structure (outcaps, 0); gint par_num, par_den; AVCodecContext *ctx; if (!gst_structure_get_int (instructure, "width", &scale->in_width)) return FALSE; if (!gst_structure_get_int (instructure, "height", &scale->in_height)) return FALSE; if (!gst_structure_get_int (outstructure, "width", &scale->out_width)) return FALSE; if (!gst_structure_get_int (outstructure, "height", &scale->out_height)) return FALSE; if (gst_structure_get_fraction (instructure, "pixel-aspect-ratio", &par_num, &par_den)) { gst_structure_set (outstructure, "pixel-aspect-ratio", GST_TYPE_FRACTION, par_num * scale->in_width / scale->out_width, par_den * scale->in_height / scale->out_height, NULL); } ctx = avcodec_alloc_context (); ctx->width = scale->in_width; ctx->height = scale->in_height; ctx->pix_fmt = PIX_FMT_NB; gst_ffmpeg_caps_with_codectype (CODEC_TYPE_VIDEO, incaps, ctx); if (ctx->pix_fmt == PIX_FMT_NB) { av_free (ctx); return FALSE; } scale->pixfmt = ctx->pix_fmt; av_free (ctx); scale->res = img_resample_init (scale->out_width, scale->out_height, scale->in_width, scale->in_height); return TRUE; }
static gboolean gst_ffmpegdeinterlace_sink_setcaps (GstPad * pad, GstCaps * caps) { GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (gst_pad_get_parent (pad)); GstStructure *structure = gst_caps_get_structure (caps, 0); AVCodecContext *ctx; GstCaps *src_caps; gboolean ret; if (!gst_structure_get_int (structure, "width", &deinterlace->width)) return FALSE; if (!gst_structure_get_int (structure, "height", &deinterlace->height)) return FALSE; deinterlace->interlaced = FALSE; gst_structure_get_boolean (structure, "interlaced", &deinterlace->interlaced); gst_ffmpegdeinterlace_update_passthrough (deinterlace); ctx = avcodec_alloc_context (); ctx->width = deinterlace->width; ctx->height = deinterlace->height; ctx->pix_fmt = PIX_FMT_NB; gst_ffmpeg_caps_with_codectype (AVMEDIA_TYPE_VIDEO, caps, ctx); if (ctx->pix_fmt == PIX_FMT_NB) { av_free (ctx); return FALSE; } deinterlace->pixfmt = ctx->pix_fmt; av_free (ctx); deinterlace->to_size = avpicture_get_size (deinterlace->pixfmt, deinterlace->width, deinterlace->height); src_caps = gst_caps_copy (caps); gst_caps_set_simple (src_caps, "interlaced", G_TYPE_BOOLEAN, deinterlace->interlaced, NULL); ret = gst_pad_set_caps (deinterlace->srcpad, src_caps); gst_caps_unref (src_caps); return ret; }
static gboolean gst_ffmpegdeinterlace_sink_setcaps (GstPad * pad, GstCaps * caps) { GstFFMpegDeinterlace *deinterlace = GST_FFMPEGDEINTERLACE (gst_pad_get_parent (pad)); GstStructure *structure = gst_caps_get_structure (caps, 0); AVCodecContext *ctx; GValue interlaced = { 0 }; GstCaps *srcCaps; GstFlowReturn ret; if (!gst_structure_get_int (structure, "width", &deinterlace->width)) return FALSE; if (!gst_structure_get_int (structure, "height", &deinterlace->height)) return FALSE; ctx = avcodec_alloc_context (); ctx->width = deinterlace->width; ctx->height = deinterlace->height; ctx->pix_fmt = PIX_FMT_NB; gst_ffmpeg_caps_with_codectype (CODEC_TYPE_VIDEO, caps, ctx); if (ctx->pix_fmt == PIX_FMT_NB) { av_free (ctx); return FALSE; } deinterlace->pixfmt = ctx->pix_fmt; av_free (ctx); deinterlace->to_size = avpicture_get_size (deinterlace->pixfmt, deinterlace->width, deinterlace->height); srcCaps = gst_caps_copy (caps); g_value_init (&interlaced, G_TYPE_BOOLEAN); g_value_set_boolean (&interlaced, FALSE); gst_caps_set_value (srcCaps, "interlaced", &interlaced); g_value_unset (&interlaced); ret = gst_pad_set_caps (deinterlace->srcpad, srcCaps); gst_caps_unref (srcCaps); return ret; }
static gboolean gst_ffmpegenc_setcaps (GstPad * pad, GstCaps * caps) { GstCaps *other_caps; GstCaps *allowed_caps; GstCaps *icaps; enum PixelFormat pix_fmt; GstFFMpegEnc *ffmpegenc = (GstFFMpegEnc *) GST_PAD_PARENT (pad); GstFFMpegEncClass *oclass = (GstFFMpegEncClass *) G_OBJECT_GET_CLASS (ffmpegenc); /* close old session */ if (ffmpegenc->opened) { gst_ffmpeg_avcodec_close (ffmpegenc->context); ffmpegenc->opened = FALSE; } /* set defaults */ avcodec_get_context_defaults (ffmpegenc->context); /* if we set it in _getcaps we should set it also in _link */ ffmpegenc->context->strict_std_compliance = -1; /* user defined properties */ ffmpegenc->context->bit_rate = ffmpegenc->bitrate; ffmpegenc->context->bit_rate_tolerance = ffmpegenc->bitrate; ffmpegenc->context->gop_size = ffmpegenc->gop_size; ffmpegenc->context->me_method = ffmpegenc->me_method; GST_DEBUG_OBJECT (ffmpegenc, "Setting avcontext to bitrate %lu, gop_size %d", ffmpegenc->bitrate, ffmpegenc->gop_size); /* RTP payload used for GOB production (for Asterisk) */ if (ffmpegenc->rtp_payload_size) { ffmpegenc->context->rtp_payload_size = ffmpegenc->rtp_payload_size; } /* additional avcodec settings */ /* first fill in the majority by copying over */ gst_ffmpeg_cfg_fill_context (ffmpegenc, ffmpegenc->context); /* then handle some special cases */ ffmpegenc->context->lmin = (ffmpegenc->lmin * FF_QP2LAMBDA + 0.5); ffmpegenc->context->lmax = (ffmpegenc->lmax * FF_QP2LAMBDA + 0.5); if (ffmpegenc->interlaced) { ffmpegenc->context->flags |= CODEC_FLAG_INTERLACED_DCT | CODEC_FLAG_INTERLACED_ME; ffmpegenc->picture->interlaced_frame = TRUE; /* if this is not the case, a filter element should be used to swap fields */ ffmpegenc->picture->top_field_first = TRUE; } /* some other defaults */ ffmpegenc->context->rc_strategy = 2; ffmpegenc->context->b_frame_strategy = 0; ffmpegenc->context->coder_type = 0; ffmpegenc->context->context_model = 0; ffmpegenc->context->scenechange_threshold = 0; ffmpegenc->context->inter_threshold = 0; /* and last but not least the pass; CBR, 2-pass, etc */ ffmpegenc->context->flags |= ffmpegenc->pass; switch (ffmpegenc->pass) { /* some additional action depends on type of pass */ case CODEC_FLAG_QSCALE: ffmpegenc->context->global_quality = ffmpegenc->picture->quality = FF_QP2LAMBDA * ffmpegenc->quantizer; break; case CODEC_FLAG_PASS1: /* need to prepare a stats file */ /* we don't close when changing caps, fingers crossed */ if (!ffmpegenc->file) ffmpegenc->file = g_fopen (ffmpegenc->filename, "w"); if (!ffmpegenc->file) { GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, OPEN_WRITE, (("Could not open file \"%s\" for writing."), ffmpegenc->filename), GST_ERROR_SYSTEM); return FALSE; } break; case CODEC_FLAG_PASS2: { /* need to read the whole stats file ! */ gsize size; if (!g_file_get_contents (ffmpegenc->filename, &ffmpegenc->context->stats_in, &size, NULL)) { GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, READ, (("Could not get contents of file \"%s\"."), ffmpegenc->filename), GST_ERROR_SYSTEM); return FALSE; } break; } default: break; } /* fetch pix_fmt and so on */ gst_ffmpeg_caps_with_codectype (oclass->in_plugin->type, caps, ffmpegenc->context); if (!ffmpegenc->context->time_base.den) { ffmpegenc->context->time_base.den = 25; ffmpegenc->context->time_base.num = 1; ffmpegenc->context->ticks_per_frame = 1; } else if ((oclass->in_plugin->id == CODEC_ID_MPEG4) && (ffmpegenc->context->time_base.den > 65535)) { /* MPEG4 Standards do not support time_base denominator greater than * (1<<16) - 1 . We therefore scale them down. * Agreed, it will not be the exact framerate... but the difference * shouldn't be that noticeable */ ffmpegenc->context->time_base.num = (gint) gst_util_uint64_scale_int (ffmpegenc->context->time_base.num, 65535, ffmpegenc->context->time_base.den); ffmpegenc->context->time_base.den = 65535; GST_LOG_OBJECT (ffmpegenc, "MPEG4 : scaled down framerate to %d / %d", ffmpegenc->context->time_base.den, ffmpegenc->context->time_base.num); } pix_fmt = ffmpegenc->context->pix_fmt; /* max-key-interval may need the framerate set above */ if (ffmpegenc->max_key_interval) { AVCodecContext *ctx; /* override gop-size */ ctx = ffmpegenc->context; ctx->gop_size = (ffmpegenc->max_key_interval < 0) ? (-ffmpegenc->max_key_interval * (ctx->time_base.den * ctx->ticks_per_frame / ctx->time_base.num)) : ffmpegenc->max_key_interval; } /* open codec */ if (gst_ffmpeg_avcodec_open (ffmpegenc->context, oclass->in_plugin) < 0) { if (ffmpegenc->context->priv_data) gst_ffmpeg_avcodec_close (ffmpegenc->context); if (ffmpegenc->context->stats_in) g_free (ffmpegenc->context->stats_in); GST_DEBUG_OBJECT (ffmpegenc, "ffenc_%s: Failed to open FFMPEG codec", oclass->in_plugin->name); return FALSE; } /* second pass stats buffer no longer needed */ if (ffmpegenc->context->stats_in) g_free (ffmpegenc->context->stats_in); /* is the colourspace correct? */ if (pix_fmt != ffmpegenc->context->pix_fmt) { gst_ffmpeg_avcodec_close (ffmpegenc->context); GST_DEBUG_OBJECT (ffmpegenc, "ffenc_%s: AV wants different colourspace (%d given, %d wanted)", oclass->in_plugin->name, pix_fmt, ffmpegenc->context->pix_fmt); return FALSE; } /* we may have failed mapping caps to a pixfmt, * and quite some codecs do not make up their own mind about that * in any case, _NONE can never work out later on */ if (oclass->in_plugin->type == CODEC_TYPE_VIDEO && pix_fmt == PIX_FMT_NONE) { GST_DEBUG_OBJECT (ffmpegenc, "ffenc_%s: Failed to determine input format", oclass->in_plugin->name); return FALSE; } /* some codecs support more than one format, first auto-choose one */ GST_DEBUG_OBJECT (ffmpegenc, "picking an output format ..."); allowed_caps = gst_pad_get_allowed_caps (ffmpegenc->srcpad); if (!allowed_caps) { GST_DEBUG_OBJECT (ffmpegenc, "... but no peer, using template caps"); /* we need to copy because get_allowed_caps returns a ref, and * get_pad_template_caps doesn't */ allowed_caps = gst_caps_copy (gst_pad_get_pad_template_caps (ffmpegenc->srcpad)); } GST_DEBUG_OBJECT (ffmpegenc, "chose caps %" GST_PTR_FORMAT, allowed_caps); gst_ffmpeg_caps_with_codecid (oclass->in_plugin->id, oclass->in_plugin->type, allowed_caps, ffmpegenc->context); /* try to set this caps on the other side */ other_caps = gst_ffmpeg_codecid_to_caps (oclass->in_plugin->id, ffmpegenc->context, TRUE); if (!other_caps) { gst_ffmpeg_avcodec_close (ffmpegenc->context); GST_DEBUG ("Unsupported codec - no caps found"); return FALSE; } icaps = gst_caps_intersect (allowed_caps, other_caps); gst_caps_unref (allowed_caps); gst_caps_unref (other_caps); if (gst_caps_is_empty (icaps)) { gst_caps_unref (icaps); return FALSE; } if (gst_caps_get_size (icaps) > 1) { GstCaps *newcaps; newcaps = gst_caps_new_full (gst_structure_copy (gst_caps_get_structure (icaps, 0)), NULL); gst_caps_unref (icaps); icaps = newcaps; } if (!gst_pad_set_caps (ffmpegenc->srcpad, icaps)) { gst_ffmpeg_avcodec_close (ffmpegenc->context); gst_caps_unref (icaps); return FALSE; } gst_caps_unref (icaps); /* success! */ ffmpegenc->opened = TRUE; return TRUE; }