Exemple #1
0
/* gst_x265_enc_set_src_caps
 * Returns: TRUE on success.
 */
static gboolean
gst_x265_enc_set_src_caps (GstX265Enc * encoder, GstCaps * caps)
{
  GstCaps *outcaps;
  GstStructure *structure;
  GstVideoCodecState *state;
  GstTagList *tags;

  outcaps = gst_caps_new_empty_simple ("video/x-h265");
  structure = gst_caps_get_structure (outcaps, 0);

  gst_structure_set (structure, "stream-format", G_TYPE_STRING, "byte-stream",
      NULL);
  gst_structure_set (structure, "alignment", G_TYPE_STRING, "au", NULL);

  if (!gst_x265_enc_set_level_tier_and_profile (encoder, outcaps)) {
    gst_caps_unref (outcaps);
    return FALSE;
  }

  state = gst_video_encoder_set_output_state (GST_VIDEO_ENCODER (encoder),
      outcaps, encoder->input_state);
  GST_DEBUG_OBJECT (encoder, "output caps: %" GST_PTR_FORMAT, state->caps);
  gst_video_codec_state_unref (state);

  tags = gst_tag_list_new_empty ();
  gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, "x265",
      GST_TAG_ENCODER_VERSION, x265_version_str, NULL);
  gst_video_encoder_merge_tags (GST_VIDEO_ENCODER (encoder), tags,
      GST_TAG_MERGE_REPLACE);
  gst_tag_list_unref (tags);

  return TRUE;
}
static gboolean
gst_vaapiencode_set_format (GstVideoEncoder * venc, GstVideoCodecState * state)
{
  GstVaapiEncode *const encode = GST_VAAPIENCODE_CAST (venc);
  gboolean ret;

  g_return_val_if_fail (state->caps != NULL, FALSE);

  if (!set_codec_state (encode, state))
    return FALSE;

  if (!gst_vaapi_plugin_base_set_caps (GST_VAAPI_PLUGIN_BASE (encode),
          state->caps, NULL))
    return FALSE;

  if (encode->input_state)
    gst_video_codec_state_unref (encode->input_state);
  encode->input_state = gst_video_codec_state_ref (state);
  encode->input_state_changed = TRUE;

  ret = gst_pad_start_task (GST_VAAPI_PLUGIN_BASE_SRC_PAD (encode),
      (GstTaskFunction) gst_vaapiencode_buffer_loop, encode, NULL);

  if (!ret)
    return FALSE;

  /* Store some tags */
  {
    GstTagList *tags = gst_tag_list_new_empty ();
    const gchar *encoder, *codec;
    guint bitrate = 0;

    g_object_get (encode, "bitrate", &bitrate, NULL);
    gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_NOMINAL_BITRATE,
        bitrate, NULL);

    if ((encoder =
            gst_element_class_get_metadata (GST_ELEMENT_GET_CLASS (encode),
                GST_ELEMENT_METADATA_LONGNAME)))
      gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, encoder,
          NULL);

    if ((codec =
            gst_vaapi_codec_get_name (gst_vaapi_profile_get_codec
                (gst_vaapi_profile_from_caps (state->caps)))))
      gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_CODEC, codec,
          NULL);

    gst_video_encoder_merge_tags (venc, tags, GST_TAG_MERGE_REPLACE);
    gst_tag_list_unref (tags);
  }

  return TRUE;
}
Exemple #3
0
static gboolean
gst_ffmpegvidenc_set_format (GstVideoEncoder * encoder,
                             GstVideoCodecState * state)
{
    GstCaps *other_caps;
    GstCaps *allowed_caps;
    GstCaps *icaps;
    GstVideoCodecState *output_format;
    enum AVPixelFormat pix_fmt;
    GstFFMpegVidEnc *ffmpegenc = (GstFFMpegVidEnc *) encoder;
    GstFFMpegVidEncClass *oclass =
        (GstFFMpegVidEncClass *) G_OBJECT_GET_CLASS (ffmpegenc);

    /* close old session */
    if (ffmpegenc->opened) {
        gst_ffmpeg_avcodec_close (ffmpegenc->context);
        ffmpegenc->opened = FALSE;
        if (avcodec_get_context_defaults3 (ffmpegenc->context,
                                           oclass->in_plugin) < 0) {
            GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
            return FALSE;
        }
    }

    /* if we set it in _getcaps we should set it also in _link */
    ffmpegenc->context->strict_std_compliance = ffmpegenc->compliance;

    /* 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 %d, gop_size %d",
                      ffmpegenc->bitrate, ffmpegenc->gop_size);

    if (ffmpegenc->max_threads == 0) {
        if (!(oclass->in_plugin->capabilities & CODEC_CAP_AUTO_THREADS))
            ffmpegenc->context->thread_count = gst_ffmpeg_auto_max_threads ();
        else
            ffmpegenc->context->thread_count = 0;
    } else
        ffmpegenc->context->thread_count = ffmpegenc->max_threads;

    /* 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;
    }

    /* 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;

    /* 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)
            goto open_file_err;
        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))
            goto file_read_err;

        break;
    }
    default:
        break;
    }

    GST_DEBUG_OBJECT (ffmpegenc, "Extracting common video information");
    /* fetch pix_fmt, fps, par, width, height... */
    gst_ffmpeg_videoinfo_to_context (&state->info, ffmpegenc->context);

    /* sanitize time base */
    if (ffmpegenc->context->time_base.num <= 0
            || ffmpegenc->context->time_base.den <= 0)
        goto insane_timebase;

    if ((oclass->in_plugin->id == AV_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;
    }

    /* 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 (GST_VIDEO_ENCODER_SRC_PAD (encoder));
    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_pad_get_pad_template_caps (GST_VIDEO_ENCODER_SRC_PAD (encoder));
    }
    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);

    /* open codec */
    if (gst_ffmpeg_avcodec_open (ffmpegenc->context, oclass->in_plugin) < 0) {
        gst_caps_unref (allowed_caps);
        goto open_codec_fail;
    }

    /* is the colourspace correct? */
    if (pix_fmt != ffmpegenc->context->pix_fmt) {
        gst_caps_unref (allowed_caps);
        goto pix_fmt_err;
    }

    /* 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 (pix_fmt == AV_PIX_FMT_NONE) {
        gst_caps_unref (allowed_caps);
        goto bad_input_fmt;
    }

    /* second pass stats buffer no longer needed */
    g_free (ffmpegenc->context->stats_in);

    /* 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_caps_unref (allowed_caps);
        goto unsupported_codec;
    }

    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);
        goto unsupported_codec;
    }
    icaps = gst_caps_fixate (icaps);

    GST_DEBUG_OBJECT (ffmpegenc, "codec flags 0x%08x", ffmpegenc->context->flags);

    /* Store input state and set output state */
    if (ffmpegenc->input_state)
        gst_video_codec_state_unref (ffmpegenc->input_state);
    ffmpegenc->input_state = gst_video_codec_state_ref (state);

    output_format = gst_video_encoder_set_output_state (encoder, icaps, state);
    gst_video_codec_state_unref (output_format);

    /* Store some tags */
    {
        GstTagList *tags = gst_tag_list_new_empty ();
        const gchar *codec;

        gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_NOMINAL_BITRATE,
                          (guint) ffmpegenc->context->bit_rate, NULL);

        if ((codec =
                    gst_ffmpeg_get_codecid_longname (ffmpegenc->context->codec_id)))
            gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_VIDEO_CODEC, codec,
                              NULL);

        gst_video_encoder_merge_tags (encoder, tags, GST_TAG_MERGE_REPLACE);
        gst_tag_list_unref (tags);
    }

    /* success! */
    ffmpegenc->opened = TRUE;

    return TRUE;

    /* ERRORS */
