public VideoEncoder(int width, int height, int fps) { _converter = new VideoConverter(CODEC_PIXEL_FORMAT); AVCodec* codec = FFmpegInvoke.avcodec_find_encoder(CODEC_ID); if (codec == null) throw new Exception("Codec not found"); _codec_context = FFmpegInvoke.avcodec_alloc_context3(codec); if (_codec_context == null) throw new Exception("Could not allocate video codec context"); _codec_context->bit_rate = 50000; _codec_context->width = width; _codec_context->height = height; _codec_context->time_base = new AVRational() { num = 1, den = fps }; _codec_context->gop_size = 10; // emit one intra frame every ten frames _codec_context->max_b_frames = 1; _codec_context->pix_fmt = CODEC_PIXEL_FORMAT; FFmpegInvoke.av_opt_set(_codec_context->priv_data, "preset", "fast", 0); if (FFmpegInvoke.avcodec_open2(_codec_context, codec, null) < 0) throw new Exception("Could not open codec"); _avFrameYUV = FFmpegInvoke.avcodec_alloc_frame(); if (_avFrameYUV == null) throw new Exception("Could not allocate video frame"); _avFrameYUV->format = (int)CODEC_PIXEL_FORMAT; _avFrameYUV->width = width; _avFrameYUV->height = height; var ret1 = FFmpegInvoke.av_image_alloc(&_avFrameYUV->data_0, _avFrameYUV->linesize, width, height, CODEC_PIXEL_FORMAT, 32); if (ret1 < 0) throw new Exception("Could not allocate raw picture buffer"); _avFrameBGR = FFmpegInvoke.avcodec_alloc_frame(); if (_avFrameBGR == null) throw new Exception("Could not allocate video frame"); _avFrameBGR->format = (int)INPUT_PIXEL_FORMAT; _avFrameBGR->width = width; _avFrameBGR->height = height; var ret2 = FFmpegInvoke.av_image_alloc(&_avFrameBGR->data_0, _avFrameBGR->linesize, width, height, INPUT_PIXEL_FORMAT, 32); if (ret2 < 0) throw new Exception("Could not allocate raw picture buffer"); }
public static extern int av_compare_ts(long ts_a, AVRational tb_a, long ts_b, AVRational tb_b);
public static extern long av_rescale_q_rnd(long a, AVRational bq, AVRational cq, AVRounding p3);
/// <summary> /// Initialize a <see cref="Rational"/>. /// </summary> /// <param name="AValue">The <see cref="Unsafe.AVRational"/>.</param> internal Rational(Unsafe.AVRational AValue) { Value = AValue; Reduce(ref this); }
public static extern void av_stream_set_r_frame_rate(AVStream* s, AVRational r);
public static extern int av_opt_set_video_rate(void* obj, String name, AVRational val, int search_flags);
public static extern AVOption* av_set_q(void* obj, String name, AVRational n);
public static extern void av_codec_set_pkt_timebase(AVCodecContext* avctx, AVRational val);
public static extern AVRational av_div_q(AVRational b, AVRational c);
public static extern AVRational av_mul_q(AVRational b, AVRational c);
private static string ToDescString(AVRational value) { double dVal = Math.Floor(ToDouble(value) * 10.0) / 10; return dVal.ToString(); }
private static double ToDouble(AVRational value) { return Convert.ToDouble(value.num) / Convert.ToDouble(value.den); }
/// <summary> /// Converts seconds to a timestamp value. /// </summary> /// <param name="seconds">The seconds.</param> /// <param name="streamTimeBase">The stream time base.</param> /// <returns></returns> public static long SecondsToTimestamp(decimal seconds, AVRational streamTimeBase) { return Convert.ToInt64(Convert.ToDouble(seconds) * Convert.ToDouble(streamTimeBase.den) / Convert.ToDouble(streamTimeBase.num)); }
/// <summary> /// Converts a Timestamp to seconds. /// </summary> /// <param name="ts">The ts.</param> /// <param name="streamTimeBase">The stream time base.</param> /// <returns></returns> public static decimal TimestampToSeconds(long ts, AVRational streamTimeBase) { return Convert.ToDecimal(Convert.ToDouble(ts) * Convert.ToDouble(streamTimeBase.num) / Convert.ToDouble(streamTimeBase.den)); }
private double q2d(AVRational r) { return (double)r.num / (double)r.den; }
public static extern long av_rescale_delta(AVRational in_tb, long in_ts, AVRational fs_tb, int duration, long* last, AVRational out_tb);
public static extern long av_add_stable(AVRational ts_tb, long ts, AVRational inc_tb, long inc);
public static extern AVRational av_add_q(AVRational b, AVRational c);
public static extern void av_packet_rescale_ts(AVPacket* pkt, AVRational tb_src, AVRational tb_dst);
public static extern AVRational av_sub_q(AVRational b, AVRational c);
public static extern int av_opt_eval_q(void* obj, AVOption* o, String val, AVRational* q_out);
public static extern int av_nearer_q(AVRational q, AVRational q1, AVRational q2);
public static extern int av_opt_get_video_rate(void* obj, String name, int search_flags, AVRational* out_val);
public static extern int av_find_nearest_q_idx(AVRational q, AVRational* q_list);
public static extern int av_image_check_sar(int w, int h, AVRational sar);
public static extern long av_rescale_q(long a, AVRational bq, AVRational cq);