path: root/common/encode_lavc.h
diff options
authorwm4 <wm4@nowhere>2018-04-22 19:40:36 +0200
committerJan Ekström <>2018-04-29 02:21:32 +0300
commit6c8362ef54f4e90476553cb6b64996cc414da06d (patch)
treeab3fa75a18958bacc08c473304f59519a58f5969 /common/encode_lavc.h
parent8135e25600ace2894df274e6a825cfef525fee77 (diff)
encode: rewrite half of it
The main change is that we wait with opening the muxer ("writing headers") until we have data from all streams. This fixes race conditions at init due to broken assumptions in the old code. This also changes a lot of other stuff. I found and fixed a few API violations (often things for which better mechanisms were invented, and the old ones are not valid anymore). I try to get away from the public mutex and shared fields in encode_lavc_context. For now it's still needed for some timestamp-related fields, but most are gone. It also removes some bad code duplication between audio and video paths.
Diffstat (limited to 'common/encode_lavc.h')
1 files changed, 51 insertions, 54 deletions
diff --git a/common/encode_lavc.h b/common/encode_lavc.h
index 467520cc6d..a689b1ea47 100644
--- a/common/encode_lavc.h
+++ b/common/encode_lavc.h
@@ -31,43 +31,26 @@
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
+#include "common/common.h"
#include "encode.h"
#include "video/csputils.h"
struct encode_lavc_context {
+ // --- Immutable after init
struct mpv_global *global;
struct encode_opts *options;
struct mp_log *log;
- struct mp_tags *metadata;
+ struct encode_priv *priv;
+ AVOutputFormat *oformat;
+ const char *filename;
// All entry points must be guarded with the lock. Functions called by
// the playback core lock this automatically, but ao_lavc.c and vo_lavc.c
// must lock manually before accessing state.
pthread_mutex_t lock;
- float vo_fps;
- // FFmpeg contexts.
- AVFormatContext *avc;
- AVStream *vst;
- AVStream *ast;
- AVCodecContext *vcc;
- AVCodecContext *acc;
- // these are processed from the options
- AVRational timebase;
- AVCodec *vc;
- AVCodec *ac;
- AVDictionary *foptions;
- AVDictionary *aoptions;
- AVDictionary *voptions;
- // values created during encoding
- int header_written; // -1 means currently writing
// sync to audio mode
double audio_pts_offset;
- double last_video_in_pts;
double last_audio_in_pts;
int64_t samples_since_last_pts;
@@ -75,40 +58,54 @@ struct encode_lavc_context {
// anti discontinuity mode
double next_in_pts;
double discontinuity_pts_offset;
+// --- interface for vo/ao drivers
+// Static information after encoder init. This never changes (even if there are
+// dynamic runtime changes, they have to work over AVPacket side data).
+// For use in encoder_context, most fields are copied from encoder_context.encoder
+// by encoder_init_codec_and_muxer().
+struct encoder_stream_info {
+ AVRational timebase; // timebase used by the encoder (in frames/out packets)
+ AVCodecParameters *codecpar;
+// The encoder parts for each stream (no muxing parts included).
+// This is private to each stream.
+struct encoder_context {
+ struct mpv_global *global;
+ struct encode_opts *options;
+ struct mp_log *log;
+ AVOutputFormat *oformat;
+ // (avoid using this)
+ struct encode_lavc_context *encode_lavc_ctx;
+ enum stream_type type;
- long long abytes;
- long long vbytes;
- struct stream *twopass_bytebuffer_a;
- struct stream *twopass_bytebuffer_v;
- double t0;
- unsigned int frames;
- double audioseconds;
- bool expect_video;
- bool expect_audio;
- bool video_first;
- bool audio_first;
- // has encoding failed?
- bool failed;
+ // (different access restrictions before/after encoder init)
+ struct encoder_stream_info info;
+ AVCodecContext *encoder;
+ struct mux_stream *mux_stream;
+ struct stream *twopass_bytebuffer;
-// interface for vo/ao drivers
-int encode_lavc_alloc_stream(struct encode_lavc_context *ctx,
- enum AVMediaType mt, AVStream **stream_out,
- AVCodecContext **codec_out);
-void encode_lavc_write_stats(struct encode_lavc_context *ctx,
- AVCodecContext *stream);
-int encode_lavc_write_frame(struct encode_lavc_context *ctx, AVStream *stream,
- AVPacket *packet);
-int encode_lavc_supports_pixfmt(struct encode_lavc_context *ctx, enum AVPixelFormat format);
-int encode_lavc_open_codec(struct encode_lavc_context *ctx,
- AVCodecContext *codec);
-int encode_lavc_available(struct encode_lavc_context *ctx);
-int encode_lavc_timesyncfailed(struct encode_lavc_context *ctx);
-int encode_lavc_start(struct encode_lavc_context *ctx); // returns 1 on success
-double encode_lavc_getoffset(struct encode_lavc_context *ctx,
- AVCodecContext *codec);
-void encode_lavc_fail(struct encode_lavc_context *ctx, const char *format, ...); // report failure of encoding
+// Free with talloc_free(). (Keep in mind actual deinitialization requires
+// sending a flush packet.)
+// This can fail and return NULL.
+struct encoder_context *encoder_context_alloc(struct encode_lavc_context *ctx,
+ enum stream_type type,
+ struct mp_log *log);
+// After setting your codec parameters on p->encoder, you call this to "open"
+// the encoder. This also initializes p->mux_stream. Returns false on failure.
+bool encoder_init_codec_and_muxer(struct encoder_context *p);
+// Encode the frame and write the packet. frame is ref'ed as need.
+bool encoder_encode(struct encoder_context *p, AVFrame *frame);
+double encoder_get_offset(struct encoder_context *p);