// What the live conversion's files share: the job, the state of one run,
// and the muxing and encoding steps the picture's encode (hdff_picture.c)
// takes from the run (hdff_hls.c).
#ifndef HDFF_CONVERT_H
#define HDFF_CONVERT_H

#include "hdff_internal.h"

#include <pthread.h>
#include <stdatomic.h>

#include <libavfilter/avfilter.h>

// args.go's segmentSeconds.
#define SEGMENT_SECONDS 2
// Packets kept for the muxer while the encoder waits for its first picture.
#define PENDING_MAX 1024
// The deepest picture reordering a copy's derived decode times follow,
// libavformat's own bound.
#define MAX_REORDER_DELAY 16

struct hdff_job {
    pthread_t thread;
    atomic_int cancelled;
    int joined;
    int result;
    char *input;
    char *playlist;
    double start_seconds;
    int max_height;
    int allow_software_encoder;
    double read_rate;
    hdff_callbacks cb;
};

typedef struct hdff_conv {
    hdff_job *job;
    AVFormatContext *in, *out;
    AVPacket *pkt, *opkt;
    AVFrame *frame, *filtered;
    int vin, ain;
    AVStream *vout, *aout;
    int plan;
    int grid;
    // The input timestamp, in AV_TIME_BASE units, that becomes 0.
    int64_t base;
    // Whether the container stores no times (a bare stream): its pictures are
    // encoded, timed by count from the first, and it is read from its start.
    int untimed;
    // The next untimed picture's presentation time, in its stream's time base.
    int64_t untimed_next;
    int copy_started;
    // The last presentation times of a copied picture, for derive_dts.
    int64_t pts_buffer[MAX_REORDER_DELAY + 1];

    // The picture's encode.
    hdff_decoder dec;
    int decoder_reported;
    int out_w, out_h, scales, caps_rate, bitrate;
    double fps;
    AVRational source_rate;
    AVFilterGraph *vgraph;
    AVFilterContext *vsrc, *vsink;
    AVCodecContext *venc;
    int64_t forced;

    // The sound's encode.
    AVCodecContext *adec, *aenc;
    AVFilterGraph *agraph;
    AVFilterContext *asrc, *asink;

    int header, trailer;
    AVPacket *pending[PENDING_MAX];
    int npending;
    double written, reported;
    int paced;
    int64_t wall_start, first_us;
    int code;
    char reason[160];
} hdff_conv;

/// Records why the run failed (the first reason wins) and answers the code.
int hdff_fail(hdff_conv *c, int code, const char *reason);

/// Writes the playlist's header once every stream is known, then the packets
/// kept until then.
int hdff_write_header(hdff_conv *c);

/// Sends a frame (NULL flushes) to an encoder and muxes what comes out.
int hdff_encode(hdff_conv *c, AVCodecContext *enc, AVStream *st, const AVFrame *frame);

/// Adds a filter after *last and makes it the last.
int hdff_add_filter(AVFilterGraph *graph, AVFilterContext **last, const char *name, const char *args);

/// Plans the picture's encode from the source (size, rate, bitrate) and opens
/// its decoder.
int hdff_picture_prepare(hdff_conv *c);

/// Decodes a picture packet and filters and encodes what comes out.
int hdff_picture_decode(hdff_conv *c, const AVPacket *pkt);

/// Drains the decoder, the filters and the encoder at the end of the input.
int hdff_picture_finish(hdff_conv *c);

#endif
