2012-09-14 15:51:26 +00:00
|
|
|
/*
|
2012-12-28 10:41:30 +00:00
|
|
|
* muxing using libavformat
|
2017-06-13 18:22:15 +00:00
|
|
|
*
|
2012-09-14 15:51:26 +00:00
|
|
|
* Copyright (C) 2011 Rudolf Polzer <divVerent@xonotic.org>
|
|
|
|
*
|
2012-12-28 10:41:30 +00:00
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2017-06-13 18:22:15 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2012-09-14 15:51:26 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2012-09-14 15:51:26 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-06-13 18:22:15 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2012-09-14 15:51:26 +00:00
|
|
|
*
|
2017-06-13 18:22:15 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2012-09-14 15:51:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MPLAYER_ENCODE_LAVC_H
|
|
|
|
#define MPLAYER_ENCODE_LAVC_H
|
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavutil/avstring.h>
|
|
|
|
#include <libavutil/pixfmt.h>
|
|
|
|
#include <libavutil/opt.h>
|
|
|
|
#include <libavutil/mathematics.h>
|
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
#include "common/common.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
#include "encode.h"
|
2023-10-21 02:55:41 +00:00
|
|
|
#include "osdep/threads.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/csputils.h"
|
2012-09-14 15:51:26 +00:00
|
|
|
|
|
|
|
struct encode_lavc_context {
|
2018-04-22 17:40:36 +00:00
|
|
|
// --- Immutable after init
|
2013-12-21 19:11:04 +00:00
|
|
|
struct mpv_global *global;
|
2014-06-11 00:04:02 +00:00
|
|
|
struct encode_opts *options;
|
2013-12-21 19:11:04 +00:00
|
|
|
struct mp_log *log;
|
2018-04-22 17:40:36 +00:00
|
|
|
struct encode_priv *priv;
|
2021-05-01 19:53:56 +00:00
|
|
|
const AVOutputFormat *oformat;
|
2018-04-22 17:40:36 +00:00
|
|
|
const char *filename;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2014-03-08 22:38:53 +00:00
|
|
|
// 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.
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex lock;
|
2014-03-08 22:38:53 +00:00
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
// anti discontinuity mode
|
|
|
|
double next_in_pts;
|
|
|
|
double discontinuity_pts_offset;
|
2018-04-22 17:40:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// --- 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;
|
2021-05-01 19:53:56 +00:00
|
|
|
const AVOutputFormat *oformat;
|
2018-04-22 17:40:36 +00:00
|
|
|
|
|
|
|
// (avoid using this)
|
|
|
|
struct encode_lavc_context *encode_lavc_ctx;
|
|
|
|
|
|
|
|
enum stream_type type;
|
2012-09-14 15:51:26 +00:00
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
// (different access restrictions before/after encoder init)
|
|
|
|
struct encoder_stream_info info;
|
|
|
|
AVCodecContext *encoder;
|
|
|
|
struct mux_stream *mux_stream;
|
|
|
|
|
2021-12-13 15:29:41 +00:00
|
|
|
// (essentially private)
|
2018-04-22 17:40:36 +00:00
|
|
|
struct stream *twopass_bytebuffer;
|
2021-12-13 15:29:41 +00:00
|
|
|
AVPacket *pkt;
|
2012-09-14 15:51:26 +00:00
|
|
|
};
|
|
|
|
|
2018-04-22 17:40:36 +00:00
|
|
|
// 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.
|
2018-04-29 17:42:18 +00:00
|
|
|
// on_ready is called as soon as the muxer has been initialized. Then you are
|
|
|
|
// allowed to write packets with encoder_encode().
|
|
|
|
// Warning: the on_ready callback is called asynchronously, so you need to
|
|
|
|
// make sure to properly synchronize everything.
|
|
|
|
bool encoder_init_codec_and_muxer(struct encoder_context *p,
|
|
|
|
void (*on_ready)(void *ctx), void *ctx);
|
2018-04-22 17:40:36 +00:00
|
|
|
|
|
|
|
// Encode the frame and write the packet. frame is ref'ed as need.
|
|
|
|
bool encoder_encode(struct encoder_context *p, AVFrame *frame);
|
|
|
|
|
2020-03-22 12:06:59 +00:00
|
|
|
// Return muxer timebase (only available after on_ready() has been called).
|
|
|
|
// Caller needs to acquire encode_lavc_context.lock (or call it from on_ready).
|
|
|
|
AVRational encoder_get_mux_timebase_unlocked(struct encoder_context *p);
|
|
|
|
|
2024-05-05 14:42:28 +00:00
|
|
|
void encoder_update_log(struct mpv_global *global);
|
|
|
|
|
2012-09-14 15:51:26 +00:00
|
|
|
#endif
|