open_file_err:
    {
        GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, OPEN_WRITE,
                           (("Could not open file \"%s\" for writing."), ffmpegenc->filename),
                           GST_ERROR_SYSTEM);
        return FALSE;
    }
file_read_err:
    {
        GST_ELEMENT_ERROR (ffmpegenc, RESOURCE, READ,
                           (("Could not get contents of file \"%s\"."), ffmpegenc->filename),
                           GST_ERROR_SYSTEM);
        return FALSE;
    }

insane_timebase:
    {
        GST_ERROR_OBJECT (ffmpegenc, "Rejecting time base %d/%d",
                          ffmpegenc->context->time_base.den, ffmpegenc->context->time_base.num);
        goto cleanup_stats_in;
    }
unsupported_codec:
    {
        GST_DEBUG ("Unsupported codec - no caps found");
        goto cleanup_stats_in;
    }
open_codec_fail:
    {
        GST_DEBUG_OBJECT (ffmpegenc, "avenc_%s: Failed to open libav codec",
                          oclass->in_plugin->name);
        goto close_codec;
    }

pix_fmt_err:
    {
        GST_DEBUG_OBJECT (ffmpegenc,
                          "avenc_%s: AV wants different colourspace (%d given, %d wanted)",
                          oclass->in_plugin->name, pix_fmt, ffmpegenc->context->pix_fmt);
        goto close_codec;
    }

bad_input_fmt:
    {
        GST_DEBUG_OBJECT (ffmpegenc, "avenc_%s: Failed to determine input format",
                          oclass->in_plugin->name);
        goto close_codec;
    }
close_codec:
    {
        gst_ffmpeg_avcodec_close (ffmpegenc->context);
        if (avcodec_get_context_defaults3 (ffmpegenc->context,
                                           oclass->in_plugin) < 0)
            GST_DEBUG_OBJECT (ffmpegenc, "Failed to set context defaults");
        goto cleanup_stats_in;
    }
cleanup_stats_in:
    {
        g_free (ffmpegenc->context->stats_in);
        return FALSE;
    }
}