/*
 * CINELERRA
 * Copyright (C) 2012-2014 Paolo Rampino
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <limits.h>
#include <ctype.h>

// work arounds (centos)
#include <lzma.h>
#ifndef INT64_MAX
#define INT64_MAX 9223372036854775807LL
#endif
#define MAX_RETRY 1000
// max pts/curr_pos drift allowed before correction (in seconds)
#define AUDIO_PTS_TOLERANCE 0.04

#include "asset.h"
#include "bccmodels.h"
#include "bchash.h"
#include "edl.h"
#include "edlsession.h"
#include "file.h"
#include "fileffmpeg.h"
#include "filesystem.h"
#include "ffmpeg.h"
#include "indexfile.h"
#include "interlacemodes.h"
#ifdef HAVE_DV
#include "libdv.h"
#endif
#ifdef HAVE_LIBLCMS2
#include "lcms2.h"
#endif
#include "libmjpeg.h"
#include "mainerror.h"
#include "mwindow.h"
#include "preferences.h"
#include "vframe.h"

#ifdef FFMPEG3
#define url filename
#else
#define av_register_all(s)
#define avfilter_register_all(s)
#endif

#define VIDEO_INBUF_SIZE 0x10000
#define AUDIO_INBUF_SIZE 0x10000
#define VIDEO_REFILL_THRESH 0
#define AUDIO_REFILL_THRESH 0x1000
#define AUDIO_MIN_FRAME_SZ 128

#define FF_ESTM_TIMES 0x0001
#define FF_BAD_TIMES  0x0002

Mutex FFMPEG::fflock("FFMPEG::fflock");

static void ff_err(int ret, const char *fmt, ...)
{
	char msg[BCTEXTLEN];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);
	char errmsg[BCSTRLEN];
	av_strerror(ret, errmsg, sizeof(errmsg));
	fprintf(stderr,_("%s  err: %s\n"),msg, errmsg);
}

void FFPacket::init()
{
	av_init_packet(&pkt);
	pkt.data = 0; pkt.size = 0;
}
void FFPacket::finit()
{
	av_packet_unref(&pkt);
}

FFrame::FFrame(FFStream *fst)
{
	this->fst = fst;
	frm = av_frame_alloc();
	init = fst->init_frame(frm);
}

FFrame::~FFrame()
{
	av_frame_free(&frm);
}

void FFrame::queue(int64_t pos)
{
	position = pos;
	fst->queue(this);
}

void FFrame::dequeue()
{
	fst->dequeue(this);
}

void FFrame::set_hw_frame(AVFrame *frame)
{
	av_frame_free(&frm);
	frm = frame;
}

int FFAudioStream::read(float *fp, long len)
{
	long n = len * nch;
	float *op = outp;
	while( n > 0 ) {
		int k = lmt - op;
		if( k > n ) k = n;
		n -= k;
		while( --k >= 0 ) *fp++ = *op++;
		if( op >= lmt ) op = bfr;
	}
	return len;
}

void FFAudioStream::realloc(long nsz, int nch, long len)
{
	long bsz = nsz * nch;
	float *np = new float[bsz];
	inp = np + read(np, len) * nch;
	outp = np;
	lmt = np + bsz;
	this->nch = nch;
	sz = nsz;
	delete [] bfr;  bfr = np;
}

void FFAudioStream::realloc(long nsz, int nch)
{
	if( nsz > sz || this->nch != nch ) {
		long len = this->nch != nch ? 0 : hpos;
		if( len > sz ) len = sz;
		iseek(len);
		realloc(nsz, nch, len);
	}
}

void FFAudioStream::reserve(long nsz, int nch)
{
	long len = (inp - outp) / nch;
	nsz += len;
	if( nsz > sz || this->nch != nch ) {
		if( this->nch != nch ) len = 0;
		realloc(nsz, nch, len);
		return;
	}
	if( (len*=nch) > 0 && bfr != outp )
		memmove(bfr, outp, len*sizeof(*bfr));
	outp = bfr;
	inp = bfr + len;
}

long FFAudioStream::used()
{
	long len = inp>=outp ? inp-outp : inp-bfr + lmt-outp;
	return len / nch;
}
long FFAudioStream::avail()
{
	float *in1 = inp+1;
	if( in1 >= lmt ) in1 = bfr;
	long len = outp >= in1 ? outp-in1 : outp-bfr + lmt-in1;
	return len / nch;
}
void FFAudioStream::reset_history()
{
	inp = outp = bfr;
	hpos = 0;
	memset(bfr, 0, lmt-bfr);
}

void FFAudioStream::iseek(int64_t ofs)
{
	if( ofs > hpos ) ofs = hpos;
	if( ofs > sz ) ofs = sz;
	outp = inp - ofs*nch;
	if( outp < bfr ) outp += sz*nch;
}

float *FFAudioStream::get_outp(int ofs)
{
	float *ret = outp;
	outp += ofs*nch;
	return ret;
}

int64_t FFAudioStream::put_inp(int ofs)
{
	inp += ofs*nch;
	return (inp-outp) / nch;
}

int FFAudioStream::write(const float *fp, long len)
{
	long n = len * nch;
	float *ip = inp;
	while( n > 0 ) {
		int k = lmt - ip;
		if( k > n ) k = n;
		n -= k;
		while( --k >= 0 ) *ip++ = *fp++;
		if( ip >= lmt ) ip = bfr;
	}
	inp = ip;
	hpos += len;
	return len;
}

int FFAudioStream::zero(long len)
{
	long n = len * nch;
	float *ip = inp;
	while( n > 0 ) {
		int k = lmt - ip;
		if( k > n ) k = n;
		n -= k;
		while( --k >= 0 ) *ip++ = 0;
		if( ip >= lmt ) ip = bfr;
	}
	inp = ip;
	hpos += len;
	return len;
}

// does not advance outp
int FFAudioStream::read(double *dp, long len, int ch)
{
	long n = len;
	float *op = outp + ch;
	float *lmt1 = lmt + nch-1;
	while( n > 0 ) {
		int k = (lmt1 - op) / nch;
		if( k > n ) k = n;
		n -= k;
		while( --k >= 0 ) { *dp++ = *op;  op += nch; }
		if( op >= lmt ) op -= sz*nch;
	}
	return len;
}

// load linear buffer, no wrapping allowed, does not advance inp
int FFAudioStream::write(const double *dp, long len, int ch)
{
	long n = len;
	float *ip = inp + ch;
	while( --n >= 0 ) { *ip = *dp++;  ip += nch; }
	return len;
}


FFStream::FFStream(FFMPEG *ffmpeg, AVStream *st, int fidx)
{
	this->ffmpeg = ffmpeg;
	this->st = st;
	this->fidx = fidx;
	frm_lock = new Mutex("FFStream::frm_lock");
	fmt_ctx = 0;
	avctx = 0;
	filter_graph = 0;
	filt_ctx = 0;
	filt_id = 0;
	buffersrc_ctx = 0;
	buffersink_ctx = 0;
	frm_count = 0;
	nudge = AV_NOPTS_VALUE;
	seek_pos = curr_pos = 0;
	seeking = 0; seeked = 1;
	eof = 0;
	reading = writing = 0;
	hw_pixfmt = AV_PIX_FMT_NONE;
	hw_device_ctx = 0;
	flushed = 0;
	need_packet = 1;
	frame = fframe = 0;
	probe_frame = 0;
	bsfc = 0;
	stats_fp = 0;
	stats_filename = 0;
	stats_in = 0;
	pass = 0;
}

FFStream::~FFStream()
{
	frm_lock->lock("FFStream::~FFStream");
#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(59,16,100)
	if( reading > 0 || writing > 0 ) avcodec_close(avctx);
#endif	
	if( avctx ) avcodec_free_context(&avctx);
	if( fmt_ctx ) avformat_close_input(&fmt_ctx);
	if( hw_device_ctx ) av_buffer_unref(&hw_device_ctx);
	if( bsfc ) av_bsf_free(&bsfc);
	while( frms.first ) frms.remove(frms.first);
	if( filter_graph ) avfilter_graph_free(&filter_graph);
	if( frame ) av_frame_free(&frame);
	if( fframe ) av_frame_free(&fframe);
	if( probe_frame ) av_frame_free(&probe_frame);
	frm_lock->unlock();
	delete frm_lock;
	if( stats_fp ) fclose(stats_fp);
	if( stats_in ) av_freep(&stats_in);
	delete [] stats_filename;
}

void FFStream::ff_lock(const char *cp)
{
	FFMPEG::fflock.lock(cp);
}

void FFStream::ff_unlock()
{
	FFMPEG::fflock.unlock();
}

void FFStream::queue(FFrame *frm)
{
 	frm_lock->lock("FFStream::queue");
	frms.append(frm);
	++frm_count;
	frm_lock->unlock();
	ffmpeg->mux_lock->unlock();
}

void FFStream::dequeue(FFrame *frm)
{
	frm_lock->lock("FFStream::dequeue");
	--frm_count;
	frms.remove_pointer(frm);
	frm_lock->unlock();
}

int FFStream::encode_activate()
{
	if( writing < 0 )
		writing = ffmpeg->encode_activate();
	return writing;
}

// this is a global parameter that really should be in the context
static AVPixelFormat hw_pix_fmt = AV_PIX_FMT_NONE; // protected by ff_lock

// goofy maneuver to attach a hw_format to an av_context
#define GET_HW_PIXFMT(fn, fmt) \
static AVPixelFormat get_hw_##fn(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts) { \
	return fmt; \
}
GET_HW_PIXFMT(vaapi, AV_PIX_FMT_VAAPI)
GET_HW_PIXFMT(vdpau, AV_PIX_FMT_VDPAU)
GET_HW_PIXFMT(cuda,  AV_PIX_FMT_CUDA)
GET_HW_PIXFMT(nv12,  AV_PIX_FMT_NV12)
GET_HW_PIXFMT(vulkan,  AV_PIX_FMT_VULKAN)
GET_HW_PIXFMT(mediacodec,  AV_PIX_FMT_MEDIACODEC)
GET_HW_PIXFMT(qsv,  AV_PIX_FMT_QSV)

static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
			const enum AVPixelFormat *pix_fmts)
{
	for( const enum AVPixelFormat *p=pix_fmts; *p!=AV_PIX_FMT_NONE; ++p ) {
		if( *p != hw_pix_fmt ) continue;
		switch( *p ) {
		case AV_PIX_FMT_VAAPI: ctx->get_format = get_hw_vaapi; return *p;
		case AV_PIX_FMT_VDPAU: ctx->get_format = get_hw_vdpau; return *p;
		case AV_PIX_FMT_CUDA:  ctx->get_format = get_hw_cuda;  return *p;
		case AV_PIX_FMT_NV12:  ctx->get_format = get_hw_nv12;  return *p;
		case AV_PIX_FMT_VULKAN:  ctx->get_format = get_hw_vulkan;  return *p;
		case AV_PIX_FMT_MEDIACODEC:  ctx->get_format = get_hw_mediacodec;  return *p;
		case AV_PIX_FMT_QSV:  ctx->get_format = get_hw_qsv;  return *p;
		default:
			fprintf(stderr, "Unknown HW surface format: %s\n",
				av_get_pix_fmt_name(*p));
			continue;
		}
	}
	fprintf(stderr, "Failed to get HW surface format.\n");
	return hw_pix_fmt = AV_PIX_FMT_NONE;
}


AVHWDeviceType FFStream::decode_hw_activate()
{
	return AV_HWDEVICE_TYPE_NONE;
}
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
int FFStream::decode_hw_format(const AVCodec *decoder, AVHWDeviceType type)
#else
int FFStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
#endif
{
	return 0;
}

int FFStream::decode_activate()
{
	if( reading < 0 && (reading=ffmpeg->decode_activate()) > 0 ) {
		ff_lock("FFStream::decode_activate");
		reading = 0;
		AVDictionary *copts = 0;
		av_dict_copy(&copts, ffmpeg->opts, 0);
		int ret = 0;
		AVHWDeviceType hw_type = decode_hw_activate();

		// this should be avformat_copy_context(), but no copy avail
		ret = avformat_open_input(&fmt_ctx,
			ffmpeg->fmt_ctx->url, ffmpeg->fmt_ctx->iformat, &copts);
		if( ret >= 0 ) {
			ret = avformat_find_stream_info(fmt_ctx, 0);
			st = fmt_ctx->streams[fidx];
			load_markers();
		}
		while( ret >= 0 && st != 0 && !reading ) {
			AVCodecID codec_id = st->codecpar->codec_id;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
			const AVCodec *decoder = 0;
#else
			AVCodec *decoder = 0;
#endif
			if( is_video() ) {
				if( ffmpeg->opt_video_decoder )
					decoder = avcodec_find_decoder_by_name(ffmpeg->opt_video_decoder);
				else
					ffmpeg->video_codec_remaps.update(codec_id, decoder);
			}
			else if( is_audio() ) {
				if( ffmpeg->opt_audio_decoder )
					decoder = avcodec_find_decoder_by_name(ffmpeg->opt_audio_decoder);
				else
					ffmpeg->audio_codec_remaps.update(codec_id, decoder);
			}
			if( !decoder )
				decoder = avcodec_find_decoder(codec_id);
			avctx = avcodec_alloc_context3(decoder);
			if( !avctx ) {
				eprintf(_("cant allocate codec context\n"));
				ret = AVERROR(ENOMEM);
			}
			if( ret >= 0 && hw_type != AV_HWDEVICE_TYPE_NONE ) {
				ret = decode_hw_format(decoder, hw_type);
			}
			if( ret >= 0 ) {
				avcodec_parameters_to_context(avctx, st->codecpar);
				if( !av_dict_get(copts, "threads", NULL, 0) )
					avctx->thread_count = ffmpeg->ff_cpus();
				ret = avcodec_open2(avctx, decoder, &copts);
			}
			AVFrame *hw_frame = 0;
			if( ret >= 0 && hw_type != AV_HWDEVICE_TYPE_NONE ) {
				if( !(hw_frame=av_frame_alloc()) ) {
					fprintf(stderr, "FFStream::decode_activate: av_frame_alloc failed\n");
					ret = AVERROR(ENOMEM);
				}
				if( ret >= 0 )
					ret = decode(hw_frame);
			}
			if( ret < 0 && hw_type != AV_HWDEVICE_TYPE_NONE ) {
				ff_err(ret, "HW device init failed, using SW decode.\nfile:%s\n",
					ffmpeg->fmt_ctx->url);
#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(59,16,100)
				avcodec_close(avctx);
#endif
				avcodec_free_context(&avctx);
				av_buffer_unref(&hw_device_ctx);
				hw_device_ctx = 0;
				av_frame_free(&hw_frame);
				hw_type = AV_HWDEVICE_TYPE_NONE;
				int flags = AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY;
				int idx = st->index;
				av_seek_frame(fmt_ctx, idx, 0, flags);
				need_packet = 1;  flushed = 0;
				seeked = 1;  st_eof(0);
				ret = 0;
				continue;
			}
			probe_frame = hw_frame;
			if( ret >= 0 )
				reading = 1;
			else
				eprintf(_("open decoder failed\n"));
		}
		if( ret < 0 )
			eprintf(_("can't open input file: %s\n"), ffmpeg->fmt_ctx->url);
		av_dict_free(&copts);
		ff_unlock();
	}
	return reading;
}

int FFStream::read_packet()
{
	av_packet_unref(ipkt);
	int ret = av_read_frame(fmt_ctx, ipkt);
	if( ret < 0 ) {
		st_eof(1);
		if( ret == AVERROR_EOF ) return 0;
		ff_err(ret, "FFStream::read_packet: av_read_frame failed\n");
		flushed = 1;
		return -1;
	}
	return 1;
}

int FFStream::decode(AVFrame *frame)
{
	if( probe_frame ) { // hw probe reads first frame
		av_frame_ref(frame, probe_frame);
		av_frame_free(&probe_frame);
		return 1;
	}
	int ret = 0;
	int retries = MAX_RETRY;
	frm_lock->lock("FFStream::decode");
	while( ret >= 0 && !flushed && --retries >= 0 ) {
		if( need_packet ) {
			if( (ret=read_packet()) < 0 ) break;
			AVPacket *pkt = ret > 0 ? (AVPacket*)ipkt : 0;
			if( pkt ) {
				if( pkt->stream_index != st->index ) continue;
				if( !pkt->data || !pkt->size ) continue;
			}
			if( (ret=avcodec_send_packet(avctx, pkt)) < 0 ) {
				ff_err(ret, "FFStream::decode: avcodec_send_packet failed.\nfile:%s\n",
						ffmpeg->fmt_ctx->url);
				break;
			}
			need_packet = 0;
			retries = MAX_RETRY;
		}
		if( (ret=decode_frame(frame)) > 0 ) break;
		if( !ret ) {
			need_packet = 1;
			flushed = st_eof();
		}
	}
	frm_lock->unlock();

	if( retries < 0 ) {
		fprintf(stderr, "FFStream::decode: Retry limit\n");
		ret = 0;
	}
	if( ret < 0 )
		fprintf(stderr, "FFStream::decode: failed\n");
	return ret;
}

int FFStream::load_filter(AVFrame *frame)
{
	int ret = av_buffersrc_add_frame_flags(buffersrc_ctx, frame, 0);
	if( ret < 0 )
		eprintf(_("av_buffersrc_add_frame_flags failed\n"));
	return ret;
}

int FFStream::read_filter(AVFrame *frame)
{
	int ret = av_buffersink_get_frame(buffersink_ctx, frame);
	if( ret < 0 ) {
		if( ret == AVERROR(EAGAIN) ) return 0;
		if( ret == AVERROR_EOF ) { st_eof(1); return -1; }
		ff_err(ret, "FFStream::read_filter: av_buffersink_get_frame failed\n");
		return ret;
	}
	return 1;
}

int FFStream::read_frame(AVFrame *frame)
{
	av_frame_unref(frame);
	if( !filter_graph || !buffersrc_ctx || !buffersink_ctx )
		return decode(frame);
	if( !fframe && !(fframe=av_frame_alloc()) ) {
		fprintf(stderr, "FFStream::read_frame: av_frame_alloc failed\n");
		return -1;
	}
	int ret = -1;
	while( !flushed && !(ret=read_filter(frame)) ) {
		if( (ret=decode(fframe)) < 0 ) break;
		if( ret > 0 && (ret=load_filter(fframe)) < 0 ) break;
	}
	return ret;
}

int FFStream::write_packet(FFPacket &pkt)
{
	int ret = 0;
	if( !bsfc ) {
		av_packet_rescale_ts(pkt, avctx->time_base, st->time_base);
		pkt->stream_index = st->index;
		ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, pkt);
	}
	else {
	bsfc->time_base_in = st->time_base;
	avcodec_parameters_copy(bsfc->par_in, st->codecpar);
	av_bsf_init(bsfc);
	
		ret = av_bsf_send_packet(bsfc, pkt);
		while( ret >= 0 ) {
			FFPacket bs;
			if( (ret=av_bsf_receive_packet(bsfc, bs)) < 0 ) {
				if( ret == AVERROR(EAGAIN) ) return 0;
				if( ret == AVERROR_EOF ) return -1;
				break;
			}
			//printf(" filter name %s \n", bsfc->filter[0].name);
			//avcodec_parameters_copy(ffmpeg->fmt_ctx->streams[0]->codecpar, bsfc->par_out);
			//avcodec_parameters_copy(st->codecpar, bsfc->par_out);
			av_packet_rescale_ts(bs, avctx->time_base, st->time_base);
			bs->stream_index = st->index;
			ret = av_interleaved_write_frame(ffmpeg->fmt_ctx, bs);
		}
	}
	if( ret < 0 )
		ff_err(ret, "FFStream::write_packet: write packet failed.\nfile:%s\n",
				ffmpeg->fmt_ctx->url);
	return ret;
}

int FFStream::encode_frame(AVFrame *frame)
{
	int pkts = 0, ret = 0;
	for( int retry=MAX_RETRY; --retry>=0; ) {
		if( frame || !pkts )
			ret = avcodec_send_frame(avctx, frame);
		if( !ret && frame ) return pkts;
		if( ret < 0 && ret != AVERROR(EAGAIN) ) break;
		if ( ret == AVERROR(EAGAIN) && !frame ) continue;
		FFPacket opkt;
		ret = avcodec_receive_packet(avctx, opkt);
		if( !frame && (ret == AVERROR_EOF || ret == AVERROR(EAGAIN) )) return pkts;
		//if( ret < 0 ) break;
		ret = write_packet(opkt);
		if( ret < 0 ) break;
		++pkts;
		if( frame && stats_fp ) {
			ret = write_stats_file();
			if( ret < 0 ) break;
		}
	}
	ff_err(ret, "FFStream::encode_frame: encode failed.\nfile: %s\n",
				ffmpeg->fmt_ctx->url);
	return -1;
}

int FFStream::flush()
{
	if( writing < 0 )
		return -1;
	int ret = encode_frame(0);
	if( ret >= 0 && stats_fp ) {
		ret = write_stats_file();
		close_stats_file();
	}
	if( ret < 0 )
		ff_err(ret, "FFStream::flush failed\n:file:%s\n",
				ffmpeg->fmt_ctx->url);
	return ret >= 0 ? 0 : 1;
}


int FFStream::open_stats_file()
{
	stats_fp = fopen(stats_filename,"w");
	return stats_fp ? 0 : AVERROR(errno);
}

int FFStream::close_stats_file()
{
	if( stats_fp ) {
		fclose(stats_fp);  stats_fp = 0;
	}
	return 0;
}

int FFStream::read_stats_file()
{
	int64_t len = 0;  struct stat stats_st;
	int fd = open(stats_filename, O_RDONLY);
	int ret = fd >= 0 ? 0: ENOENT;
	if( !ret && fstat(fd, &stats_st) )
		ret = EINVAL;
	if( !ret ) {
		len = stats_st.st_size;
		stats_in = (char *)av_malloc(len+1);
		if( !stats_in )
			ret = ENOMEM;
	}
	if( !ret && read(fd, stats_in, len+1) != len )
		ret = EIO;
	if( !ret ) {
		stats_in[len] = 0;
		avctx->stats_in = stats_in;
	}
	if( fd >= 0 )
		close(fd);
	return !ret ? 0 : AVERROR(ret);
}

int FFStream::write_stats_file()
{
	int ret = 0;
	if( avctx->stats_out && (ret=strlen(avctx->stats_out)) > 0 ) {
		int len = fwrite(avctx->stats_out, 1, ret, stats_fp);
		if( ret != len )
			ff_err(ret = AVERROR(errno), "FFStream::write_stats_file.\n%file:%s\n",
				ffmpeg->fmt_ctx->url);
	}
	return ret;
}

int FFStream::init_stats_file()
{
	int ret = 0;
	if( (pass & 2) && (ret = read_stats_file()) < 0 )
		ff_err(ret, "stat file read: %s", stats_filename);
	if( (pass & 1) && (ret=open_stats_file()) < 0 )
		ff_err(ret, "stat file open: %s", stats_filename);
	return ret >= 0 ? 0 : ret;
}

int FFStream::seek(int64_t no, double rate)
{
// default ffmpeg native seek
	int npkts = 1;
	int64_t pos = no, pkt_pos = -1;
	IndexMarks *index_markers = get_markers();
	if( index_markers && index_markers->size() > 1 ) {
		IndexMarks &marks = *index_markers;
		int i = marks.find(pos);
		int64_t n = i < 0 ? (i=0) : marks[i].no;
// if indexed seek point not too far away (<30 secs), use index
		if( no-n < 30*rate ) {
			if( n < 0 ) n = 0;
			pos = n;
			if( i < marks.size() ) pkt_pos = marks[i].pos;
			npkts = MAX_RETRY;
		}
	}
	if( pos == curr_pos ) return 0;
	seeking = -1;
	double secs = pos < 0 ? 0. : pos / rate;
	AVRational time_base = st->time_base;
	int64_t tstmp = time_base.num > 0 ? secs * time_base.den/time_base.num : 0;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
	int nb_index_entries = avformat_index_get_entries_count(st);
#endif
	if( !tstmp ) {
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
		if( nb_index_entries > 0 ) tstmp = (avformat_index_get_entry(st, 0))->timestamp;
#else
		if( st->nb_index_entries > 0 ) tstmp = st->index_entries[0].timestamp;
#endif
		else if( st->start_time != AV_NOPTS_VALUE ) tstmp = st->start_time;
#if LIBAVCODEC_VERSION_INT  <= AV_VERSION_INT(58,134,100)
		else if( st->first_dts != AV_NOPTS_VALUE ) tstmp = st->first_dts;
#endif
		else tstmp = INT64_MIN+1;
	}
	else if( nudge != AV_NOPTS_VALUE ) tstmp += nudge;
	int idx = st->index;
#if 0
// seek all streams using the default timebase.
//   this is how ffmpeg and ffplay work.  stream seeks are less tested.
	tstmp = av_rescale_q(tstmp, time_base, AV_TIME_BASE_Q);
	idx = -1;
#endif
	frm_lock->lock("FFStream::seek");
	av_frame_free(&probe_frame);
	avcodec_flush_buffers(avctx);
	avformat_flush(fmt_ctx);
#if 0
	int64_t seek = tstmp;
	int flags = AVSEEK_FLAG_ANY;
	if( !(fmt_ctx->iformat->flags & AVFMT_NO_BYTE_SEEK) && pkt_pos >= 0 ) {
		seek = pkt_pos;
		flags = AVSEEK_FLAG_BYTE;
	}
	int ret = avformat_seek_file(fmt_ctx, st->index, -INT64_MAX, seek, INT64_MAX, flags);
#else
// finds the first index frame below the target time
	int flags = AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY;
	int ret = av_seek_frame(fmt_ctx, idx, tstmp, flags);
#endif
	int retry = MAX_RETRY;
	while( ret >= 0 ) {
		need_packet = 0;  flushed = 0;
		seeked = 1;  st_eof(0);
// read up to retry packets, limited to npkts in stream, and not pkt.pos past pkt_pos
		while( --retry >= 0 ) {
			if( read_packet() <= 0 ) { ret = -1;  break; }
			if( ipkt->stream_index != st->index ) continue;
			if( !ipkt->data || !ipkt->size ) continue;
			if( pkt_pos >= 0 && ipkt->pos >= pkt_pos ) break;
			if( --npkts <= 0 ) break;
			int64_t pkt_ts = ipkt->dts != AV_NOPTS_VALUE ? ipkt->dts : ipkt->pts;
			if( pkt_ts == AV_NOPTS_VALUE ) continue;
			if( pkt_ts >= tstmp ) break;
		}
		if( retry < 0 ) {
			ff_err(AVERROR(EIO), "FFStream::seek: %s\n"
				" retry limit, pos=%jd tstmp=%jd, ",
				ffmpeg->fmt_ctx->url, pos, tstmp);
			ret = -1;
		}
		if( ret < 0 ) break;
		ret = avcodec_send_packet(avctx, ipkt);
		if( !ret ) break;
//some codecs need more than one pkt to resync
		if( ret == AVERROR_INVALIDDATA ) ret = 0;
		if( ret < 0 ) {
			ff_err(ret, "FFStream::avcodec_send_packet failed.\nseek:%s\n",
				ffmpeg->fmt_ctx->url);
			break;
		}
	}
	frm_lock->unlock();
	if( ret < 0 ) {
printf("** seek fail %jd, %jd\n", pos, tstmp);
		seeked = need_packet = 0;
		st_eof(flushed=1);
		return -1;
	}
//printf("seeked pos = %ld, %ld\n", pos, tstmp);
	seek_pos = curr_pos = pos;
	return 0;
}

FFAudioStream::FFAudioStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx)
 : FFStream(ffmpeg, strm, fidx)
{
	this->idx = idx;
	channel0 = channels = 0;
	sample_rate = 0;
	mbsz = 0;
	frame_sz = AUDIO_MIN_FRAME_SZ;
	length = 0;
	resample_context = 0;
	swr_ichs = swr_ifmt = swr_irate = 0;

	aud_bfr_sz = 0;
	aud_bfr = 0;

// history buffer
	nch = 2;
	sz = 0x10000;
	long bsz = sz * nch;
	bfr = new float[bsz];
	lmt = bfr + bsz;
	reset_history();
}

FFAudioStream::~FFAudioStream()
{
	if( resample_context ) swr_free(&resample_context);
	delete [] aud_bfr;
	delete [] bfr;
}

void FFAudioStream::init_swr(int ichs, int ifmt, int irate)
{
	if( resample_context ) {
		if( swr_ichs == ichs && swr_ifmt == ifmt && swr_irate == irate )
			return;
		swr_free(&resample_context);
	}
	swr_ichs = ichs;  swr_ifmt = ifmt;  swr_irate = irate;
	if( ichs == channels && ifmt == AV_SAMPLE_FMT_FLT && irate == sample_rate )
		return;
	//uint64_t ilayout = av_get_default_channel_layout(ichs);
	AVChannelLayout ilayout, olayout;
	av_channel_layout_default(&ilayout, ichs);
	//if( !ilayout ) ilayout = ((uint64_t)1<<ichs) - 1;
	//uint64_t olayout = av_get_default_channel_layout(channels);
	av_channel_layout_default(&olayout, channels);
	//if( !olayout ) olayout = ((uint64_t)1<<channels) - 1;
	
	swr_alloc_set_opts2(&resample_context,
		&olayout, AV_SAMPLE_FMT_FLT, sample_rate,
		&ilayout, (AVSampleFormat)ifmt, irate,
		0, NULL);
	if( resample_context )
		swr_init(resample_context);
}

int FFAudioStream::get_samples(float *&samples, uint8_t **data, int len)
{
	samples = *(float **)data;
	if( resample_context ) {
		if( len > aud_bfr_sz ) {
			delete [] aud_bfr;
			aud_bfr = 0;
		}
		if( !aud_bfr ) {
			aud_bfr_sz = len;
			aud_bfr = new float[aud_bfr_sz*channels];
		}
		int ret = swr_convert(resample_context,
			(uint8_t**)&aud_bfr, aud_bfr_sz, (const uint8_t**)data, len);
		if( ret < 0 ) {
			ff_err(ret, "FFAudioStream::get_samples: swr_convert failed\n");
			return -1;
		}
		samples = aud_bfr;
		len = ret;
	}
	return len;
}

int FFAudioStream::load_history(uint8_t **data, int len)
{
	float *samples;
	len = get_samples(samples, data, len);
	if( len > 0 ) {
		// biggest user bfr since seek + frame
		realloc(mbsz + len + 1, channels);
		write(samples, len);
	}
	return len;
}

int FFAudioStream::decode_frame(AVFrame *frame)
{
	int first_frame = seeked;  seeked = 0;
	frame->best_effort_timestamp = AV_NOPTS_VALUE;
	int ret = avcodec_receive_frame(avctx, frame);
	if( ret < 0 ) {
		if( first_frame ) return 0;
		if( ret == AVERROR(EAGAIN) ) return 0;
		if( ret == AVERROR_EOF ) { st_eof(1); return 0; }
		ff_err(ret, "FFAudioStream::decode_frame: Could not read audio frame.\nfile:%s\n",
				ffmpeg->fmt_ctx->url);
		return -1;
	}
	int64_t pkt_ts = frame->best_effort_timestamp;
	if( pkt_ts != AV_NOPTS_VALUE ) {
		double ts = ffmpeg->to_secs(pkt_ts - nudge, st->time_base);
		double t = (double)curr_pos / sample_rate;
// some time_base clocks are very grainy, too grainy for audio (clicks, pops)
		if( fabs(ts - t) > AUDIO_PTS_TOLERANCE )
			curr_pos = ts * sample_rate + 0.5;
	}
	return 1;
}

int FFAudioStream::encode_activate()
{
	if( writing >= 0 ) return writing;
	if( !avctx->codec ) return writing = 0;
	frame_sz = avctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE ?
		10000 : avctx->frame_size;
	return FFStream::encode_activate();
}

int64_t FFAudioStream::load_buffer(double ** const sp, int len)
{
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61,3,100)
	reserve(len+1, st->codecpar->ch_layout.nb_channels);
#else
	reserve(len+1, st->codecpar->channels);
#endif
	for( int ch=0; ch<nch; ++ch )
		write(sp[ch], len, ch);
	return put_inp(len);
}

int FFAudioStream::in_history(int64_t pos)
{
	if( pos > curr_pos ) return 0;
	int64_t len = hpos;
	if( len > sz ) len = sz;
	if( pos < curr_pos - len ) return 0;
	return 1;
}


int FFAudioStream::init_frame(AVFrame *frame)
{
	frame->nb_samples = frame_sz;
	frame->format = avctx->sample_fmt;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(61,3,100)
	frame->ch_layout.u.mask = avctx->ch_layout.u.mask;
	av_channel_layout_copy(&frame->ch_layout, &avctx->ch_layout);
#else
	frame->channel_layout = avctx->channel_layout;
#endif
	frame->sample_rate = avctx->sample_rate;
	int ret = av_frame_get_buffer(frame, 0);
	if (ret < 0)
		ff_err(ret, "FFAudioStream::init_frame: av_frame_get_buffer failed\n");
	return ret;
}

int FFAudioStream::load(int64_t pos, int len)
{
	if( audio_seek(pos) < 0 ) return -1;
	if( !frame && !(frame=av_frame_alloc()) ) {
		fprintf(stderr, "FFAudioStream::load: av_frame_alloc failed\n");
		return -1;
	}
	if( mbsz < len ) mbsz = len;
	int64_t end_pos = pos + len;
	int ret = 0, i = len / frame_sz + MAX_RETRY;
	while( ret>=0 && !flushed && curr_pos<end_pos && --i>=0 ) {
		ret = read_frame(frame);
		if( ret > 0 && frame->nb_samples > 0 ) {
			init_swr(frame->ch_layout.nb_channels, frame->format, frame->sample_rate);
			load_history(&frame->extended_data[0], frame->nb_samples);
			curr_pos += frame->nb_samples;
		}
	}
	if( end_pos > curr_pos ) {
		zero(end_pos - curr_pos);
		curr_pos = end_pos;
	}
	len = curr_pos - pos;
	iseek(len);
	return len;
}

int FFAudioStream::audio_seek(int64_t pos)
{
	if( decode_activate() <= 0 ) return -1;
	if( !st->codecpar ) return -1;
	if( in_history(pos) ) return 0;
	if( pos == curr_pos ) return 0;
	reset_history();  mbsz = 0;
// guarantee preload > 1sec samples
	if( (pos-=sample_rate) < 0 ) pos = 0;
	if( seek(pos, sample_rate) < 0 ) return -1;
	return 1;
}

int FFAudioStream::encode(double **samples, int len)
{
	if( encode_activate() <= 0 ) return -1;
	ffmpeg->flow_ctl();
	int ret = 0;
	int64_t count = samples ? load_buffer(samples, len) : used();
	int frame_sz1 = samples ? frame_sz-1 : 0;
	FFrame *frm = 0;

	while( ret >= 0 && count > frame_sz1 ) {
		frm = new FFrame(this);
		if( (ret=frm->initted()) < 0 ) break;
		AVFrame *frame = *frm;
		len = count >= frame_sz ? frame_sz : count;
		float *bfrp = get_outp(len);
		ret =  swr_convert(resample_context,
			(uint8_t **)frame->extended_data, len,
			(const uint8_t **)&bfrp, len);
		if( ret < 0 ) {
			ff_err(ret, "FFAudioStream::encode: swr_convert failed\n");
			break;
		}
		frame->nb_samples = len;
		frm->queue(curr_pos);
		frm = 0;
		curr_pos += len;
		count -= len;
	}

	delete frm;
	return ret >= 0 ? 0 : 1;
}

int FFAudioStream::drain()
{
	return encode(0,0);
}

int FFAudioStream::encode_frame(AVFrame *frame)
{
	return FFStream::encode_frame(frame);
}

int FFAudioStream::write_packet(FFPacket &pkt)
{
	return FFStream::write_packet(pkt);
}

void FFAudioStream::load_markers()
{
	IndexState *index_state = ffmpeg->file_base->asset->index_state;
	if( !index_state || idx >= index_state->audio_markers.size() ) return;
	if( index_state->marker_status == MARKERS_NOTTESTED ) return;
	FFStream::load_markers(*index_state->audio_markers[idx], sample_rate);
}

IndexMarks *FFAudioStream::get_markers()
{
	IndexState *index_state = ffmpeg->file_base->asset->index_state;
	if( !index_state || idx >= index_state->audio_markers.size() ) return 0;
	return index_state->audio_markers[idx];
}

FFVideoStream::FFVideoStream(FFMPEG *ffmpeg, AVStream *strm, int idx, int fidx)
 : FFStream(ffmpeg, strm, fidx),
   FFVideoConvert(ffmpeg->ff_prefs())
{
	this->idx = idx;
	width = height = 0;
	transpose = 0;
	frame_rate = 0;
	aspect_ratio = 0;
	length = 0;
	interlaced = 0;
	top_field_first = 0;
	color_space = -1;
	color_range = -1;
	fconvert_ctx = 0;
}

FFVideoStream::~FFVideoStream()
{
	if( fconvert_ctx ) sws_freeContext(fconvert_ctx);
}

AVHWDeviceType FFVideoStream::decode_hw_activate()
{
	AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
	const char *hw_dev = ffmpeg->opt_hw_dev;
	if( !hw_dev ) hw_dev = getenv("CIN_HW_DEV");
	if( !hw_dev ) hw_dev = ffmpeg->ff_hw_dev();
	if( hw_dev && *hw_dev &&
	    strcmp("none", hw_dev) && strcmp(_("none"), hw_dev) ) {
		type = av_hwdevice_find_type_by_name(hw_dev);
		if( type == AV_HWDEVICE_TYPE_NONE ) {
			fprintf(stderr, "Device type %s is not supported.\n", hw_dev);
			fprintf(stderr, "Available device types:");
			while( (type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE )
				fprintf(stderr, " %s", av_hwdevice_get_type_name(type));
			fprintf(stderr, "\n");
		}
	}
	return type;
}
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
int FFVideoStream::decode_hw_format(const AVCodec *decoder, AVHWDeviceType type)
#else
int FFVideoStream::decode_hw_format(AVCodec *decoder, AVHWDeviceType type)
#endif
{
	int ret = 0;
	hw_pix_fmt = AV_PIX_FMT_NONE;
	for( int i=0; ; ++i ) {
		const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
		if( !config ) {
			fprintf(stderr, "Decoder %s does not support device type %s.\n",
				decoder->name, av_hwdevice_get_type_name(type));
			ret = -1;
			break;
		}
		if( (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) != 0 &&
		    config->device_type == type ) {
			hw_pix_fmt = config->pix_fmt;
			break;
		}
	}
	if( hw_pix_fmt >= 0 ) {
		hw_pixfmt = hw_pix_fmt;
		avctx->get_format  = get_hw_format;
		const char *drm_node = getenv("CIN_DRM_DEC");
		if(drm_node && type==AV_HWDEVICE_TYPE_VAAPI) {
		    ret = av_hwdevice_ctx_create(&hw_device_ctx, type, drm_node, 0, 0);
		}
		else {
		ret = av_hwdevice_ctx_create(&hw_device_ctx, type, 0, 0, 0);
		}

		if( ret >= 0 ) {
			avctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
			ret = 1;
		}
		else {
			ff_err(ret, "Failed HW device create.\ndev:%s\n",
				av_hwdevice_get_type_name(type));
			ret = -1;
		}
	}
	return ret;
}

AVHWDeviceType FFVideoStream::encode_hw_activate(const char *hw_dev, const char *hw_sformat)
{
	const char *drm_node_enc = getenv("CIN_DRM_ENC");
	AVBufferRef *hw_device_ctx = 0;
	AVBufferRef *hw_frames_ref = 0;
	AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
	if( strcmp(_("none"), hw_dev) ) {
		type = av_hwdevice_find_type_by_name(hw_dev);
		if( !((type == AV_HWDEVICE_TYPE_VAAPI) || (type == AV_HWDEVICE_TYPE_VULKAN ))) {
			fprintf(stderr, "currently, only vaapi/vulkan hw encode is supported\n");
			type = AV_HWDEVICE_TYPE_NONE;
		}
	}

	if( type != AV_HWDEVICE_TYPE_NONE ) {
		int ret = 0;
		if (drm_node_enc) {
		ret = av_hwdevice_ctx_create(&hw_device_ctx, AV_HWDEVICE_TYPE_VAAPI, drm_node_enc, 0, 0);
		} else {
		ret = av_hwdevice_ctx_create(&hw_device_ctx, type, 0, 0, 0);
		}
		
		if( ret < 0 ) {
			ff_err(ret, "Failed to create a HW device.\n");
			type = AV_HWDEVICE_TYPE_NONE;
		}
	}
	if( type != AV_HWDEVICE_TYPE_NONE ) {
		hw_frames_ref = av_hwframe_ctx_alloc(hw_device_ctx);
		if( !hw_frames_ref ) {
			fprintf(stderr, "Failed to create HW frame context.\n");
			type = AV_HWDEVICE_TYPE_NONE;
		}
	}
	if( type != AV_HWDEVICE_TYPE_NONE ) {
		AVHWFramesContext *frames_ctx = (AVHWFramesContext *)(hw_frames_ref->data);
		frames_ctx->format = AV_PIX_FMT_VAAPI;

		if (type == AV_HWDEVICE_TYPE_VULKAN)
		frames_ctx->format = AV_PIX_FMT_VULKAN;

		frames_ctx->sw_format = AV_PIX_FMT_NV12;
		if (strcmp(hw_sformat, "vaapi")) frames_ctx->sw_format = av_get_pix_fmt(hw_sformat);
		if (strcmp(hw_sformat, "vulkan")) frames_ctx->sw_format = av_get_pix_fmt(hw_sformat);
		
		frames_ctx->width = width;
		frames_ctx->height = height;
		frames_ctx->initial_pool_size = 0; // 200;
		int ret = av_hwframe_ctx_init(hw_frames_ref);
		if( ret >= 0 ) {
			avctx->hw_frames_ctx = av_buffer_ref(hw_frames_ref);
			if( !avctx->hw_frames_ctx ) ret = AVERROR(ENOMEM);
		}
		if( ret < 0 ) {
			ff_err(ret, "Failed to initialize HW frame context.\n");
			type = AV_HWDEVICE_TYPE_NONE;
		}
		av_buffer_unref(&hw_frames_ref);
	}
	return type;
}

int FFVideoStream::encode_hw_write(FFrame *picture)
{
	int ret = 0;
	AVFrame *hw_frm = 0;
	switch( avctx->pix_fmt ) {
	case AV_PIX_FMT_VAAPI:
	case AV_PIX_FMT_VULKAN:
		hw_frm = av_frame_alloc();
		if( !hw_frm ) { ret = AVERROR(ENOMEM);  break; }
		ret = av_hwframe_get_buffer(avctx->hw_frames_ctx, hw_frm, 0);
		if( ret < 0 ) break;
		ret = av_hwframe_transfer_data(hw_frm, *picture, 0);
		if( ret < 0 ) break;
		picture->set_hw_frame(hw_frm);
		return 0;
	default:
		return 0;
	}
	av_frame_free(&hw_frm);
	ff_err(ret, "Error while transferring frame data to GPU.\n");
	return ret;
}

int FFVideoStream::decode_frame(AVFrame *frame)
{
	int first_frame = seeked;  seeked = 0;
	int ret = avcodec_receive_frame(avctx, frame);
	if( ret < 0 ) {
		if( first_frame ) return 0;
		if( ret == AVERROR(EAGAIN) ) return 0;
		if( ret == AVERROR_EOF ) { st_eof(1); return 0; }
		ff_err(ret, "FFVideoStream::decode_frame: Could not read video frame.\nfile:%s\n,",
				ffmpeg->fmt_ctx->url);
		return -1;
	}
	int64_t pkt_ts = frame->best_effort_timestamp;
	if( pkt_ts != AV_NOPTS_VALUE )
		curr_pos = ffmpeg->to_secs(pkt_ts - nudge, st->time_base) * frame_rate + 0.5;
	return 1;
}

int FFVideoStream::probe(int64_t pos)
{
	int ret = video_seek(pos);
	if( ret < 0 ) return -1;
	if( !frame && !(frame=av_frame_alloc()) ) {
		fprintf(stderr, "FFVideoStream::probe: av_frame_alloc failed\n");
		return -1;
	}
		
	if (ffmpeg->interlace_from_codec) return 1;

		ret = read_frame(frame);
		if( ret > 0 ) {
			//printf("codec interlace: %i \n",frame->interlaced_frame);
			//printf("codec tff: %i \n",frame->top_field_first);
#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(59,16,100)
			if (!frame->interlaced_frame)
				ffmpeg->interlace_from_codec = AV_FIELD_PROGRESSIVE;
			if ((frame->interlaced_frame) && (frame->top_field_first))
				ffmpeg->interlace_from_codec = AV_FIELD_TT;
			if ((frame->interlaced_frame) && (!frame->top_field_first))
				ffmpeg->interlace_from_codec = AV_FIELD_BB;
			//printf("Interlace mode from codec: %i\n", ffmpeg->interlace_from_codec);
#else
			if (!frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)
				ffmpeg->interlace_from_codec = AV_FIELD_PROGRESSIVE;
			if ((frame->flags & AV_FRAME_FLAG_INTERLACED) && (frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST))
				ffmpeg->interlace_from_codec = AV_FIELD_TT;
			if ((frame->flags & AV_FRAME_FLAG_INTERLACED ) && (!frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST))
				ffmpeg->interlace_from_codec = AV_FIELD_BB;
#endif
	}

	if( frame->format == AV_PIX_FMT_NONE || frame->width <= 0 || frame->height <= 0 )
		ret = -1;

	ret = ret > 0 ? 1 : ret < 0 ? -1 : 0;
	av_frame_free(&frame);
	return ret;
}

int FFVideoStream::load(VFrame *vframe, int64_t pos)
{
	int ret = video_seek(pos);
	if( ret < 0 ) return -1;
	if( !frame && !(frame=av_frame_alloc()) ) {
		fprintf(stderr, "FFVideoStream::load: av_frame_alloc failed\n");
		return -1;
	}
	

	int i = MAX_RETRY + pos - curr_pos;
	int64_t cache_start = 0;
	while( ret>=0 && !flushed && curr_pos<=pos && --i>=0 ) {
		ret = read_frame(frame);
		if( ret > 0 ) {
#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(59,16,100)
			if( frame->key_frame && seeking < 0 ) {
#else
			if( (frame->flags & AV_FRAME_FLAG_KEY) && seeking < 0 ) {
#endif
				int use_cache = ffmpeg->get_use_cache();
				if( use_cache < 0 ) {
// for reverse read, reload file frame_cache from keyframe to pos
					ffmpeg->purge_cache();
					int count = preferences->cache_size /
						vframe->get_data_size() / 2;  // try to burn only 1/2 of cache
					cache_start = pos - count + 1;
					seeking = 1;
				}
				else
					seeking = 0;
			}
			if( seeking > 0 && curr_pos >= cache_start && curr_pos < pos ) {
				int vw =vframe->get_w(), vh = vframe->get_h();
				int vcolor_model = vframe->get_color_model();
// do not use shm here, puts too much pressure on 32bit systems
				VFrame *cache_frame = new VFrame(vw, vh, vcolor_model, 0);
				ret = convert_cmodel(cache_frame, frame);
				if( ret > 0 )
					ffmpeg->put_cache_frame(cache_frame, curr_pos);
			}
			++curr_pos;
		}
	}
	seeking = 0;
	if( frame->format == AV_PIX_FMT_NONE || frame->width <= 0 || frame->height <= 0 )
		ret = -1;
	if( ret >= 0 ) {
		ret = convert_cmodel(vframe, frame);
	}
	ret = ret > 0 ? 1 : ret < 0 ? -1 : 0;
	return ret;
}

int FFVideoStream::video_seek(int64_t pos)
{
	if( decode_activate() <= 0 ) return -1;
	if( !st->codecpar ) return -1;
	if( pos == curr_pos-1 && !seeked ) return 0;
// if close enough, just read up to current
	int gop = avctx->gop_size;
	if( gop < 4 ) gop = 4;
	if( gop > 64 ) gop = 64;
	int read_limit = curr_pos + 3*gop;
	if( pos >= curr_pos && pos <= read_limit ) return 0;
// guarantee preload more than 2*gop frames
	if( seek(pos - 3*gop, frame_rate) < 0 ) return -1;
	return 1;
}

int FFVideoStream::init_frame(AVFrame *picture)
{
	switch( avctx->pix_fmt ) {
	case AV_PIX_FMT_VAAPI:
		picture->format = AV_PIX_FMT_NV12;
		break;
	case AV_PIX_FMT_VULKAN:
		picture->format = avctx->sw_pix_fmt;
		break;
	default:
		picture->format = avctx->pix_fmt;
		break;
	}
	picture->width  = avctx->width;
	picture->height = avctx->height;
	int ret = av_frame_get_buffer(picture, 32);
	return ret;
}

int FFVideoStream::convert_hw_frame(AVFrame *ifrm, AVFrame *ofrm)
{
	AVPixelFormat ifmt = (AVPixelFormat)ifrm->format;
	AVPixelFormat ofmt = (AVPixelFormat)st->codecpar->format;
	ofrm->width  = ifrm->width;
	ofrm->height = ifrm->height;
	ofrm->format = ofmt;
        int ret = av_frame_get_buffer(ofrm, 32);
	if( ret < 0 ) {
		ff_err(ret, "FFVideoStream::convert_hw_frame:"
				" av_frame_get_buffer failed\n");
		return -1;
	}
	fconvert_ctx = sws_getCachedContext(fconvert_ctx,
		ifrm->width, ifrm->height, ifmt,
		ofrm->width, ofrm->height, ofmt,
		SWS_POINT, NULL, NULL, NULL);
	if( !fconvert_ctx ) {
		ff_err(AVERROR(EINVAL), "FFVideoStream::convert_hw_frame:"
				" sws_getCachedContext() failed\n");
		return -1;
	}
	int codec_range = st->codecpar->color_range;
	int codec_space = st->codecpar->color_space;
	const int *codec_table = sws_getCoefficients(codec_space);
	int *inv_table, *table, src_range, dst_range;
	int brightness, contrast, saturation;
	if( !sws_getColorspaceDetails(fconvert_ctx,
			&inv_table, &src_range, &table, &dst_range,
			&brightness, &contrast, &saturation) ) {
		if( src_range != codec_range || dst_range != codec_range ||
		    inv_table != codec_table || table != codec_table )
			sws_setColorspaceDetails(fconvert_ctx,
					codec_table, codec_range, codec_table, codec_range,
					brightness, contrast, saturation);
	}
	ret = sws_scale(fconvert_ctx,
		ifrm->data, ifrm->linesize, 0, ifrm->height,
		ofrm->data, ofrm->linesize);
	if( ret < 0 ) {
		ff_err(ret, "FFVideoStream::convert_hw_frame:"
				" sws_scale() failed\nfile: %s\n",
				ffmpeg->fmt_ctx->url);
		return -1;
	}
	return 0;
}

int FFVideoStream::load_filter(AVFrame *frame)
{
	AVPixelFormat pix_fmt = (AVPixelFormat)frame->format;
	if( pix_fmt == hw_pixfmt ) {
		AVFrame *hw_frame = this->frame;
		av_frame_unref(hw_frame);
		int ret = av_hwframe_transfer_data(hw_frame, frame, 0);
		if( ret < 0 ) {
			eprintf(_("Error retrieving data from GPU to CPU\nfile: %s\n"),
				ffmpeg->fmt_ctx->url);
			return -1;
		}
		av_frame_unref(frame);
		ret = convert_hw_frame(hw_frame, frame);
		if( ret < 0 ) {
			eprintf(_("Error converting data from GPU to CPU\nfile: %s\n"),
				ffmpeg->fmt_ctx->url);
			return -1;
		}
		av_frame_unref(hw_frame);
	}
	return FFStream::load_filter(frame);
}

int FFVideoStream::encode(VFrame *vframe)
{
	if( encode_activate() <= 0 ) return -1;
	ffmpeg->flow_ctl();
	FFrame *picture = new FFrame(this);
	int ret = picture->initted();
	if( ret >= 0 ) {
		AVFrame *frame = *picture;
		frame->pts = curr_pos;
		ret = convert_pixfmt(vframe, frame);
	}

	if( ret >= 0 && avctx->hw_frames_ctx )
		encode_hw_write(picture);
	if( ret >= 0 ) {
		picture->queue(curr_pos);
		++curr_pos;
	}
	else {
		fprintf(stderr, "FFVideoStream::encode: encode failed\n");
		delete picture;
	}
	return ret >= 0 ? 0 : 1;
}

int FFVideoStream::drain()
{

	return 0;
}

int FFVideoStream::encode_frame(AVFrame *frame)
{
	if( frame ) {
#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(59,16,100)
		frame->interlaced_frame = interlaced;
		frame->top_field_first = top_field_first;
#else
		if(top_field_first)
		frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
		if(interlaced)
		frame->flags |= AV_FRAME_FLAG_INTERLACED;
#endif
	}
	if( frame && (frame->format == AV_PIX_FMT_VAAPI || frame->format == AV_PIX_FMT_VULKAN))  { // ugly
		int ret = avcodec_send_frame(avctx, frame);
		for( int retry=MAX_RETRY; !ret && --retry>=0; ) {
			FFPacket pkt;  av_init_packet(pkt);
			pkt->data = NULL;  pkt->size = 0;
			if( (ret=avcodec_receive_packet(avctx, pkt)) < 0 ) {
				if( ret == AVERROR(EAGAIN) ) ret = 0; // weird
				break;
			}
			ret = write_packet(pkt);
			pkt->stream_index = 0;
			av_packet_unref(pkt);
		}
		if( ret < 0 ) {
			ff_err(ret, "FFStream::encode_frame: vaapi encode failed.\nfile: %s\n",
				ffmpeg->fmt_ctx->url);
			return -1;
		}
		return 0;
	}
	return FFStream::encode_frame(frame);
}

int FFVideoStream::write_packet(FFPacket &pkt)
{
	if( !(ffmpeg->fmt_ctx->oformat->flags & AVFMT_VARIABLE_FPS) )
		pkt->duration = 1;
	return FFStream::write_packet(pkt);
}

AVPixelFormat FFVideoConvert::color_model_to_pix_fmt(int color_model)
{
	switch( color_model ) {
	case BC_YUV422:		return AV_PIX_FMT_YUYV422;
	case BC_RGB888:		return AV_PIX_FMT_RGB24;
	case BC_RGBA8888:	return AV_PIX_FMT_RGBA;
	case BC_BGR8888:	return AV_PIX_FMT_BGR0;
	case BC_BGR888:		return AV_PIX_FMT_BGR24;
	case BC_ARGB8888:	return AV_PIX_FMT_ARGB;
	case BC_ABGR8888:	return AV_PIX_FMT_ABGR;
	case BC_RGB8:		return AV_PIX_FMT_RGB8;
	case BC_YUV420P:	return AV_PIX_FMT_YUV420P;
	case BC_YUV422P:	return AV_PIX_FMT_YUV422P;
	case BC_YUV444P:	return AV_PIX_FMT_YUV444P;
	case BC_YUV411P:	return AV_PIX_FMT_YUV411P;
	case BC_RGB565:		return AV_PIX_FMT_RGB565;
	case BC_RGB161616:      return AV_PIX_FMT_RGB48LE;
	case BC_RGBA16161616:   return AV_PIX_FMT_RGBA64LE;
	case BC_AYUV16161616:	return AV_PIX_FMT_AYUV64LE;
	case BC_GBRP:		return AV_PIX_FMT_GBRP;
	default: break;
	}

	return AV_PIX_FMT_NB;
}

int FFVideoConvert::pix_fmt_to_color_model(AVPixelFormat pix_fmt)
{
	switch (pix_fmt) {
	case AV_PIX_FMT_YUYV422:	return BC_YUV422;
	case AV_PIX_FMT_RGB24:		return BC_RGB888;
	case AV_PIX_FMT_RGBA:		return BC_RGBA8888;
	case AV_PIX_FMT_BGR0:		return BC_BGR8888;
	case AV_PIX_FMT_BGR24:		return BC_BGR888;
	case AV_PIX_FMT_ARGB:		return BC_ARGB8888;
	case AV_PIX_FMT_ABGR:		return BC_ABGR8888;
	case AV_PIX_FMT_RGB8:		return BC_RGB8;
	case AV_PIX_FMT_YUV420P:	return BC_YUV420P;
	case AV_PIX_FMT_YUV422P:	return BC_YUV422P;
	case AV_PIX_FMT_YUV444P:	return BC_YUV444P;
	case AV_PIX_FMT_YUV411P:	return BC_YUV411P;
	case AV_PIX_FMT_RGB565:		return BC_RGB565;
	case AV_PIX_FMT_RGB48LE:	return BC_RGB161616;
	case AV_PIX_FMT_RGBA64LE:	return BC_RGBA16161616;
	case AV_PIX_FMT_AYUV64LE:	return BC_AYUV16161616;
	case AV_PIX_FMT_GBRP:		return BC_GBRP;
	default: break;
	}

	return -1;
}

int FFVideoConvert::convert_picture_vframe(VFrame *frame, AVFrame *ip)
{
	AVFrame *ipic = av_frame_alloc();
	int ret = convert_picture_vframe(frame, ip, ipic);
	av_frame_free(&ipic);
	return ret;
}

int FFVideoConvert::convert_picture_vframe(VFrame *frame, AVFrame *ip, AVFrame *ipic)
{ // picture = vframe
	int cmodel = frame->get_color_model();
	AVPixelFormat ofmt = color_model_to_pix_fmt(cmodel);
	if( ofmt == AV_PIX_FMT_NB ) return -1;
	int size = av_image_fill_arrays(ipic->data, ipic->linesize,
		frame->get_data(), ofmt, frame->get_w(), frame->get_h(), 1);
	if( size < 0 ) return -1;

	int bpp = BC_CModels::calculate_pixelsize(cmodel);
	int ysz = bpp * frame->get_w(), usz = ysz;
	switch( cmodel ) {
	case BC_YUV410P:
	case BC_YUV411P:
		usz /= 2;
	case BC_YUV420P:
	case BC_YUV422P:
		usz /= 2;
	case BC_YUV444P:
	case BC_GBRP:
		// override av_image_fill_arrays() for planar types
		ipic->data[0] = frame->get_y();  ipic->linesize[0] = ysz;
		ipic->data[1] = frame->get_u();  ipic->linesize[1] = usz;
		ipic->data[2] = frame->get_v();  ipic->linesize[2] = usz;
		break;
	default:
		ipic->data[0] = frame->get_data();
		ipic->linesize[0] = frame->get_bytes_per_line();
		break;
	}

	AVPixelFormat pix_fmt = (AVPixelFormat)ip->format;
	FFVideoStream *vid =(FFVideoStream *)this;
	
	if( pix_fmt == vid->hw_pixfmt ) {
		int ret = 0;
		if( !sw_frame && !(sw_frame=av_frame_alloc()) )
			ret = AVERROR(ENOMEM);
		if( !ret ) {
			ret = av_hwframe_transfer_data(sw_frame, ip, 0);
			ip = sw_frame;
			pix_fmt = (AVPixelFormat)ip->format;
		}
		if( ret < 0 ) {
			eprintf(_("Error retrieving data from GPU to CPU\nfile: %s\n"),
				vid->ffmpeg->fmt_ctx->url);
			return -1;
		}
	}
	convert_ctx = sws_getCachedContext(convert_ctx, ip->width, ip->height, pix_fmt,
		frame->get_w(), frame->get_h(), ofmt, SWS_POINT, NULL, NULL, NULL);
	if( !convert_ctx ) {
		fprintf(stderr, "FFVideoConvert::convert_picture_frame:"
				" sws_getCachedContext() failed\n");
		return -1;
	}
	
	
	int color_range = 0;
	switch( preferences->yuv_color_range ) {
	case BC_COLORS_JPEG:  color_range = 1;  break;
	case BC_COLORS_MPEG:  color_range = 0;  break;
	}
	int color_space = SWS_CS_ITU601;
	switch( preferences->yuv_color_space ) {
	case BC_COLORS_BT601_PAL:  color_space = SWS_CS_ITU601;  break;
	case BC_COLORS_BT601_NTSC: color_space = SWS_CS_SMPTE170M; break;
	case BC_COLORS_BT709:  color_space = SWS_CS_ITU709;  break;
	case BC_COLORS_BT2020_NCL: 
	case BC_COLORS_BT2020_CL: color_space = SWS_CS_BT2020;  break;
	}
	const int *color_table = sws_getCoefficients(color_space);

	int *inv_table, *table, src_range, dst_range;
	int brightness, contrast, saturation;
	if( !sws_getColorspaceDetails(convert_ctx,
			&inv_table, &src_range, &table, &dst_range,
			&brightness, &contrast, &saturation) ) {
		if( src_range != color_range || dst_range != color_range ||
		    inv_table != color_table || table != color_table )
			sws_setColorspaceDetails(convert_ctx,
					color_table, color_range, color_table, color_range,
					brightness, contrast, saturation);
	}

	int ret = sws_scale(convert_ctx, ip->data, ip->linesize, 0, ip->height,
	    ipic->data, ipic->linesize);
	if( ret < 0 ) {
		ff_err(ret, "FFVideoConvert::convert_picture_frame: sws_scale() failed\nfile: %s\n",
			vid->ffmpeg->fmt_ctx->url);
		return -1;
	}
	
#ifdef HAVE_LIBLCMS2
	const AVFrameSideData *sd;
	cmsHPROFILE profile = 0, profile1 = 0;
	cmsContext cms_ctx;
	cmsHTRANSFORM hTransform;

	sd = av_frame_get_side_data(ip, AV_FRAME_DATA_ICC_PROFILE);
	if(sd) {
	profile = cmsOpenProfileFromMemTHR(cms_ctx, sd->data, sd->size);
	char *profile1_path, *profile1_path_hardcoded; 
	profile1_path = getenv("CIN_CM_PROFILE");
	//strcpy(profile1_path_hardcoded, "/usr/share/apps/krita/profiles/WideGamut.icm");
	
	if(profile1_path) {
	profile1 = cmsOpenProfileFromFile(profile1_path, "r");
	
	hTransform = cmsCreateTransformTHR(cms_ctx, profile,
                                TYPE_RGBA_8,
                                profile1,
                                TYPE_RGBA_8,
                                INTENT_PERCEPTUAL, cmsFLAGS_NOOPTIMIZE | cmsFLAGS_BLACKPOINTCOMPENSATION | cmsFLAGS_COPY_ALPHA);
        if(!hTransform)
        printf("failed to create transform1 \n");

	//printf ("cmodel: %i \n", cmodel);
	uint8_t *data = frame->get_data();
	if (cmodel == BC_RGBA8888 && hTransform) {
	cmsDoTransform(hTransform, data, data, frame->get_w() * frame->get_h());
	printf("cms! \n");
	}
	cmsCloseProfile(profile1);
	}
	cmsCloseProfile(profile);
	}
#endif // lcms2

	return 0;
}

int FFVideoConvert::convert_cmodel(VFrame *frame, AVFrame *ip)
{
	// try direct transfer
	if( !convert_picture_vframe(frame, ip) ) return 1;
	// use indirect transfer
	AVPixelFormat ifmt = (AVPixelFormat)ip->format;
	const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(ifmt);
	int max_bits = 0;
	for( int i = 0; i <desc->nb_components; ++i ) {
		int bits = desc->comp[i].depth;
		if( bits > max_bits ) max_bits = bits;
	}
	int imodel = pix_fmt_to_color_model(ifmt);
	int imodel_is_yuv = BC_CModels::is_yuv(imodel);
	int cmodel = frame->get_color_model();
	int cmodel_is_yuv = BC_CModels::is_yuv(cmodel);
	if( imodel < 0 || imodel_is_yuv != cmodel_is_yuv ) {
		imodel = cmodel_is_yuv ?
		    (BC_CModels::has_alpha(cmodel) ?
			BC_AYUV16161616 :
			(max_bits > 8 ? BC_AYUV16161616 : BC_YUV444P)) :
		    (BC_CModels::has_alpha(cmodel) ?
			(max_bits > 8 ? BC_RGBA16161616 : BC_RGBA8888) :
			(max_bits > 8 ? BC_RGB161616 : BC_RGB888)) ;
	}
	VFrame vframe(ip->width, ip->height, imodel);
	if( convert_picture_vframe(&vframe, ip) ) return -1;
	frame->transfer_from(&vframe);
	return 1;
}

int FFVideoConvert::transfer_cmodel(VFrame *frame, AVFrame *ifp)
{
	int ret = convert_cmodel(frame, ifp);
	if( ret > 0 ) {
		const AVDictionary *src = ifp->metadata;
		AVDictionaryEntry *t = NULL;
		BC_Hash *hp = frame->get_params();
		//hp->clear();
		while( (t=av_dict_get(src, "", t, AV_DICT_IGNORE_SUFFIX)) )
			hp->update(t->key, t->value);
	}
	return ret;
}

int FFVideoConvert::convert_vframe_picture(VFrame *frame, AVFrame *op)
{
	AVFrame *opic = av_frame_alloc();
	int ret = convert_vframe_picture(frame, op, opic);
	av_frame_free(&opic);
	return ret;
}

int FFVideoConvert::convert_vframe_picture(VFrame *frame, AVFrame *op, AVFrame *opic)
{ // vframe = picture
	int cmodel = frame->get_color_model();
	AVPixelFormat ifmt = color_model_to_pix_fmt(cmodel);
	if( ifmt == AV_PIX_FMT_NB ) return -1;
	int size = av_image_fill_arrays(opic->data, opic->linesize,
		 frame->get_data(), ifmt, frame->get_w(), frame->get_h(), 1);
	if( size < 0 ) return -1;

	int bpp = BC_CModels::calculate_pixelsize(cmodel);
	int ysz = bpp * frame->get_w(), usz = ysz;
	switch( cmodel ) {
	case BC_YUV410P:
	case BC_YUV411P:
		usz /= 2;
	case BC_YUV420P:
	case BC_YUV422P:
		usz /= 2;
	case BC_YUV444P:
	case BC_GBRP:
		// override av_image_fill_arrays() for planar types
		opic->data[0] = frame->get_y();  opic->linesize[0] = ysz;
		opic->data[1] = frame->get_u();  opic->linesize[1] = usz;
		opic->data[2] = frame->get_v();  opic->linesize[2] = usz;
		break;
	default:
		opic->data[0] = frame->get_data();
		opic->linesize[0] = frame->get_bytes_per_line();
		break;
	}

	AVPixelFormat ofmt = (AVPixelFormat)op->format;
	convert_ctx = sws_getCachedContext(convert_ctx, frame->get_w(), frame->get_h(),
		ifmt, op->width, op->height, ofmt, SWS_POINT, NULL, NULL, NULL);
	if( !convert_ctx ) {
		fprintf(stderr, "FFVideoConvert::convert_frame_picture:"
				" sws_getCachedContext() failed\n");
		return -1;
	}


	int color_range = 0;
	switch( preferences->yuv_color_range ) {
	case BC_COLORS_JPEG:  color_range = 1;  break;
	case BC_COLORS_MPEG:  color_range = 0;  break;
	}
	int color_space = SWS_CS_ITU601;
	switch( preferences->yuv_color_space ) {
	case BC_COLORS_BT601_PAL:  color_space = SWS_CS_ITU601;  break;
	case BC_COLORS_BT601_NTSC: color_space = SWS_CS_SMPTE170M; break;
	case BC_COLORS_BT709:  color_space = SWS_CS_ITU709;  break;
	case BC_COLORS_BT2020_NCL:
	case BC_COLORS_BT2020_CL: color_space = SWS_CS_BT2020;  break;
	}
	const int *color_table = sws_getCoefficients(color_space);

	int *inv_table, *table, src_range, dst_range;
	int brightness, contrast, saturation;
	if( !sws_getColorspaceDetails(convert_ctx,
			&inv_table, &src_range, &table, &dst_range,
			&brightness, &contrast, &saturation) ) {
		if( dst_range != color_range || table != color_table )
			sws_setColorspaceDetails(convert_ctx,
					inv_table, src_range, color_table, color_range,
					brightness, contrast, saturation);
	}

	int ret = sws_scale(convert_ctx, opic->data, opic->linesize, 0, frame->get_h(),
			op->data, op->linesize);
	if( ret < 0 ) {
		ff_err(ret, "FFVideoConvert::convert_frame_picture: sws_scale() failed\n");
		return -1;
	}
	

	return 0;
}

int FFVideoConvert::convert_pixfmt(VFrame *frame, AVFrame *op)
{
	// try direct transfer
	if( !convert_vframe_picture(frame, op) ) return 1;
	// use indirect transfer
	int cmodel = frame->get_color_model();
	int max_bits = BC_CModels::calculate_pixelsize(cmodel) * 8;
	max_bits /= BC_CModels::components(cmodel);
	AVPixelFormat ofmt = (AVPixelFormat)op->format;
	int imodel = pix_fmt_to_color_model(ofmt);
	int imodel_is_yuv = BC_CModels::is_yuv(imodel);
	int cmodel_is_yuv = BC_CModels::is_yuv(cmodel);
	if( imodel < 0 || imodel_is_yuv != cmodel_is_yuv ) {
		imodel = cmodel_is_yuv ?
		    (BC_CModels::has_alpha(cmodel) ?
			BC_AYUV16161616 :
			(max_bits > 8 ? BC_AYUV16161616 : BC_YUV444P)) :
		    (BC_CModels::has_alpha(cmodel) ?
			(max_bits > 8 ? BC_RGBA16161616 : BC_RGBA8888) :
			(max_bits > 8 ? BC_RGB161616 : BC_RGB888)) ;
	}
	VFrame vframe(frame->get_w(), frame->get_h(), imodel);
	vframe.transfer_from(frame);
	if( !convert_vframe_picture(&vframe, op) ) return 1;
	return -1;
}

int FFVideoConvert::transfer_pixfmt(VFrame *frame, AVFrame *ofp)
{
	int ret = convert_pixfmt(frame, ofp);
	if( ret > 0 ) {
		BC_Hash *hp = frame->get_params();
		AVDictionary **dict = &ofp->metadata;
		//av_dict_free(dict);
		for( int i=0; i<hp->size(); ++i ) {
			char *key = hp->get_key(i), *val = hp->get_value(i);
			av_dict_set(dict, key, val, 0);
		}
	}
	return ret;
}

void FFVideoStream::load_markers()
{
	IndexState *index_state = ffmpeg->file_base->asset->index_state;
	if( !index_state || idx >= index_state->video_markers.size() ) return;
	FFStream::load_markers(*index_state->video_markers[idx], frame_rate);
}

IndexMarks *FFVideoStream::get_markers()
{
	IndexState *index_state = ffmpeg->file_base->asset->index_state;
	if( !index_state || idx >= index_state->video_markers.size() ) return 0;
	return !index_state ? 0 : index_state->video_markers[idx];
}


FFMPEG::FFMPEG(FileBase *file_base)
{
	fmt_ctx = 0;
	this->file_base = file_base;
	memset(file_format,0,sizeof(file_format));
	mux_lock = new Condition(0,"FFMPEG::mux_lock",0);
	flow_lock = new Condition(1,"FFStream::flow_lock",0);
	done = -1;
	flow = 1;
	decoding = encoding = 0;
	has_audio = has_video = 0;
	interlace_from_codec = 0;
	opts = 0;
	opt_duration = -1;
	opt_video_filter = 0;
	opt_audio_filter = 0;
	opt_hw_dev = 0;
	opt_video_decoder = 0;
	opt_audio_decoder = 0;
	fflags = 0;
	char option_path[BCTEXTLEN];
	set_option_path(option_path, "%s", "ffmpeg.opts");
	read_options(option_path, opts);
}

FFMPEG::~FFMPEG()
{
	ff_lock("FFMPEG::~FFMPEG()");
	close_encoder();
	ffaudio.remove_all_objects();
	ffvideo.remove_all_objects();
	if( fmt_ctx ) avformat_close_input(&fmt_ctx);
	ff_unlock();
	delete flow_lock;
	delete mux_lock;
	av_dict_free(&opts);
	delete [] opt_video_filter;
	delete [] opt_audio_filter;
	delete [] opt_hw_dev;
}

#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
int FFMPEG::check_sample_rate(const AVCodec *codec, int sample_rate)
#else
int FFMPEG::check_sample_rate(AVCodec *codec, int sample_rate)
#endif
{
	const int *p = codec->supported_samplerates;
	if( !p ) return sample_rate;
	while( *p != 0 ) {
		if( *p == sample_rate ) return *p;
		++p;
	}
	return 0;
}

// check_frame_rate and std_frame_rate needed for 23.976
// and 59.94 fps mpeg2
static inline AVRational std_frame_rate(int i)
{
	static const int m1 = 1001*12, m2 = 1000*12;
	static const int freqs[] = {
		40*m1, 48*m1, 50*m1, 60*m1, 80*m1,120*m1, 240*m1,
		24*m2, 30*m2, 60*m2, 12*m2, 15*m2, 48*m2, 90*m2,
		100*m2, 120*m2, 144*m2, 72*m2, 0,
	};
	int freq = i<30*12 ? (i+1)*1001 : freqs[i-30*12];
	return (AVRational) { freq, 1001*12 };
}

AVRational FFMPEG::check_frame_rate(const AVRational *p, double frame_rate)
{
	AVRational rate, best_rate = (AVRational) { 0, 0 };
	double max_err = 1.;  int i = 0;
	while( ((p ? (rate=*p++) : (rate=std_frame_rate(i++))), rate.num) != 0 ) {
		double framerate = (double) rate.num / rate.den;
		double err = fabs(frame_rate/framerate - 1.);
		if( err >= max_err ) continue;
		max_err = err;
		best_rate = rate;
	}
	return max_err < 0.0001 ? best_rate : (AVRational) { 0, 0 };
}

AVRational FFMPEG::to_sample_aspect_ratio(Asset *asset)
{
#if 1
	double display_aspect = asset->width / (double)asset->height;
	double sample_aspect = display_aspect / asset->aspect_ratio;
	int width = 1000000, height = width * sample_aspect + 0.5;
	float w, h;
	MWindow::create_aspect_ratio(w, h, width, height);
	return (AVRational){(int)w, (int)h};
#else
// square pixels
	return (AVRational){1, 1};
#endif
}

AVRational FFMPEG::to_time_base(int sample_rate)
{
	return (AVRational){1, sample_rate};
}

int FFMPEG::get_fmt_score(AVSampleFormat dst_fmt, AVSampleFormat src_fmt)
{
	int score = 0;
	int dst_planar = av_sample_fmt_is_planar(dst_fmt);
	int src_planar = av_sample_fmt_is_planar(src_fmt);
	if( dst_planar != src_planar ) ++score;
	int dst_bytes = av_get_bytes_per_sample(dst_fmt);
	int src_bytes = av_get_bytes_per_sample(src_fmt);
	score += (src_bytes > dst_bytes ? 100 : -10) * (src_bytes - dst_bytes);
	int src_packed = av_get_packed_sample_fmt(src_fmt);
	int dst_packed = av_get_packed_sample_fmt(dst_fmt);
	if( dst_packed == AV_SAMPLE_FMT_S32 && src_packed == AV_SAMPLE_FMT_FLT ) score += 20;
	if( dst_packed == AV_SAMPLE_FMT_FLT && src_packed == AV_SAMPLE_FMT_S32 ) score += 2;
	return score;
}

AVSampleFormat FFMPEG::find_best_sample_fmt_of_list(
		const AVSampleFormat *sample_fmts, AVSampleFormat src_fmt)
{
	AVSampleFormat best = AV_SAMPLE_FMT_NONE;
	int best_score = get_fmt_score(best, src_fmt);
	for( int i=0; sample_fmts[i] >= 0; ++i ) {
		AVSampleFormat sample_fmt = sample_fmts[i];
		int score = get_fmt_score(sample_fmt, src_fmt);
		if( score >= best_score ) continue;
		best = sample_fmt;  best_score = score;
	}
	return best;
}


void FFMPEG::set_option_path(char *path, const char *fmt, ...)
{
	char *ep = path + BCTEXTLEN-1;
	strncpy(path, File::get_cindat_path(), ep-path);
	strncat(path, "/ffmpeg/", ep-path);
	path += strlen(path);
	va_list ap;
	va_start(ap, fmt);
	path += vsnprintf(path, ep-path, fmt, ap);
	va_end(ap);
	*path = 0;
}

void FFMPEG::get_option_path(char *path, const char *type, const char *spec)
{
	if( *spec == '/' )
		strcpy(path, spec);
	else
		set_option_path(path, "%s/%s", type, spec);
}

int FFMPEG::get_format(char *format, const char *path, const char *spec)
{
	char option_path[BCTEXTLEN], line[BCTEXTLEN], codec[BCTEXTLEN];
	get_option_path(option_path, path, spec);
	FILE *fp = fopen(option_path,"r");
	if( !fp ) return 1;
	int ret = 0;
	if( !fgets(line, sizeof(line), fp) ) ret = 1;
	if( !ret ) {
		line[sizeof(line)-1] = 0;
		ret = scan_option_line(line, format, codec);
	}
	fclose(fp);
	return ret;
}

int FFMPEG::get_codec(char *codec, const char *path, const char *spec)
{
	char option_path[BCTEXTLEN], line[BCTEXTLEN], format[BCTEXTLEN];
	get_option_path(option_path, path, spec);
	FILE *fp = fopen(option_path,"r");
	if( !fp ) return 1;
	int ret = 0;
	if( !fgets(line, sizeof(line), fp) ) ret = 1;
	fclose(fp);
	if( !ret ) {
		line[sizeof(line)-1] = 0;
		ret = scan_option_line(line, format, codec);
	}
	if( !ret ) {
		char *vp = codec, *ep = vp+BCTEXTLEN-1;
		while( vp < ep && *vp && *vp != '|' ) ++vp;
		if( *vp == '|' ) --vp;
		while( vp > codec && (*vp==' ' || *vp=='\t') ) *vp-- = 0;
	}
	return ret;
}

int FFMPEG::get_file_format()
{
	char audio_muxer[BCSTRLEN], video_muxer[BCSTRLEN];
	char audio_format[BCSTRLEN], video_format[BCSTRLEN];
	audio_muxer[0] = audio_format[0] = 0;
	video_muxer[0] = video_format[0] = 0;
	Asset *asset = file_base->asset;
	int ret = asset ? 0 : 1;
	if( !ret && asset->audio_data ) {
		if( !(ret=get_format(audio_format, "audio", asset->acodec)) ) {
			if( get_format(audio_muxer, "format", audio_format) ) {
				strcpy(audio_muxer, audio_format);
				audio_format[0] = 0;
			}
		}
	}
	if( !ret && asset->video_data ) {
		if( !(ret=get_format(video_format, "video", asset->vcodec)) ) {
			if( get_format(video_muxer, "format", video_format) ) {
				strcpy(video_muxer, video_format);
				video_format[0] = 0;
			}
		}
	}
	if( !ret && !audio_muxer[0] && !video_muxer[0] )
		ret = 1;
	if( !ret && audio_muxer[0] && video_muxer[0] &&
	    strcmp(audio_muxer, video_muxer) ) ret = -1;
	if( !ret && audio_format[0] && video_format[0] &&
	    strcmp(audio_format, video_format) ) ret = -1;
	if( !ret )
		strcpy(file_format, !audio_format[0] && !video_format[0] ?
			(audio_muxer[0] ? audio_muxer : video_muxer) :
			(audio_format[0] ? audio_format : video_format));
	return ret;
}

int FFMPEG::scan_option_line(const char *cp, char *tag, char *val)
{
	while( *cp == ' ' || *cp == '\t' ) ++cp;
	const char *bp = cp;
	while( *cp && *cp != ' ' && *cp != '\t' && *cp != '=' && *cp != '\n' ) ++cp;
	int len = cp - bp;
	if( !len || len > BCSTRLEN-1 ) return 1;
	while( bp < cp ) *tag++ = *bp++;
	*tag = 0;
	while( *cp == ' ' || *cp == '\t' ) ++cp;
	if( *cp == '=' ) ++cp;
	while( *cp == ' ' || *cp == '\t' ) ++cp;
	bp = cp;
	while( *cp && *cp != '\n' ) ++cp;
	len = cp - bp;
	if( len > BCTEXTLEN-1 ) return 1;
	while( bp < cp ) *val++ = *bp++;
	*val = 0;
	return 0;
}

int FFMPEG::can_render(const char *fformat, const char *type)
{
	FileSystem fs;
	char option_path[BCTEXTLEN];
	FFMPEG::set_option_path(option_path, type);
	fs.update(option_path);
	int total_files = fs.total_files();
	for( int i=0; i<total_files; ++i ) {
		const char *name = fs.get_entry(i)->get_name();
		const char *ext = strrchr(name,'.');
		if( !ext ) continue;
		if( !strcmp(fformat, ++ext) ) return 1;
	}
	return 0;
}

int FFMPEG::get_ff_option(const char *nm, const char *options, char *value)
{
	for( const char *cp=options; *cp!=0; ) {
		char line[BCTEXTLEN], *bp = line, *ep = bp+sizeof(line)-1;
		while( bp < ep && *cp && *cp!='\n' ) *bp++ = *cp++;
		if( *cp ) ++cp;
		*bp = 0;
		if( !line[0] || line[0] == '#' || line[0] == ';' ) continue;
		char key[BCSTRLEN], val[BCTEXTLEN];
		if( FFMPEG::scan_option_line(line, key, val) ) continue;
		if( !strcmp(key, nm) ) {
			strncpy(value, val, BCSTRLEN);
			return 0;
		}
	}
	return 1;
}

void FFMPEG::scan_audio_options(Asset *asset, EDL *edl)
{
	char cin_sample_fmt[BCSTRLEN];
	int cin_fmt = AV_SAMPLE_FMT_NONE;
	const char *options = asset->ff_audio_options;
	if( !get_ff_option("cin_sample_fmt", options, cin_sample_fmt) )
		cin_fmt = (int)av_get_sample_fmt(cin_sample_fmt);
	if( cin_fmt < 0 ) {
		char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100)
		const AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ?
#else
		AVCodec *av_codec = !FFMPEG::get_codec(audio_codec, "audio", asset->acodec) ?
#endif
			avcodec_find_encoder_by_name(audio_codec) : 0;
		if( av_codec && av_codec->sample_fmts )
			cin_fmt = find_best_sample_fmt_of_list(av_codec->sample_fmts, AV_SAMPLE_FMT_FLT);
	}
	if( cin_fmt < 0 ) cin_fmt = AV_SAMPLE_FMT_S16;
	const char *name = av_get_sample_fmt_name((AVSampleFormat)cin_fmt);
	if( !name ) name = _("None");
	strcpy(asset->ff_sample_format, name);

	char value[BCSTRLEN];
	if( !get_ff_option("cin_bitrate", options, value) )
		asset->ff_audio_bitrate = atoi(value);
	if( !get_ff_option("cin_quality", options, value) )
		asset->ff_audio_quality = atoi(value);
}

void FFMPEG::load_audio_options(Asset *asset, EDL *edl)
{
	char options_path[BCTEXTLEN];
	set_option_path(options_path, "audio/%s", asset->acodec);
	if( !load_options(options_path,
			asset->ff_audio_options,
			sizeof(asset->ff_audio_options)) )
		scan_audio_options(asset, edl);
}

void FFMPEG::scan_video_options(Asset *asset, EDL *edl)
{
	char cin_pix_fmt[BCSTRLEN];
	int cin_fmt = AV_PIX_FMT_NONE;
	const char *options = asset->ff_video_options;
	if( !get_ff_option("cin_pix_fmt", options, cin_pix_fmt) )
			cin_fmt = (int)av_get_pix_fmt(cin_pix_fmt);
	if( cin_fmt < 0 ) {
		char video_codec[BCSTRLEN];  video_codec[0] = 0;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100)
		const AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ?
#else
		AVCodec *av_codec = !get_codec(video_codec, "video", asset->vcodec) ?
#endif
			avcodec_find_encoder_by_name(video_codec) : 0;
		if( av_codec && av_codec->pix_fmts ) {
			if( 0 && edl ) { // frequently picks a bad answer
				int color_model = edl->session->color_model;
				int max_bits = BC_CModels::calculate_pixelsize(color_model) * 8;
				max_bits /= BC_CModels::components(color_model);
				cin_fmt = avcodec_find_best_pix_fmt_of_list(av_codec->pix_fmts,
					(BC_CModels::is_yuv(color_model) ?
						(max_bits > 8 ? AV_PIX_FMT_AYUV64LE : AV_PIX_FMT_YUV444P) :
						(max_bits > 8 ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGB24)), 0, 0);
			}
			else
				cin_fmt = av_codec->pix_fmts[0];
		}
	}
	if( cin_fmt < 0 ) cin_fmt = AV_PIX_FMT_YUV420P;
	const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get((AVPixelFormat)cin_fmt);
	const char *name = desc ? desc->name : _("None");
	strcpy(asset->ff_pixel_format, name);

	char value[BCSTRLEN];
	if( !get_ff_option("cin_bitrate", options, value) )
		asset->ff_video_bitrate = atoi(value);
	if( !get_ff_option("cin_quality", options, value) )
		asset->ff_video_quality = atoi(value);
}

void FFMPEG::load_video_options(Asset *asset, EDL *edl)
{
	char options_path[BCTEXTLEN];
	set_option_path(options_path, "video/%s", asset->vcodec);
	if( !load_options(options_path,
			asset->ff_video_options,
			sizeof(asset->ff_video_options)) )
		scan_video_options(asset, edl);
}

void FFMPEG::scan_format_options(Asset *asset, EDL *edl)
{
}

void FFMPEG::load_format_options(Asset *asset, EDL *edl)
{
	char options_path[BCTEXTLEN];
	set_option_path(options_path, "format/%s", asset->fformat);
	if( !load_options(options_path,
			asset->ff_format_options,
			sizeof(asset->ff_format_options)) )
		scan_format_options(asset, edl);
}

int FFMPEG::load_defaults(const char *path, const char *type,
		 char *codec, char *codec_options, int len)
{
	char default_file[BCTEXTLEN];
	set_option_path(default_file, "%s/%s.dfl", path, type);
	FILE *fp = fopen(default_file,"r");
	if( !fp ) return 1;
	fgets(codec, BCSTRLEN, fp);
	char *cp = codec;
	while( *cp && *cp!='\n' ) ++cp;
	*cp = 0;
	while( len > 0 && fgets(codec_options, len, fp) ) {
		int n = strlen(codec_options);
		codec_options += n;  len -= n;
	}
	fclose(fp);
	set_option_path(default_file, "%s/%s", path, codec);
	return load_options(default_file, codec_options, len);
}

void FFMPEG::set_asset_format(Asset *asset, EDL *edl, const char *text)
{
	if( asset->format != FILE_FFMPEG ) return;
	if( text != asset->fformat )
		strcpy(asset->fformat, text);
	if( !asset->ff_format_options[0] )
		load_format_options(asset, edl);
	if( asset->audio_data && !asset->ff_audio_options[0] ) {
		if( !load_defaults("audio", text, asset->acodec,
				asset->ff_audio_options, sizeof(asset->ff_audio_options)) )
			scan_audio_options(asset, edl);
		else
			asset->audio_data = 0;
	}
	if( asset->video_data && !asset->ff_video_options[0] ) {
		if( !load_defaults("video", text, asset->vcodec,
				asset->ff_video_options, sizeof(asset->ff_video_options)) )
			scan_video_options(asset, edl);
		else
			asset->video_data = 0;
	}
}

int FFMPEG::get_encoder(const char *options,
		char *format, char *codec, char *bsfilter)
{
	FILE *fp = fopen(options,"r");
	if( !fp ) {
		eprintf(_("options open failed %s\n"),options);
		return 1;
	}
	char line[BCTEXTLEN];
	if( !fgets(line, sizeof(line), fp) ||
	    scan_encoder(line, format, codec, bsfilter) )
		eprintf(_("format/codec not found %s\n"), options);
	fclose(fp);
	return 0;
}

int FFMPEG::scan_encoder(const char *line,
		char *format, char *codec, char *bsfilter)
{
	format[0] = codec[0] = bsfilter[0] = 0;
	if( scan_option_line(line, format, codec) ) return 1;
	char *cp = codec;
	while( *cp && *cp != '|' ) ++cp;
	if( !*cp ) return 0;
	char *bp = cp;
	do { *bp-- = 0; } while( bp>=codec && (*bp==' ' || *bp == '\t' ) );
	while( *++cp && (*cp==' ' || *cp == '\t') );
	bp = bsfilter;
	for( int i=BCTEXTLEN; --i>0 && *cp; ) *bp++ = *cp++;
	*bp = 0;
	return 0;
}

int FFMPEG::read_options(const char *options, AVDictionary *&opts, int skip)
{
	FILE *fp = fopen(options,"r");
	if( !fp ) return 1;
	int ret = 0;
	while( !ret && --skip >= 0 ) {
		int ch = getc(fp);
		while( ch >= 0 && ch != '\n' ) ch = getc(fp);
		if( ch < 0 ) ret = 1;
	}
	if( !ret )
		ret = read_options(fp, options, opts);
	fclose(fp);
	return ret;
}

int FFMPEG::scan_options(const char *options, AVDictionary *&opts, AVStream *st)
{
	FILE *fp = fmemopen((void *)options,strlen(options),"r");
	if( !fp ) return 0;
	int ret = read_options(fp, options, opts);
	fclose(fp);
	if( !ret && st ) {
		AVDictionaryEntry *tag = av_dict_get(opts, "id", NULL, 0);
		if( tag ) st->id = strtol(tag->value,0,0);
	}
	return ret;
}

void FFMPEG::put_cache_frame(VFrame *frame, int64_t position)
{
	file_base->file->put_cache_frame(frame, position, 0);
}

int FFMPEG::get_use_cache()
{
	return file_base->file->get_use_cache();
}

void FFMPEG::purge_cache()
{
	file_base->file->purge_cache();
}

FFCodecRemap::FFCodecRemap()
{
	old_codec = 0;
	new_codec = 0;
}
FFCodecRemap::~FFCodecRemap()
{
	delete [] old_codec;
	delete [] new_codec;
}

int FFCodecRemaps::add(const char *val)
{
	char old_codec[BCSTRLEN], new_codec[BCSTRLEN];
	if( sscanf(val, " %63[a-zA-z0-9_-] = %63[a-z0-9_-]",
		&old_codec[0], &new_codec[0]) != 2 ) return 1;
	FFCodecRemap &remap = append();
	remap.old_codec = cstrdup(old_codec);
	remap.new_codec = cstrdup(new_codec);
	return 0;
}

#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
int FFCodecRemaps::update(AVCodecID &codec_id, const AVCodec *&decoder)
{
	const AVCodec *codec = avcodec_find_decoder(codec_id);
#else
int FFCodecRemaps::update(AVCodecID &codec_id, AVCodec *&decoder)
{
	AVCodec *codec = avcodec_find_decoder(codec_id);
#endif
	if( !codec ) return -1;
	const char *name = codec->name;
	FFCodecRemaps &map = *this;
	int k = map.size();
	while( --k >= 0 && strcmp(map[k].old_codec, name) );
	if( k < 0 ) return 1;
	const char *new_codec = map[k].new_codec;
	codec = avcodec_find_decoder_by_name(new_codec);
	if( !codec ) return -1;
	decoder = codec;
	return 0;
}

int FFMPEG::read_options(FILE *fp, const char *options, AVDictionary *&opts)
{
	int ret = 0, no = 0;
	char line[BCTEXTLEN];
	while( !ret && fgets(line, sizeof(line), fp) ) {
		line[sizeof(line)-1] = 0;
		if( line[0] == '#' ) continue;
		if( line[0] == '\n' ) continue;
		char key[BCSTRLEN], val[BCTEXTLEN];
		if( scan_option_line(line, key, val) ) {
			eprintf(_("err reading %s: line %d\n"), options, no);
			ret = 1;
		}
		if( !ret ) {
			if( !strcmp(key, "duration") )
				opt_duration = strtod(val, 0);
			else if( !strcmp(key, "video_decoder") )
				opt_video_decoder = cstrdup(val);
			else if( !strcmp(key, "audio_decoder") )
				opt_audio_decoder = cstrdup(val);
			else if( !strcmp(key, "remap_video_decoder") )
				video_codec_remaps.add(val);
			else if( !strcmp(key, "remap_audio_decoder") )
				audio_codec_remaps.add(val);
			else if( !strcmp(key, "video_filter") )
				opt_video_filter = cstrdup(val);
			else if( !strcmp(key, "audio_filter") )
				opt_audio_filter = cstrdup(val);
			else if( !strcmp(key, "cin_hw_dev") )
				opt_hw_dev = cstrdup(val);
			else if( !strcmp(key, "loglevel") )
				set_loglevel(val);
			else
				av_dict_set(&opts, key, val, 0);
		}
	}
	return ret;
}

int FFMPEG::load_options(const char *options, AVDictionary *&opts)
{
	char option_path[BCTEXTLEN];
	set_option_path(option_path, "%s", options);
	return read_options(option_path, opts);
}

int FFMPEG::load_options(const char *path, char *bfr, int len)
{
	*bfr = 0;
	FILE *fp = fopen(path, "r");
	if( !fp ) return 1;
	fgets(bfr, len, fp); // skip hdr
	len = fread(bfr, 1, len-1, fp);
	if( len < 0 ) len = 0;
	bfr[len] = 0;
	fclose(fp);
	return 0;
}

void FFMPEG::set_loglevel(const char *ap)
{
	if( !ap || !*ap ) return;
	const struct {
		const char *name;
		int level;
	} log_levels[] = {
		{ "quiet"  , AV_LOG_QUIET   },
		{ "panic"  , AV_LOG_PANIC   },
		{ "fatal"  , AV_LOG_FATAL   },
		{ "error"  , AV_LOG_ERROR   },
		{ "warning", AV_LOG_WARNING },
		{ "info"   , AV_LOG_INFO    },
		{ "verbose", AV_LOG_VERBOSE },
		{ "debug"  , AV_LOG_DEBUG   },
	};
	for( int i=0; i<(int)(sizeof(log_levels)/sizeof(log_levels[0])); ++i ) {
		if( !strcmp(log_levels[i].name, ap) ) {
			av_log_set_level(log_levels[i].level);
			return;
		}
	}
	av_log_set_level(atoi(ap));
}

double FFMPEG::to_secs(int64_t time, AVRational time_base)
{
	double base_time = time == AV_NOPTS_VALUE ? 0 :
		av_rescale_q(time, time_base, AV_TIME_BASE_Q);
	return base_time / AV_TIME_BASE;
}

int FFMPEG::info(char *text, int len)
{
	if( len <= 0 ) return 0;
	decode_activate();
#define report(s...) do { int n = snprintf(cp,len,s); cp += n;  len -= n; } while(0)
	char *cp = text;
	report("format: %s\n",fmt_ctx->iformat->name);
	if( ffvideo.size() > 0 )
		report("\n%d video stream%s\n",ffvideo.size(), ffvideo.size()!=1 ? "s" : "");
	for( int vidx=0; vidx<ffvideo.size(); ++vidx ) {
		const char *unkn = _("(unkn)");
		FFVideoStream *vid = ffvideo[vidx];
		AVStream *st = vid->st;
		AVCodecID codec_id = st->codecpar->codec_id;
		report(_("vid%d (%d),  id 0x%06x:\n"), vid->idx, vid->fidx, codec_id);
		const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
		report("  video%d %s ", vidx+1, desc ? desc->name : unkn);
		report(" %dx%d %5.2f", vid->width, vid->height, vid->frame_rate);
		AVPixelFormat pix_fmt = (AVPixelFormat)st->codecpar->format;
		const char *pfn = av_get_pix_fmt_name(pix_fmt);
		report(" pix %s\n", pfn ? pfn : unkn);
		int interlace = st->codecpar->field_order;
		report("  interlace (container level): %i\n", interlace ? interlace : -1);
		int interlace_codec = interlace_from_codec;
		report("  interlace (codec level): %i\n", interlace_codec ? interlace_codec : -1);
		enum AVColorSpace space = st->codecpar->color_space;
		const char *nm = av_color_space_name(space);
		report("    color space:%s", nm ? nm : unkn);
    		enum AVColorRange range = st->codecpar->color_range;
		const char *rg = av_color_range_name(range);
		report("/ range:%s\n", rg ? rg : unkn);

		enum AVColorPrimaries primaries = st->codecpar->color_primaries;
		const char *pr = av_color_primaries_name(primaries);
		report("    color primaries:%s", pr ? pr : unkn);
    		enum AVColorTransferCharacteristic trc = st->codecpar->color_trc;
		const char *transfer = av_color_transfer_name(trc);
		report("/ transfer characteristics:%s\n", transfer ? transfer : unkn);


		AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx, st, NULL);
		AVRational display_aspect_ratio;
		if(sar.num) {
		
    		av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
                  st->codecpar->width  * (int64_t)sar.num,
                  st->codecpar->height * (int64_t)sar.den,
                  1024 * 1024);
/*		report("  Guessed SAR: %d:%d, ", sar.num, sar.den );
		report("DAR: %d:%d \n", display_aspect_ratio.num, display_aspect_ratio.den); */
		}
		if (st->sample_aspect_ratio.num)
		{
    		av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
                  st->codecpar->width  * (int64_t)st->sample_aspect_ratio.num,
                  st->codecpar->height * (int64_t)st->sample_aspect_ratio.den,
                  1024 * 1024);
		report("  container Detected SAR: %d:%d , DAR %d:%d \n", st->sample_aspect_ratio.num, st->sample_aspect_ratio.den, display_aspect_ratio.num, display_aspect_ratio.den);
		}
		if (st->codecpar->sample_aspect_ratio.num)
		{
    		av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
                  st->codecpar->width  * (int64_t)st->codecpar->sample_aspect_ratio.num,
                  st->codecpar->height * (int64_t)st->codecpar->sample_aspect_ratio.den,
                  1024 * 1024);
		report("  codec Detected SAR: %d:%d , DAR %d:%d \n", st->codecpar->sample_aspect_ratio.num, st->codecpar->sample_aspect_ratio.den, display_aspect_ratio.num, display_aspect_ratio.den);
		}
		double secs = to_secs(st->duration, st->time_base);
		int64_t length = secs * vid->frame_rate + 0.5;
		double ofs = to_secs((vid->nudge - st->start_time), st->time_base);
		int64_t nudge = ofs * vid->frame_rate;
		int ch = nudge >= 0 ? '+' : (nudge=-nudge, '-');
		report("    %jd%c%jd frms %0.2f secs", length,ch,nudge, secs);
		int hrs = secs/3600;  secs -= hrs*3600;
		int mins = secs/60;  secs -= mins*60;
		report("  %d:%02d:%05.2f\n", hrs, mins, secs);
		double theta = vid->get_rotation_angle();
		if( fabs(theta) > 1 ) 
			report("    rotation angle: %0.1f\n", theta);
	}
	if( ffaudio.size() > 0 )
		report("\n%d audio stream%s\n",ffaudio.size(), ffaudio.size()!=1 ? "s" : "");
	for( int aidx=0; aidx<ffaudio.size(); ++aidx ) {
		FFAudioStream *aud = ffaudio[aidx];
		AVStream *st = aud->st;
		AVCodecID codec_id = st->codecpar->codec_id;
		report(_("aud%d (%d),  id 0x%06x:\n"), aud->idx, aud->fidx, codec_id);
		const AVCodecDescriptor *desc = avcodec_descriptor_get(codec_id);
		int nch = aud->channels, ch0 = aud->channel0+1;
		report("  audio%d-%d %s", ch0, ch0+nch-1, desc ? desc->name : " (unkn)");
		AVSampleFormat sample_fmt = (AVSampleFormat)st->codecpar->format;
		const char *fmt = av_get_sample_fmt_name(sample_fmt);
		report(" %s %d", fmt, aud->sample_rate);
		int sample_bits = av_get_bits_per_sample(codec_id);
		report(" %dbits\n", sample_bits);
		double secs = to_secs(st->duration, st->time_base);
		int64_t length = secs * aud->sample_rate + 0.5;
		double ofs = to_secs((aud->nudge - st->start_time), st->time_base);
		int64_t nudge = ofs * aud->sample_rate;
		int ch = nudge >= 0 ? '+' : (nudge=-nudge, '-');
		report("    %jd%c%jd smpl %0.2f secs", length,ch,nudge, secs);
		int hrs = secs/3600;  secs -= hrs*3600;
		int mins = secs/60;  secs -= mins*60;
		report("  %d:%02d:%05.2f\n", hrs, mins, secs);
	}
	if( fmt_ctx->nb_programs > 0 )
		report("\n%d program%s\n",fmt_ctx->nb_programs, fmt_ctx->nb_programs!=1 ? "s" : "");
	for( int i=0; i<(int)fmt_ctx->nb_programs; ++i ) {
		report("program %d", i+1);
		AVProgram *pgrm = fmt_ctx->programs[i];
		for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) {
			int idx = pgrm->stream_index[j];
			int vidx = ffvideo.size();
			while( --vidx>=0 && ffvideo[vidx]->fidx != idx );
			if( vidx >= 0 ) {
				report(", vid%d", vidx);
				continue;
			}
			int aidx = ffaudio.size();
			while( --aidx>=0 && ffaudio[aidx]->fidx != idx );
			if( aidx >= 0 ) {
				report(", aud%d", aidx);
				continue;
			}
			report(", (%d)", pgrm->stream_index[j]);
		}
		report("\n");
	}
	report("\n");
	AVDictionaryEntry *tag = 0;
	while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
		report("%s=%s\n", tag->key, tag->value);

	if( !len ) --cp;
	*cp = 0;
	return cp - text;
#undef report
}


int FFMPEG::init_decoder(const char *filename)
{
	ff_lock("FFMPEG::init_decoder");
	av_register_all();
	char file_opts[BCTEXTLEN];
	strcpy(file_opts, filename);
	char *bp = strrchr(file_opts, '/');
	if( !bp ) bp = file_opts;
	char *sp = strrchr(bp, '.');
	if( !sp ) sp = bp + strlen(bp);
	FILE *fp = 0;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,18,100)
	const AVInputFormat *ifmt = 0;
#else
	AVInputFormat *ifmt = 0;
#endif
	if( sp ) {
		strcpy(sp, ".opts");
		fp = fopen(file_opts, "r");
	}
	if( fp ) {
		read_options(fp, file_opts, opts);
		fclose(fp);
		AVDictionaryEntry *tag;
		if( (tag=av_dict_get(opts, "format", NULL, 0)) != 0 ) {
			ifmt = av_find_input_format(tag->value);
		}
	}
	else
		load_options("decode.opts", opts);
	AVDictionary *fopts = 0;
	av_dict_copy(&fopts, opts, 0);
	int ret = avformat_open_input(&fmt_ctx, filename, ifmt, &fopts);
	av_dict_free(&fopts);
	if( ret >= 0 )
		ret = avformat_find_stream_info(fmt_ctx, NULL);
	if( !ret ) {
		decoding = -1;
	}
	ff_unlock();
	return !ret ? 0 : 1;
}

int FFMPEG::open_decoder()
{
	struct stat st;
	if( stat(fmt_ctx->url, &st) < 0 ) {
		eprintf(_("can't stat file: %s\n"), fmt_ctx->url);
		return 1;
	}

	int64_t file_bits = 8 * st.st_size;
	if( !fmt_ctx->bit_rate && opt_duration > 0 )
		fmt_ctx->bit_rate = file_bits / opt_duration;

	int estimated = 0;
	if( fmt_ctx->bit_rate > 0 ) {
		for( int i=0; i<(int)fmt_ctx->nb_streams; ++i ) {
			AVStream *st = fmt_ctx->streams[i];
			if( st->duration != AV_NOPTS_VALUE ) continue;
			if( st->time_base.num > INT64_MAX / fmt_ctx->bit_rate ) continue;
			st->duration = av_rescale(file_bits, st->time_base.den,
				fmt_ctx->bit_rate * (int64_t) st->time_base.num);
			estimated = 1;
		}
	}
	if( estimated && !(fflags & FF_ESTM_TIMES) ) {
		fflags |= FF_ESTM_TIMES;
		printf("FFMPEG::open_decoder: some stream times estimated: %s\n",
			fmt_ctx->url);
	}

	ff_lock("FFMPEG::open_decoder");
	int ret = 0, bad_time = 0;
	for( int i=0; !ret && i<(int)fmt_ctx->nb_streams; ++i ) {
		AVStream *st = fmt_ctx->streams[i];
		if( st->duration == AV_NOPTS_VALUE ) bad_time = 1;
		AVCodecParameters *avpar = st->codecpar;
		const AVCodecDescriptor *codec_desc = avcodec_descriptor_get(avpar->codec_id);
		if( !codec_desc ) continue;
		switch( avpar->codec_type ) {
		case AVMEDIA_TYPE_VIDEO: {
			if( avpar->width < 1 ) continue;
			if( avpar->height < 1 ) continue;
			AVRational framerate = av_guess_frame_rate(fmt_ctx, st, 0);
			if( framerate.num < 1 ) continue;
			has_video = 1;
			int vidx = ffvideo.size();
			FFVideoStream *vid = new FFVideoStream(this, st, vidx, i);
			vstrm_index.append(ffidx(vidx, 0));
			ffvideo.append(vid);
			vid->width = avpar->width;
			vid->height = avpar->height;
			vid->frame_rate = !framerate.den ? 0 : (double)framerate.num / framerate.den;
			switch( avpar->color_range ) {
			case AVCOL_RANGE_MPEG:
				vid->color_range = BC_COLORS_MPEG;
				break;
			case AVCOL_RANGE_JPEG:
				vid->color_range = BC_COLORS_JPEG;
				break;
			default:
				vid->color_range = !file_base ? BC_COLORS_JPEG :
					file_base->file->preferences->yuv_color_range;
				break;
			}
			switch( avpar->color_space ) {
			case AVCOL_SPC_BT470BG:
				vid->color_space = BC_COLORS_BT601_PAL;
				break;
			case AVCOL_SPC_SMPTE170M:
				vid->color_space = BC_COLORS_BT601_NTSC;
				break;
			case AVCOL_SPC_BT709:
				vid->color_space = BC_COLORS_BT709;
				break;
			case AVCOL_SPC_BT2020_NCL:
				vid->color_space = BC_COLORS_BT2020_NCL;
				break;
			case AVCOL_SPC_BT2020_CL:
				vid->color_space = BC_COLORS_BT2020_CL;
				break;
			default:
				vid->color_space = !file_base ? BC_COLORS_BT601_NTSC :
					file_base->file->preferences->yuv_color_space;
				break;
			}
			double secs = to_secs(st->duration, st->time_base);
			vid->length = secs * vid->frame_rate;
			vid->aspect_ratio = (double)st->sample_aspect_ratio.num / st->sample_aspect_ratio.den;
			vid->nudge = st->start_time;
			vid->reading = -1;
			ret = vid->create_filter(opt_video_filter);
			break; }
		case AVMEDIA_TYPE_AUDIO: {
			if( avpar->ch_layout.nb_channels < 1 ) continue;
			if( avpar->sample_rate < 1 ) continue;
			has_audio = 1;
			int aidx = ffaudio.size();
			FFAudioStream *aud = new FFAudioStream(this, st, aidx, i);
			ffaudio.append(aud);
			aud->channel0 = astrm_index.size();
			aud->channels = avpar->ch_layout.nb_channels;
			for( int ch=0; ch<aud->channels; ++ch )
				astrm_index.append(ffidx(aidx, ch));
			aud->sample_rate = avpar->sample_rate;
			double secs = to_secs(st->duration, st->time_base);
			aud->length = secs * aud->sample_rate;
			aud->init_swr(aud->channels, avpar->format, aud->sample_rate);
			aud->nudge = st->start_time;
			aud->reading = -1;
			ret = aud->create_filter(opt_audio_filter);
			break; }
		default: break;
		}
	}
	if( bad_time && !(fflags & FF_BAD_TIMES) ) {
		fflags |= FF_BAD_TIMES;
		printf(_("FFMPEG::open_decoder: some stream have bad times: %s\n"),
			fmt_ctx->url);
	}
	ff_unlock();
	return ret < 0 ? -1 : 0;
}


int FFMPEG::init_encoder(const char *filename)
{
// try access first for named pipes
	int ret = access(filename, W_OK);
	if( ret ) {
		int fd = ::open(filename,O_WRONLY);
		if( fd < 0 ) fd = open(filename,O_WRONLY+O_CREAT,0666);
		if( fd >= 0 ) { close(fd);  ret = 0; }
	}
	if( ret ) {
		eprintf(_("bad file path: %s\n"), filename);
		return 1;
	}
	ret = get_file_format();
	if( ret > 0 ) {
		eprintf(_("bad file format: %s\n"), filename);
		return 1;
	}
	if( ret < 0 ) {
		eprintf(_("mismatch audio/video file format: %s\n"), filename);
		return 1;
	}
	ff_lock("FFMPEG::init_encoder");
	av_register_all();
	char format[BCSTRLEN];
	if( get_format(format, "format", file_format) )
		strcpy(format, file_format);
	avformat_alloc_output_context2(&fmt_ctx, 0, format, filename);
	if( !fmt_ctx ) {
		eprintf(_("failed: %s\n"), filename);
		ret = 1;
	}
	if( !ret ) {
		encoding = -1;
		load_options("encode.opts", opts);
	}
	ff_unlock();
	return ret;
}

int FFMPEG::open_encoder(const char *type, const char *spec)
{

	Asset *asset = file_base->asset;
	char *filename = asset->path;
	AVDictionary *sopts = 0;
	av_dict_copy(&sopts, opts, 0);
	char option_path[BCTEXTLEN];
	set_option_path(option_path, "%s/%s.opts", type, type);
	read_options(option_path, sopts);
	get_option_path(option_path, type, spec);
	char format_name[BCSTRLEN], codec_name[BCTEXTLEN], bsfilter[BCTEXTLEN];
	if( get_encoder(option_path, format_name, codec_name, bsfilter) ) {
		eprintf(_("get_encoder failed %s:%s\n"), option_path, filename);
		return 1;
	}

#ifdef HAVE_DV
	if( !strcmp(codec_name, CODEC_TAG_DVSD) ) strcpy(codec_name, "dv");
#endif
	else if( !strcmp(codec_name, CODEC_TAG_MJPEG) ) strcpy(codec_name, "mjpeg");
	else if( !strcmp(codec_name, CODEC_TAG_JPEG) ) strcpy(codec_name, "jpeg");

	int ret = 0;
	ff_lock("FFMPEG::open_encoder");
	FFStream *fst = 0;
	AVStream *st = 0;
	AVCodecContext *ctx = 0;
	
	/* some encoders dislike ildct flag in ffmpeg 7.0/7.1 */
	int is_no_ildct;
	if (!strcmp (codec_name, "av1_qsv") ||
	!strcmp(codec_name, "h264_qsv") ||
	!strcmp(codec_name, "vp9_qsv"))
		is_no_ildct=1;
	else
		is_no_ildct=0;


	const AVCodecDescriptor *codec_desc = 0;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
	const AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
#else
	AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
#endif
	if( !codec ) {
		eprintf(_("cant find codec %s:%s\n"), codec_name, filename);
		ret = 1;
	}
	if( !ret ) {
		codec_desc = avcodec_descriptor_get(codec->id);
		if( !codec_desc ) {
			eprintf(_("unknown codec %s:%s\n"), codec_name, filename);
			ret = 1;
		}
	}
	if( !ret ) {
		st = avformat_new_stream(fmt_ctx, 0);
		if( !st ) {
			eprintf(_("cant create stream %s:%s\n"), codec_name, filename);
			ret = 1;
		}
	}
	if( !ret ) {
		switch( codec_desc->type ) {
		case AVMEDIA_TYPE_AUDIO: {
			if( has_audio ) {
				eprintf(_("duplicate audio %s:%s\n"), codec_name, filename);
				ret = 1;
				break;
			}
			if( scan_options(asset->ff_audio_options, sopts, st) ) {
				eprintf(_("bad audio options %s:%s\n"), codec_name, filename);
				ret = 1;
				break;
			}
			has_audio = 1;
			ctx = avcodec_alloc_context3(codec);
			if( asset->ff_audio_bitrate > 0 ) {
				ctx->bit_rate = asset->ff_audio_bitrate;
				char arg[BCSTRLEN];
				sprintf(arg, "%d", asset->ff_audio_bitrate);
				av_dict_set(&sopts, "b", arg, 0);
			}
			else if( asset->ff_audio_quality >= 0 ) {
				ctx->global_quality = asset->ff_audio_quality * FF_QP2LAMBDA;
				ctx->qmin    = ctx->qmax =  asset->ff_audio_quality;
				ctx->mb_lmin = ctx->qmin * FF_QP2LAMBDA;
				ctx->mb_lmax = ctx->qmax * FF_QP2LAMBDA;
				ctx->flags |= AV_CODEC_FLAG_QSCALE;
				char arg[BCSTRLEN];
				av_dict_set(&sopts, "flags", "+qscale", 0);
				sprintf(arg, "%d", asset->ff_audio_quality);
				av_dict_set(&sopts, "qscale", arg, 0);
				sprintf(arg, "%d", ctx->global_quality);
				av_dict_set(&sopts, "global_quality", arg, 0);
			}
			int aidx = ffaudio.size();
			int fidx = aidx + ffvideo.size();
			FFAudioStream *aud = new FFAudioStream(this, st, aidx, fidx);
			aud->avctx = ctx;  ffaudio.append(aud);  fst = aud;
			aud->sample_rate = asset->sample_rate;
			ctx->ch_layout.nb_channels = aud->channels = asset->channels;
			for( int ch=0; ch<aud->channels; ++ch )
				astrm_index.append(ffidx(aidx, ch));
			AVChannelLayout ch_layout;
			av_channel_layout_default(&ch_layout, ctx->ch_layout.nb_channels);
			ctx->ch_layout.u.mask =  ch_layout.u.mask;
			av_channel_layout_copy(&ctx->ch_layout, &ch_layout);
			ctx->sample_rate = check_sample_rate(codec, asset->sample_rate);
			if( !ctx->sample_rate ) {
				eprintf(_("check_sample_rate failed %s\n"), filename);
				ret = 1;
				break;
			}
			ctx->time_base = st->time_base = (AVRational){1, aud->sample_rate};
			AVSampleFormat sample_fmt = av_get_sample_fmt(asset->ff_sample_format);
			if( sample_fmt == AV_SAMPLE_FMT_NONE )
				sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_S16;
			ctx->sample_fmt = sample_fmt;
			//uint64_t layout = av_get_default_channel_layout(ctx->ch_layout.nb_channels);
			AVChannelLayout layout;
			av_channel_layout_default(&layout, ctx->ch_layout.nb_channels);
			swr_alloc_set_opts2(&aud->resample_context,
				&layout, ctx->sample_fmt, aud->sample_rate,
				&layout, AV_SAMPLE_FMT_FLT, ctx->sample_rate,
				0, NULL);
			swr_init(aud->resample_context);
			aud->writing = -1;
			break; }
		case AVMEDIA_TYPE_VIDEO: {
			if( has_video ) {
				eprintf(_("duplicate video %s:%s\n"), codec_name, filename);
				ret = 1;
				break;
			}
			if( scan_options(asset->ff_video_options, sopts, st) ) {
				eprintf(_("bad video options %s:%s\n"), codec_name, filename);
				ret = 1;
				break;
			}
			has_video = 1;
			ctx = avcodec_alloc_context3(codec);
			if( asset->ff_video_bitrate > 0 ) {
				ctx->bit_rate = asset->ff_video_bitrate;
				char arg[BCSTRLEN];
				sprintf(arg, "%d", asset->ff_video_bitrate);
				av_dict_set(&sopts, "b", arg, 0);
			}
			else if( asset->ff_video_quality >= 0 ) {
				ctx->global_quality = asset->ff_video_quality * FF_QP2LAMBDA;
				ctx->qmin    = ctx->qmax =  asset->ff_video_quality;
				ctx->mb_lmin = ctx->qmin * FF_QP2LAMBDA;
				ctx->mb_lmax = ctx->qmax * FF_QP2LAMBDA;
				ctx->flags |= AV_CODEC_FLAG_QSCALE;
				char arg[BCSTRLEN];
				av_dict_set(&sopts, "flags", "+qscale", 0);
				sprintf(arg, "%d", asset->ff_video_quality);
				av_dict_set(&sopts, "qscale", arg, 0);
				sprintf(arg, "%d", ctx->global_quality);
				av_dict_set(&sopts, "global_quality", arg, 0);
			}
			int vidx = ffvideo.size();
			int fidx = vidx + ffaudio.size();
			FFVideoStream *vid = new FFVideoStream(this, st, vidx, fidx);
			vstrm_index.append(ffidx(vidx, 0));
			vid->avctx = ctx;  ffvideo.append(vid);  fst = vid;
			vid->width = asset->width;
			vid->height = asset->height;
			vid->frame_rate = asset->frame_rate;
#if 0
			char tc_str[20] = "00:00:00:00";
			double tc_offset;
			if(asset->timecode > 0)
			Units::totext(tc_str, asset->timecode, TIME_HMSF, 0, asset->frame_rate, 0);
			//printf("tc: %s \n", tc_str);
			av_dict_set(&st->metadata, "timecode", tc_str, 0);
#endif
			if( (vid->color_range = asset->ff_color_range) < 0 )
				vid->color_range = file_base->file->preferences->yuv_color_range;
			switch( vid->color_range ) {
			case BC_COLORS_MPEG:  ctx->color_range = AVCOL_RANGE_MPEG;  break;
			case BC_COLORS_JPEG:  ctx->color_range = AVCOL_RANGE_JPEG;  break;
			}
			if( (vid->color_space = asset->ff_color_space) < 0 )
				vid->color_space = file_base->file->preferences->yuv_color_space;
			switch( vid->color_space ) {
			case BC_COLORS_BT601_NTSC:  ctx->colorspace = AVCOL_SPC_SMPTE170M;  break;
			case BC_COLORS_BT601_PAL: ctx->colorspace = AVCOL_SPC_BT470BG; break;
			case BC_COLORS_BT709:  ctx->colorspace = AVCOL_SPC_BT709;      break;
			case BC_COLORS_BT2020_NCL: ctx->colorspace = AVCOL_SPC_BT2020_NCL; break;
			case BC_COLORS_BT2020_CL: ctx->colorspace = AVCOL_SPC_BT2020_CL; break;
			}
			AVPixelFormat pix_fmt = av_get_pix_fmt(asset->ff_pixel_format);
			AVHWDeviceType hw_type;
			
			int vulkan  = 0;
			if(!strcmp(codec_name, "h264_vulkan") ||
			  !strcmp(codec_name, "h265_vulkan") ||
			  !strcmp(codec_name, "ffv1_vulkan") ||
			  !strcmp(codec_name, "av1_vulkan"))
			vulkan = 1;
			
			if( opt_hw_dev != 0 || vulkan == 1) {
			if(opt_hw_dev)
				hw_type = vid->encode_hw_activate(opt_hw_dev, asset->ff_pixel_format);
			if(vulkan == 1)
				hw_type = vid->encode_hw_activate("vulkan", asset->ff_pixel_format);
				switch( hw_type ) {
				case AV_HWDEVICE_TYPE_VAAPI:
					pix_fmt = AV_PIX_FMT_VAAPI;
					break;
				case AV_HWDEVICE_TYPE_VULKAN:
					pix_fmt = AV_PIX_FMT_VULKAN;
					break;
				case AV_HWDEVICE_TYPE_NONE:
				default: break;
				}
			}
			if( pix_fmt == AV_PIX_FMT_NONE )
				pix_fmt = codec->pix_fmts ? codec->pix_fmts[0] : AV_PIX_FMT_YUV420P;
			ctx->pix_fmt = pix_fmt;

			const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
			int mask_w = (1<<desc->log2_chroma_w)-1;
			ctx->width = (vid->width+mask_w) & ~mask_w;
			int mask_h = (1<<desc->log2_chroma_h)-1;
			ctx->height = (vid->height+mask_h) & ~mask_h;
			ctx->sample_aspect_ratio = to_sample_aspect_ratio(asset);
			AVRational frame_rate;
			if (ctx->codec->id == AV_CODEC_ID_MPEG1VIDEO ||
			    ctx->codec->id == AV_CODEC_ID_MPEG2VIDEO)
			frame_rate = check_frame_rate(codec->supported_framerates, vid->frame_rate);
			else
			frame_rate = av_d2q(vid->frame_rate, INT_MAX);
			if( !frame_rate.num || !frame_rate.den ) {
				eprintf(_("check_frame_rate failed %s\n"), filename);
				ret = 1;
				break;
			}
			av_reduce(&frame_rate.num, &frame_rate.den,
				frame_rate.num, frame_rate.den, INT_MAX);
			ctx->framerate = (AVRational) { frame_rate.num, frame_rate.den };
			ctx->time_base = (AVRational) { frame_rate.den, frame_rate.num };
			if(!strcmp(format_name, "webm") || !strcmp(format_name, "matroska") || !strcmp(format_name, "mov") ||
			!strcmp(format_name, "qt") || !strcmp(format_name, "mp4") || !strcmp(format_name, "avi") ||
			!strcmp(format_name, "dv") || !strcmp(format_name, "yuv4mpegpipe"))
			{
			if (to_sample_aspect_ratio(asset).den > 0)
			st->sample_aspect_ratio = to_sample_aspect_ratio(asset);
			}
			st->avg_frame_rate = frame_rate;
			st->time_base = ctx->time_base;
			vid->writing = -1;
			vid->interlaced = asset->interlace_mode == ILACE_MODE_TOP_FIRST ||
				asset->interlace_mode == ILACE_MODE_BOTTOM_FIRST ? 1 : 0;
			vid->top_field_first = asset->interlace_mode == ILACE_MODE_TOP_FIRST ? 1 : 0;
			switch (asset->interlace_mode)	{		
			case ILACE_MODE_TOP_FIRST: 
			if (ctx->codec->id == AV_CODEC_ID_MJPEG)
			av_dict_set(&sopts, "field_order", "tt", 0); 
			else
			av_dict_set(&sopts, "field_order", "tb", 0); 
			if (ctx->codec_id != AV_CODEC_ID_MJPEG && !is_no_ildct) 
			av_dict_set(&sopts, "flags", "+ilme+ildct", 0);
			break;
			case ILACE_MODE_BOTTOM_FIRST: 
			if (ctx->codec->id == AV_CODEC_ID_MJPEG)
			av_dict_set(&sopts, "field_order", "bb", 0); 
			else
			av_dict_set(&sopts, "field_order", "bt", 0); 
			if (ctx->codec_id != AV_CODEC_ID_MJPEG && !is_no_ildct)
			av_dict_set(&sopts, "flags", "+ilme+ildct", 0);
			break;
			case ILACE_MODE_NOTINTERLACED: av_dict_set(&sopts, "field_order", "progressive", 0); break;
			}
			break; }
		default:
			eprintf(_("not audio/video, %s:%s\n"), codec_name, filename);
			ret = 1;
		}

		if( ctx ) {
			AVDictionaryEntry *tag;
			if( (tag=av_dict_get(sopts, "cin_stats_filename", NULL, 0)) != 0 ) {
				char suffix[BCSTRLEN];  sprintf(suffix,"-%d.log",fst->fidx);
				fst->stats_filename = cstrcat(2, tag->value, suffix);
			}
			if( (tag=av_dict_get(sopts, "flags", NULL, 0)) != 0 ) {
				int pass = fst->pass;
				char *cp = tag->value;
				while( *cp ) {
					int ch = *cp++, pfx = ch=='-' ? -1 : ch=='+' ? 1 : 0;
					if( !isalnum(!pfx ? ch : (ch=*cp++)) ) continue;
					char id[BCSTRLEN], *bp = id, *ep = bp+sizeof(id)-1;
					for( *bp++=ch; isalnum(ch=*cp); ++cp )
						if( bp < ep ) *bp++ = ch;
					*bp = 0;
					if( !strcmp(id, "pass1") ) {
						pass = pfx<0 ? (pass&~1) : pfx>0 ? (pass|1) : 1;
					}
					else if( !strcmp(id, "pass2") ) {
						pass = pfx<0 ? (pass&~2) : pfx>0 ? (pass|2) : 2;
					}
				}
				if( (fst->pass=pass) ) {
					if( pass & 1 ) ctx->flags |= AV_CODEC_FLAG_PASS1;
					if( pass & 2 ) ctx->flags |= AV_CODEC_FLAG_PASS2;
				}
			}
		}
	}
	if( !ret ) {
		if( fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER )
			ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
		if( fst->stats_filename && (ret=fst->init_stats_file()) )
			eprintf(_("error: stats file = %s\n"), fst->stats_filename);
	}
	if( !ret ) {
		av_dict_set(&sopts, "cin_bitrate", 0, 0);
		av_dict_set(&sopts, "cin_quality", 0, 0);

		if( !av_dict_get(sopts, "threads", NULL, 0) )
			ctx->thread_count = ff_cpus();
		ret = avcodec_open2(ctx, codec, &sopts);
		if( ret >= 0 ) {
			ret = avcodec_parameters_from_context(st->codecpar, ctx);
			if( ret < 0 )
				fprintf(stderr, "Could not copy the stream parameters\n");
		}
		if( ret >= 0 ) {
_Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100)
			ret = avcodec_copy_context(st->codec, ctx);
#else
			ret = avcodec_parameters_to_context(ctx, st->codecpar);
#endif
_Pragma("GCC diagnostic warning \"-Wdeprecated-declarations\"")
			if( ret < 0 )
				fprintf(stderr, "Could not copy the stream context\n");
		}
		if( ret < 0 ) {
			ff_err(ret,"FFMPEG::open_encoder");
			eprintf(_("open failed %s:%s\n"), codec_name, filename);
			ret = 1;
		}
		else
			ret = 0;
	}
	if( !ret && fst && bsfilter[0] ) {
		ret = av_bsf_list_parse_str(bsfilter, &fst->bsfc);
		if( ret < 0 ) {
			ff_err(ret,"FFMPEG::open_encoder");
			eprintf(_("bitstream filter failed %s:\n%s\n"), filename, bsfilter);
			ret = 1;
		}
		else
			ret = 0;
	}

	if( !ret )
		start_muxer();

	ff_unlock();
	av_dict_free(&sopts);
	return ret;
}

int FFMPEG::close_encoder()
{
	stop_muxer();
	if( encoding > 0 ) {
		av_write_trailer(fmt_ctx);
		if( !(fmt_ctx->flags & AVFMT_NOFILE) )
			avio_closep(&fmt_ctx->pb);
	}
	encoding = 0;
	return 0;
}

int FFMPEG::decode_activate()
{
	if( decoding < 0 ) {
		decoding = 0;
		for( int vidx=0; vidx<ffvideo.size(); ++vidx )
			ffvideo[vidx]->nudge = AV_NOPTS_VALUE;
		for( int aidx=0; aidx<ffaudio.size(); ++aidx )
			ffaudio[aidx]->nudge = AV_NOPTS_VALUE;
		// set nudges for each program stream set
		const int64_t min_nudge = INT64_MIN+1;
		int npgrms = fmt_ctx->nb_programs;
		for( int i=0; i<npgrms; ++i ) {
			AVProgram *pgrm = fmt_ctx->programs[i];
			// first start time video stream
			int64_t vstart_time = min_nudge, astart_time = min_nudge;
			for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) {
				int fidx = pgrm->stream_index[j];
				AVStream *st = fmt_ctx->streams[fidx];
				AVCodecParameters *avpar = st->codecpar;
				if( avpar->codec_type == AVMEDIA_TYPE_VIDEO ) {
					if( st->start_time == AV_NOPTS_VALUE ) continue;
					if( vstart_time < st->start_time )
						vstart_time = st->start_time;
					continue;
				}
				if( avpar->codec_type == AVMEDIA_TYPE_AUDIO ) {
					if( st->start_time == AV_NOPTS_VALUE ) continue;
					if( astart_time < st->start_time )
						astart_time = st->start_time;
					continue;
				}
			}
			//since frame rate is much more grainy than sample rate, it is better to
			// align using video, so that total absolute error is minimized.
			int64_t nudge = vstart_time > min_nudge ? vstart_time :
				astart_time > min_nudge ? astart_time : AV_NOPTS_VALUE;
			for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) {
				int fidx = pgrm->stream_index[j];
				AVStream *st = fmt_ctx->streams[fidx];
				AVCodecParameters *avpar = st->codecpar;
				if( avpar->codec_type == AVMEDIA_TYPE_VIDEO ) {
					for( int k=0; k<ffvideo.size(); ++k ) {
						if( ffvideo[k]->fidx != fidx ) continue;
						ffvideo[k]->nudge = nudge;
					}
					continue;
				}
				if( avpar->codec_type == AVMEDIA_TYPE_AUDIO ) {
					for( int k=0; k<ffaudio.size(); ++k ) {
						if( ffaudio[k]->fidx != fidx ) continue;
						ffaudio[k]->nudge = nudge;
					}
					continue;
				}
			}
		}
		// set nudges for any streams not yet set
		int64_t vstart_time = min_nudge, astart_time = min_nudge;
		int nstreams = fmt_ctx->nb_streams;
		for( int i=0; i<nstreams; ++i ) {
			AVStream *st = fmt_ctx->streams[i];
			AVCodecParameters *avpar = st->codecpar;
			switch( avpar->codec_type ) {
			case AVMEDIA_TYPE_VIDEO: {
				if( st->start_time == AV_NOPTS_VALUE ) continue;
				int vidx = ffvideo.size();
				while( --vidx >= 0 && ffvideo[vidx]->fidx != i );
				if( vidx < 0 ) continue;
				if( ffvideo[vidx]->nudge != AV_NOPTS_VALUE ) continue;
				if( vstart_time < st->start_time )
					vstart_time = st->start_time;
				break; }
			case AVMEDIA_TYPE_AUDIO: {
				if( st->start_time == AV_NOPTS_VALUE ) continue;
				int aidx = ffaudio.size();
				while( --aidx >= 0 && ffaudio[aidx]->fidx != i );
				if( aidx < 0 ) continue;
				if( ffaudio[aidx]->frame_sz < avpar->frame_size )
					ffaudio[aidx]->frame_sz = avpar->frame_size;
				if( ffaudio[aidx]->nudge != AV_NOPTS_VALUE ) continue;
				if( astart_time < st->start_time )
					astart_time = st->start_time;
				break; }
			default: break;
			}
		}
		int64_t nudge = vstart_time > min_nudge ? vstart_time :
			astart_time > min_nudge ? astart_time : 0;
		for( int vidx=0; vidx<ffvideo.size(); ++vidx ) {
			if( ffvideo[vidx]->nudge == AV_NOPTS_VALUE )
				ffvideo[vidx]->nudge = nudge;
		}
		for( int aidx=0; aidx<ffaudio.size(); ++aidx ) {
			if( ffaudio[aidx]->nudge == AV_NOPTS_VALUE )
				ffaudio[aidx]->nudge = nudge;
		}
		decoding = 1;
	}
	return decoding;
}

int FFMPEG::encode_activate()
{
	int ret = 0;
	if( encoding < 0 ) {
		encoding = 0;
		if( !(fmt_ctx->flags & AVFMT_NOFILE) &&
		    (ret=avio_open(&fmt_ctx->pb, fmt_ctx->url, AVIO_FLAG_WRITE)) < 0 ) {
			ff_err(ret, "FFMPEG::encode_activate: err opening : %s\n",
				fmt_ctx->url);
			return -1;
		}
		if( !strcmp(file_format, "image2") ) {
			Asset *asset = file_base->asset;
			const char *filename = asset->path;
			FILE *fp = fopen(filename,"w");
			if( !fp ) {
				eprintf(_("Can't write image2 header file: %s\n  %m"), filename);
				return 1;
			}
			fprintf(fp, "IMAGE2\n");
			fprintf(fp, "# Frame rate: %f\n", asset->frame_rate);
			fprintf(fp, "# Width: %d\n", asset->width);
			fprintf(fp, "# Height: %d\n", asset->height);
			fclose(fp);
		}
		int prog_id = 1;
		AVProgram *prog = av_new_program(fmt_ctx, prog_id);
		for( int i=0; i< ffvideo.size(); ++i )
			av_program_add_stream_index(fmt_ctx, prog_id, ffvideo[i]->fidx);
		for( int i=0; i< ffaudio.size(); ++i )
			av_program_add_stream_index(fmt_ctx, prog_id, ffaudio[i]->fidx);
		int pi = fmt_ctx->nb_programs;
		while(  --pi >= 0 && fmt_ctx->programs[pi]->id != prog_id );
		AVDictionary **meta = &prog->metadata;
		av_dict_set(meta, "service_provider", "cin5", 0);
		const char *path = fmt_ctx->url, *bp = strrchr(path,'/');
		if( bp ) path = bp + 1;
		av_dict_set(meta, "title", path, 0);

		if( ffaudio.size() ) {
			const char *ep = getenv("CIN_AUDIO_LANG"), *lp = 0;
			if( !ep && (lp=getenv("LANG")) ) { // some are guesses
				static struct { const char lc[3], lng[4]; } lcode[] = {
					{ "en", "eng" }, { "de", "ger" }, { "es", "spa" },
					{ "eu", "bas" }, { "fr", "fre" }, { "el", "gre" },
					{ "hi", "hin" }, { "it", "ita" }, { "ja", "jap" },
					{ "ko", "kor" }, { "du", "dut" }, { "pl", "pol" },
					{ "pt", "por" }, { "ru", "rus" }, { "sl", "slv" },
					{ "uk", "ukr" }, { "vi", "vie" }, { "zh", "chi" },
				};
				for( int i=sizeof(lcode)/sizeof(lcode[0]); --i>=0 && !ep; )
					if( !strncmp(lcode[i].lc,lp,2) ) ep = lcode[i].lng;
			}
			if( !ep ) ep = "und";
			char lang[5];
			strncpy(lang,ep,3);  lang[3] = 0;
			AVStream *st = ffaudio[0]->st;
			av_dict_set(&st->metadata,"language",lang,0);
		}

		AVDictionary *fopts = 0;
		char option_path[BCTEXTLEN];
		set_option_path(option_path, "format/%s", file_format);
		read_options(option_path, fopts, 1);
		av_dict_copy(&fopts, opts, 0);
		if( scan_options(file_base->asset->ff_format_options, fopts, 0) ) {
			eprintf(_("bad format options %s\n"), file_base->asset->path);
			ret = -1;
		}
		if( ret >= 0 )
			ret = avformat_write_header(fmt_ctx, &fopts);
		if( ret < 0 ) {
			ff_err(ret, "FFMPEG::encode_activate: write header failed %s\n",
				fmt_ctx->url);
			return -1;
		}
		av_dict_free(&fopts);
		encoding = 1;
	}
	return encoding;
}


int FFMPEG::audio_seek(int stream, int64_t pos)
{
	int aidx = astrm_index[stream].st_idx;
	FFAudioStream *aud = ffaudio[aidx];
	aud->audio_seek(pos);
	return 0;
}

int FFMPEG::video_probe(int64_t pos)
{
	int vidx = vstrm_index[0].st_idx;
	FFVideoStream *vid = ffvideo[vidx];
	vid->probe(pos);
	
	int interlace1 = interlace_from_codec;
	//printf("interlace from codec: %i\n", interlace1);

	switch (interlace1)
	{
	case AV_FIELD_TT:
	case AV_FIELD_TB:
	    return ILACE_MODE_TOP_FIRST;
	case AV_FIELD_BB:
	case AV_FIELD_BT:
	    return ILACE_MODE_BOTTOM_FIRST;
	case AV_FIELD_PROGRESSIVE:
	    return ILACE_MODE_NOTINTERLACED;
	default:
	    return ILACE_MODE_UNDETECTED;
	}

}



int FFMPEG::video_seek(int stream, int64_t pos)
{
	int vidx = vstrm_index[stream].st_idx;
	FFVideoStream *vid = ffvideo[vidx];
	vid->video_seek(pos);
	return 0;
}


int FFMPEG::decode(int chn, int64_t pos, double *samples, int len)
{
	if( !has_audio || chn >= astrm_index.size() ) return -1;
	int aidx = astrm_index[chn].st_idx;
	FFAudioStream *aud = ffaudio[aidx];
	if( aud->load(pos, len) < len ) return -1;
	int ch = astrm_index[chn].st_ch;
	int ret = aud->read(samples,len,ch);
	return ret;
}

int FFMPEG::decode(int layer, int64_t pos, VFrame *vframe)
{
	if( !has_video || layer >= vstrm_index.size() ) return -1;
	int vidx = vstrm_index[layer].st_idx;
	FFVideoStream *vid = ffvideo[vidx];
	return vid->load(vframe, pos);
}


int FFMPEG::encode(int stream, double **samples, int len)
{
	FFAudioStream *aud = ffaudio[stream];
	return aud->encode(samples, len);
}


int FFMPEG::encode(int stream, VFrame *frame)
{
	FFVideoStream *vid = ffvideo[stream];
	return vid->encode(frame);
}

void FFMPEG::start_muxer()
{
	if( !running() ) {
		done = 0;
		start();
	}
}

void FFMPEG::stop_muxer()
{
	if( running() ) {
		done = 1;
		mux_lock->unlock();
	}
	join();
}

void FFMPEG::flow_off()
{
	if( !flow ) return;
	flow_lock->lock("FFMPEG::flow_off");
	flow = 0;
}

void FFMPEG::flow_on()
{
	if( flow ) return;
	flow = 1;
	flow_lock->unlock();
}

void FFMPEG::flow_ctl()
{
	while( !flow ) {
		flow_lock->lock("FFMPEG::flow_ctl");
		flow_lock->unlock();
	}
}

int FFMPEG::mux_audio(FFrame *frm)
{
	FFStream *fst = frm->fst;
	AVCodecContext *ctx = fst->avctx;
	AVFrame *frame = *frm;
	AVRational tick_rate = {1, ctx->sample_rate};
	frame->pts = av_rescale_q(frm->position, tick_rate, ctx->time_base);
	int ret = fst->encode_frame(frame);
	if( ret < 0 )
		ff_err(ret, "FFMPEG::mux_audio");
	return ret >= 0 ? 0 : 1;
}

int FFMPEG::mux_video(FFrame *frm)
{
	FFStream *fst = frm->fst;
	AVFrame *frame = *frm;
	frame->pts = frm->position;
	int ret = fst->encode_frame(frame);
	if( ret < 0 )
		ff_err(ret, "FFMPEG::mux_video");
	return ret >= 0 ? 0 : 1;
}

void FFMPEG::mux()
{
	for(;;) {
		double atm = -1, vtm = -1;
		FFrame *afrm = 0, *vfrm = 0;
		int demand = 0;
		for( int i=0; i<ffaudio.size(); ++i ) {  // earliest audio
			FFStream *fst = ffaudio[i];
			if( fst->frm_count < 3 ) { demand = 1; flow_on(); }
			FFrame *frm = fst->frms.first;
			if( !frm ) { if( !done ) return; continue; }
			double tm = to_secs(frm->position, fst->avctx->time_base);
			if( atm < 0 || tm < atm ) { atm = tm;  afrm = frm; }
		}
		for( int i=0; i<ffvideo.size(); ++i ) {  // earliest video
			FFStream *fst = ffvideo[i];
			if( fst->frm_count < 2 ) { demand = 1; flow_on(); }
			FFrame *frm = fst->frms.first;
			if( !frm ) { if( !done ) return; continue; }
			double tm = to_secs(frm->position, fst->avctx->time_base);
			if( vtm < 0 || tm < vtm ) { vtm = tm;  vfrm = frm; }
		}
		if( !demand ) flow_off();
		if( !afrm && !vfrm ) break;
		int v = !afrm ? -1 : !vfrm ? 1 : av_compare_ts(
			vfrm->position, vfrm->fst->avctx->time_base,
			afrm->position, afrm->fst->avctx->time_base);
		FFrame *frm = v <= 0 ? vfrm : afrm;
		if( frm == afrm ) mux_audio(frm);
		if( frm == vfrm ) mux_video(frm);
		frm->dequeue();
		delete frm;
	}
}

void FFMPEG::run()
{
	while( !done ) {
		mux_lock->lock("FFMPEG::run");
		if( !done ) mux();
	}
	for( int i=0; i<ffaudio.size(); ++i )
		ffaudio[i]->drain();
	for( int i=0; i<ffvideo.size(); ++i )
		ffvideo[i]->drain();
	mux();
	for( int i=0; i<ffaudio.size(); ++i )
		ffaudio[i]->flush();
	for( int i=0; i<ffvideo.size(); ++i )
		ffvideo[i]->flush();
}


int FFMPEG::ff_total_audio_channels()
{
	return astrm_index.size();
}

int FFMPEG::ff_total_astreams()
{
	return ffaudio.size();
}

int FFMPEG::ff_audio_channels(int stream)
{
	return ffaudio[stream]->channels;
}

int FFMPEG::ff_sample_rate(int stream)
{
	return ffaudio[stream]->sample_rate;
}

const char* FFMPEG::ff_audio_format(int stream)
{
	AVStream *st = ffaudio[stream]->st;
	AVCodecID id = st->codecpar->codec_id;
	const AVCodecDescriptor *desc = avcodec_descriptor_get(id);
	return desc ? desc->name : _("Unknown");
}

int FFMPEG::ff_audio_pid(int stream)
{
	return ffaudio[stream]->st->id;
}

int64_t FFMPEG::ff_audio_samples(int stream)
{
	return ffaudio[stream]->length;
}

// find audio astream/channels with this program,
//   or all program audio channels (astream=-1)
int FFMPEG::ff_audio_for_video(int vstream, int astream, int64_t &channel_mask)
{
	channel_mask = 0;
	int pidx = -1;
	int vidx = ffvideo[vstream]->fidx;
	// find first program with this video stream
	for( int i=0; pidx<0 && i<(int)fmt_ctx->nb_programs; ++i ) {
		AVProgram *pgrm = fmt_ctx->programs[i];
		for( int j=0;  pidx<0 && j<(int)pgrm->nb_stream_indexes; ++j ) {
			int st_idx = pgrm->stream_index[j];
			AVStream *st = fmt_ctx->streams[st_idx];
			if( st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ) continue;
			if( st_idx == vidx ) pidx = i;
		}
	}
	if( pidx < 0 ) return -1;
	int ret = -1;
	int64_t channels = 0;
	AVProgram *pgrm = fmt_ctx->programs[pidx];
	for( int j=0; j<(int)pgrm->nb_stream_indexes; ++j ) {
		int aidx = pgrm->stream_index[j];
		AVStream *st = fmt_ctx->streams[aidx];
		if( st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ) continue;
		if( astream > 0 ) { --astream;  continue; }
		int astrm = -1;
		for( int i=0; astrm<0 && i<ffaudio.size(); ++i )
			if( ffaudio[i]->fidx == aidx ) astrm = i;
		if( astrm >= 0 ) {
			if( ret < 0 ) ret = astrm;
			int64_t mask = (1 << ffaudio[astrm]->channels) - 1;
			channels |= mask << ffaudio[astrm]->channel0;
		}
		if( !astream ) break;
	}
	channel_mask = channels;
	return ret;
}


int FFMPEG::ff_total_video_layers()
{
	return vstrm_index.size();
}

int FFMPEG::ff_total_vstreams()
{
	return ffvideo.size();
}

int FFMPEG::ff_video_width(int stream)
{
	FFVideoStream *vst = ffvideo[stream];
	return !vst->transpose ? vst->width : vst->height;
}

int FFMPEG::ff_video_height(int stream)
{
	FFVideoStream *vst = ffvideo[stream];
	return !vst->transpose ? vst->height : vst->width;
}

int FFMPEG::ff_set_video_width(int stream, int width)
{
	FFVideoStream *vst = ffvideo[stream];
	int *vw = !vst->transpose ? &vst->width : &vst->height, w = *vw;
	*vw = width;
	return w;
}

int FFMPEG::ff_set_video_height(int stream, int height)
{
	FFVideoStream *vst = ffvideo[stream];
	int *vh = !vst->transpose ? &vst->height : &vst->width, h = *vh;
	*vh = height;
	return h;
}

int FFMPEG::ff_coded_width(int stream)
{
	return ffvideo[stream]->avctx->coded_width;
}

int FFMPEG::ff_coded_height(int stream)
{
	return ffvideo[stream]->avctx->coded_height;
}

float FFMPEG::ff_aspect_ratio(int stream)
{
	//return ffvideo[stream]->aspect_ratio;
	AVFormatContext *fmt_ctx = ffvideo[stream]->fmt_ctx;
	AVStream *strm = ffvideo[stream]->st;
	AVCodecParameters *par = ffvideo[stream]->st->codecpar;
	AVRational dar;
	AVRational sar = av_guess_sample_aspect_ratio(fmt_ctx, strm, NULL);
        if (sar.num && ffvideo[stream]->get_rotation_angle() == 0) {
            av_reduce(&dar.num, &dar.den,
                      par->width  * sar.num,
                      par->height * sar.den,
                      1024*1024);
                      return av_q2d(dar);
                      }
        return ffvideo[stream]->aspect_ratio;
}

const char* FFMPEG::ff_video_codec(int stream)
{
	AVStream *st = ffvideo[stream]->st;
	AVCodecID id = st->codecpar->codec_id;
	const AVCodecDescriptor *desc = avcodec_descriptor_get(id);
	return desc ? desc->name : _("Unknown");
}

int FFMPEG::ff_color_range(int stream)
{
	return ffvideo[stream]->color_range;
}

int FFMPEG::ff_color_space(int stream)
{
	return ffvideo[stream]->color_space;
}

double FFMPEG::ff_frame_rate(int stream)
{
	return ffvideo[stream]->frame_rate;
}

int64_t FFMPEG::ff_video_frames(int stream)
{
	return ffvideo[stream]->length;
}

int FFMPEG::ff_video_pid(int stream)
{
	return ffvideo[stream]->st->id;
}

int FFMPEG::ff_video_mpeg_color_range(int stream)
{
	return ffvideo[stream]->st->codecpar->color_range == AVCOL_RANGE_MPEG ? 1 : 0;
}

int FFMPEG::ff_interlace(int stream)
{
// https://ffmpeg.org/doxygen/trunk/structAVCodecParserContext.html
/* reads from demuxer because codec frame not ready */
	int interlace0 = ffvideo[stream]->st->codecpar->field_order;

	switch (interlace0)
	{
	case AV_FIELD_TT:
	case AV_FIELD_TB:
	    return ILACE_MODE_TOP_FIRST;
	case AV_FIELD_BB:
	case AV_FIELD_BT:
	    return ILACE_MODE_BOTTOM_FIRST;
	case AV_FIELD_PROGRESSIVE:
	    return ILACE_MODE_NOTINTERLACED;
	default:
	    return ILACE_MODE_UNDETECTED;
	}
	
}



int FFMPEG::ff_cpus()
{
	return !file_base ? 1 : file_base->file->cpus;
}

const char *FFMPEG::ff_hw_dev()
{
	return &file_base->file->preferences->use_hw_dev[0];
}

Preferences *FFMPEG::ff_prefs()
{
	return !file_base ? 0 : file_base->file->preferences;
}

double FFVideoStream::get_rotation_angle()
{
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
	size_t size = 0;
#else
	int size = 0;
#endif

#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(59,16,100)
	int *matrix = (int*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX, &size);
#else
	int32_t *matrix = NULL;
        if (!matrix) {
            const AVPacketSideData *psd = av_packet_side_data_get(st->codecpar->coded_side_data,
                                                                  st->codecpar->nb_coded_side_data,
                                                                  AV_PKT_DATA_DISPLAYMATRIX);
            if (psd)
                matrix = (int32_t *)psd->data;
        }
	
#endif
	int len = size/sizeof(*matrix);
	if( !matrix || len < 5 ) return 0;
	const double s = 1/65536.;
	double theta = (!matrix[0] && !matrix[3]) || (!matrix[1] && !matrix[4]) ? 0 :
		 atan2( s*matrix[1] / hypot(s*matrix[1], s*matrix[4]),
			s*matrix[0] / hypot(s*matrix[0], s*matrix[3])) * 180/M_PI;
	return theta;
}

int FFVideoStream::flip(double theta)
{
	int ret = 0;
	transpose = 0;
	Preferences *preferences = ffmpeg->ff_prefs();
	if( !preferences || !preferences->auto_rotate ) return ret;
	double tolerance = 1;
	if( fabs(theta-0) < tolerance ) return  ret;
	if( (theta=fmod(theta, 360)) < 0 ) theta += 360;
        if( fabs(theta-90) < tolerance ) {
		if( (ret = insert_filter("transpose", "clock")) < 0 )
			return ret;
		transpose = 1;
        }
	else if( fabs(theta-180) < tolerance ) {
		if( (ret=insert_filter("hflip", 0)) < 0 )
			return ret;
		if( (ret=insert_filter("vflip", 0)) < 0 )
			return ret;
        }
	else if (fabs(theta-270) < tolerance ) {
		if( (ret=insert_filter("transpose", "cclock")) < 0 )
			return ret;
		transpose = 1;
        }
	else {
		char angle[BCSTRLEN];
		sprintf(angle, "%f", theta*M_PI/180.);
		if( (ret=insert_filter("rotate", angle)) < 0 )
			return ret;
        }
	return 1;
}

int FFVideoStream::create_filter(const char *filter_spec)
{
	double theta = get_rotation_angle();
	if( !theta && !filter_spec )
		return 0;
	avfilter_register_all();
	if( filter_spec ) {
		const char *sp = filter_spec;
		char filter_name[BCSTRLEN], *np = filter_name;
		int i = sizeof(filter_name);
		while( --i>=0 && *sp!=0 && !strchr(" \t:=,",*sp) ) *np++ = *sp++;
		*np = 0;
		const AVFilter *filter = !filter_name[0] ? 0 : avfilter_get_by_name(filter_name);
		//AVFilterContext *ctx = filter->ctx;
		int       nb_pads = avfilter_filter_pad_count(filter,0);
		if( !filter || (nb_pads>1 && avfilter_pad_get_type(filter->inputs,0)) != AVMEDIA_TYPE_VIDEO ) {
			ff_err(AVERROR(EINVAL), "FFVideoStream::create_filter: %s\n", filter_spec);
			return -1;
		}
	}
	AVCodecParameters *avpar = st->codecpar;
	int sa_num = avpar->sample_aspect_ratio.num;
	if( !sa_num ) sa_num = 1;
	int sa_den = avpar->sample_aspect_ratio.den;
	if( !sa_den ) sa_num = 1;

	int ret = 0;  char args[BCTEXTLEN];
	AVPixelFormat pix_fmt = (AVPixelFormat)avpar->format;
	snprintf(args, sizeof(args),
		"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
		avpar->width, avpar->height, (int)pix_fmt,
		st->time_base.num, st->time_base.den, sa_num, sa_den);
	if( ret >= 0 ) {
		filt_ctx = 0;
		ret = insert_filter("buffer", args, "in");
		buffersrc_ctx = filt_ctx;
	}
	if( ret >= 0 )
		ret = flip(theta);
	AVFilterContext *fsrc = filt_ctx;
	if( ret >= 0 ) {
		filt_ctx = 0;
		ret = insert_filter("buffersink", 0, "out");
		buffersink_ctx = filt_ctx;
	}
	/*
	if( ret >= 0 ) {
	ret = av_opt_set(buffersink_ctx, "pixel_formats", av_get_pix_fmt_name(pix_fmt),
                     AV_OPT_SEARCH_CHILDREN);
	}
	
	if( ret >= 0 ) {
		ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
			(uint8_t*)&pix_fmt, sizeof(pix_fmt),
			AV_OPT_SEARCH_CHILDREN);
	}
	*/
	
	if( ret >= 0 )
		ret = config_filters(filter_spec, fsrc);
	else
		ff_err(ret, "FFVideoStream::create_filter");
	return ret >= 0 ? 0 : -1;
}

int FFAudioStream::create_filter(const char *filter_spec)
{
	if( !filter_spec )
		return 0;
	avfilter_register_all();
	if( filter_spec ) {
		const char *sp = filter_spec;
		char filter_name[BCSTRLEN], *np = filter_name;
		int i = sizeof(filter_name);
		while( --i>=0 && *sp!=0 && !strchr(" \t:=,",*sp) ) *np++ = *sp++;
		*np = 0;
		const AVFilter *filter = !filter_name[0] ? 0 : avfilter_get_by_name(filter_name);
		int       nb_pads = avfilter_filter_pad_count(filter,0);
		if( !filter || (nb_pads >1 &&  avfilter_pad_get_type(filter->inputs,0)) != AVMEDIA_TYPE_AUDIO ) {
			ff_err(AVERROR(EINVAL), "FFAudioStream::create_filter: %s\n", filter_spec);
			return -1;
		}
	}
	int ret = 0;  char args[BCTEXTLEN];
	AVCodecParameters *avpar = st->codecpar;
	AVSampleFormat sample_fmt = (AVSampleFormat)avpar->format;
	snprintf(args, sizeof(args),
		"time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%jx",
		st->time_base.num, st->time_base.den, avpar->sample_rate,
		av_get_sample_fmt_name(sample_fmt), avpar->ch_layout.u.mask);
	if( ret >= 0 ) {
		filt_ctx = 0;
		ret = insert_filter("abuffer", args, "in");
		buffersrc_ctx = filt_ctx;
	}
	AVFilterContext *fsrc = filt_ctx;
	if( ret >= 0 ) {
		filt_ctx = 0;
		ret = insert_filter("abuffersink", 0, "out");
		buffersink_ctx = filt_ctx;
	}
	if( ret >= 0 )
		ret = av_opt_set_bin(buffersink_ctx, "sample_fmts",
			(uint8_t*)&sample_fmt, sizeof(sample_fmt),
			AV_OPT_SEARCH_CHILDREN);
	if( ret >= 0 )
		ret = av_opt_set_bin(buffersink_ctx, "channel_layouts",
			(uint8_t*)&avpar->ch_layout.u.mask,
			sizeof(avpar->ch_layout.u.mask), AV_OPT_SEARCH_CHILDREN);
	if( ret >= 0 )
		ret = av_opt_set_bin(buffersink_ctx, "sample_rates",
			(uint8_t*)&sample_rate, sizeof(sample_rate),
			AV_OPT_SEARCH_CHILDREN);
	if( ret >= 0 )
		ret = config_filters(filter_spec, fsrc);
	else
		ff_err(ret, "FFAudioStream::create_filter");
	return ret >= 0 ? 0 : -1;
}

int FFStream::insert_filter(const char *name, const char *arg, const char *inst_name)
{
	const AVFilter *filter = avfilter_get_by_name(name);
	if( !filter ) return -1;
	char filt_inst[BCSTRLEN];
	if( !inst_name ) {
		snprintf(filt_inst, sizeof(filt_inst), "%s_%d", name, ++filt_id);
		inst_name = filt_inst;
	}
	if( !filter_graph )
		filter_graph = avfilter_graph_alloc();
	AVFilterContext *fctx = 0;
	int ret = avfilter_graph_create_filter(&fctx,
		filter, inst_name, arg, NULL, filter_graph);
	if( ret >= 0 && filt_ctx )
		ret = avfilter_link(filt_ctx, 0, fctx, 0);
	if( ret >= 0 )
		filt_ctx = fctx;
	else
		avfilter_free(fctx);
	return ret;
}

int FFStream::config_filters(const char *filter_spec, AVFilterContext *fsrc)
{
	int ret = 0;
	AVFilterContext *fsink = buffersink_ctx;
	if( filter_spec ) {
		/* Endpoints for the filter graph. */
		AVFilterInOut *outputs = avfilter_inout_alloc();
		AVFilterInOut *inputs = avfilter_inout_alloc();
		if( !inputs || !outputs ) ret = -1;
		if( ret >= 0 ) {
			outputs->filter_ctx = fsrc;
			outputs->pad_idx = 0;
			outputs->next = 0;
			if( !(outputs->name = av_strdup(fsrc->name)) ) ret = -1;
		}
		if( ret >= 0 ) {
			inputs->filter_ctx = fsink;
			inputs->pad_idx	= 0;
			inputs->next = 0;
			if( !(inputs->name = av_strdup(fsink->name)) ) ret = -1;
		}
		if( ret >= 0 ) {
			int len = strlen(fsrc->name)+2 + strlen(filter_spec) + 1;
			char spec[len];  sprintf(spec, "[%s]%s", fsrc->name, filter_spec);
			ret = avfilter_graph_parse_ptr(filter_graph, spec,
				&inputs, &outputs, NULL);
		}
		avfilter_inout_free(&inputs);
		avfilter_inout_free(&outputs);
	}
	else
		ret = avfilter_link(fsrc, 0, fsink, 0);
	if( ret >= 0 )
		ret = avfilter_graph_config(filter_graph, NULL);
	if( ret < 0 ) {
		ff_err(ret, "FFStream::create_filter");
		avfilter_graph_free(&filter_graph);
		filter_graph = 0;
	}
	return ret;
}


AVCodecContext *FFMPEG::activate_decoder(AVStream *st)
{
	AVDictionary *copts = 0;
	av_dict_copy(&copts, opts, 0);
	AVCodecID codec_id = st->codecpar->codec_id;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
	const AVCodec *decoder = 0;
#else
	AVCodec *decoder = 0;
#endif
	switch( st->codecpar->codec_type ) {
	case AVMEDIA_TYPE_VIDEO:
		if( opt_video_decoder )
			decoder = avcodec_find_decoder_by_name(opt_video_decoder);
		else
			video_codec_remaps.update(codec_id, decoder);
		break;
	case AVMEDIA_TYPE_AUDIO:
		if( opt_audio_decoder )
			decoder = avcodec_find_decoder_by_name(opt_audio_decoder);
		else
			audio_codec_remaps.update(codec_id, decoder);
		break;
	default:
		return 0;
	}
	if( !decoder && !(decoder = avcodec_find_decoder(codec_id)) ) {
		eprintf(_("cant find decoder codec %d\n"), (int)codec_id);
		return 0;
	}
	AVCodecContext *avctx = avcodec_alloc_context3(decoder);
	if( !avctx ) {
		eprintf(_("cant allocate codec context\n"));
		return 0;
	}
	avcodec_parameters_to_context(avctx, st->codecpar);
	if( !av_dict_get(copts, "threads", NULL, 0) )
		avctx->thread_count = ff_cpus();
	int ret = avcodec_open2(avctx, decoder, &copts);
	av_dict_free(&copts);
	if( ret < 0 ) {
		avcodec_free_context(&avctx);
		avctx = 0;
	}
	return avctx;
}

int FFMPEG::scan(IndexState *index_state, int64_t *scan_position, int *canceled)
{
	AVPacket pkt;
	av_init_packet(&pkt);
	AVFrame *frame = av_frame_alloc();
	if( !frame ) {
		fprintf(stderr,"FFMPEG::scan: ");
		fprintf(stderr,_("av_frame_alloc failed\n"));
		fprintf(stderr,"FFMPEG::scan:file=%s\n", file_base->asset->path);
		return -1;
	}

	index_state->add_video_markers(ffvideo.size());
	index_state->add_audio_markers(ffaudio.size());

	for( int i=0; i<(int)fmt_ctx->nb_streams; ++i ) {
		AVStream *st = fmt_ctx->streams[i];
		AVCodecContext *avctx = activate_decoder(st);
		if( avctx ) {
			AVCodecParameters *avpar = st->codecpar;
			switch( avpar->codec_type ) {
			case AVMEDIA_TYPE_VIDEO: {
				int vidx = ffvideo.size();
				while( --vidx>=0 && ffvideo[vidx]->fidx != i );
				if( vidx < 0 ) break;
				ffvideo[vidx]->avctx = avctx;
				continue; }
			case AVMEDIA_TYPE_AUDIO: {
				int aidx = ffaudio.size();
				while( --aidx>=0 && ffaudio[aidx]->fidx != i );
				if( aidx < 0 ) break;
				ffaudio[aidx]->avctx = avctx;
				continue; }
			default: break;
			}
		}
		fprintf(stderr,"FFMPEG::scan: ");
		fprintf(stderr,_("codec open failed\n"));
		fprintf(stderr,"FFMPEG::scan:file=%s\n", file_base->asset->path);
		avcodec_free_context(&avctx);
	}

	decode_activate();
	for( int i=0; i<(int)fmt_ctx->nb_streams; ++i ) {
		AVStream *st = fmt_ctx->streams[i];
		AVCodecParameters *avpar = st->codecpar;
		if( avpar->codec_type != AVMEDIA_TYPE_AUDIO ) continue;
		int64_t tstmp = st->start_time;
		if( tstmp == AV_NOPTS_VALUE ) continue;
		int aidx = ffaudio.size();
		while( --aidx>=0 && ffaudio[aidx]->fidx != i );
		if( aidx < 0 ) continue;
		FFAudioStream *aud = ffaudio[aidx];
		tstmp -= aud->nudge;
		double secs = to_secs(tstmp, st->time_base);
		aud->curr_pos = secs * aud->sample_rate + 0.5;
	}

	int errs = 0;
	for( int64_t count=0; !*canceled; ++count ) {
		av_packet_unref(&pkt);
		pkt.data = 0; pkt.size = 0;

		int ret = av_read_frame(fmt_ctx, &pkt);
		if( ret < 0 ) {
			if( ret == AVERROR_EOF ) break;
			if( ++errs > 100 ) {
				ff_err(ret,_("over 100 read_frame errs\n"));
				break;
			}
			continue;
		}
		if( !pkt.data ) continue;
		int i = pkt.stream_index;
		if( i < 0 || i >= (int)fmt_ctx->nb_streams ) continue;
		AVStream *st = fmt_ctx->streams[i];
		if( pkt.pos > *scan_position ) *scan_position = pkt.pos;

		AVCodecParameters *avpar = st->codecpar;
		switch( avpar->codec_type ) {
		case AVMEDIA_TYPE_VIDEO: {
			int vidx = ffvideo.size();
			while( --vidx>=0 && ffvideo[vidx]->fidx != i );
			if( vidx < 0 ) break;
			FFVideoStream *vid = ffvideo[vidx];
			if( !vid->avctx ) break;
			int64_t tstmp = pkt.pts;
			if( tstmp == AV_NOPTS_VALUE ) tstmp = pkt.dts;
			if( tstmp != AV_NOPTS_VALUE && (pkt.flags & AV_PKT_FLAG_KEY) && pkt.pos > 0 ) {
				if( vid->nudge != AV_NOPTS_VALUE ) tstmp -= vid->nudge;
				double secs = to_secs(tstmp, st->time_base);
				int64_t frm = secs * vid->frame_rate + 0.5;
				if( frm < 0 ) frm = 0;
				index_state->put_video_mark(vidx, frm, pkt.pos);
			}
#if 0
			ret = avcodec_send_packet(vid->avctx, pkt);
			if( ret < 0 ) break;
			while( (ret=vid->decode_frame(frame)) > 0 ) {}
#endif
			break; }
		case AVMEDIA_TYPE_AUDIO: {
			int aidx = ffaudio.size();
			while( --aidx>=0 && ffaudio[aidx]->fidx != i );
			if( aidx < 0 ) break;
			FFAudioStream *aud = ffaudio[aidx];
			if( !aud->avctx ) break;
			int64_t tstmp = pkt.pts;
			if( tstmp == AV_NOPTS_VALUE ) tstmp = pkt.dts;
			if( tstmp != AV_NOPTS_VALUE && (pkt.flags & AV_PKT_FLAG_KEY) && pkt.pos > 0 ) {
				if( aud->nudge != AV_NOPTS_VALUE ) tstmp -= aud->nudge;
				double secs = to_secs(tstmp, st->time_base);
				int64_t sample = secs * aud->sample_rate + 0.5;
				if( sample >= 0 )
					index_state->put_audio_mark(aidx, sample, pkt.pos);
			}
			ret = avcodec_send_packet(aud->avctx, &pkt);
			if( ret < 0 ) break;
			int ch = aud->channel0,  nch = aud->channels;
			int64_t pos = index_state->pos(ch);
			if( pos != aud->curr_pos ) {
if( abs(pos-aud->curr_pos) > 1 )
printf("audio%d pad %jd %jd (%jd)\n", aud->idx, pos, aud->curr_pos, pos-aud->curr_pos);
				index_state->pad_data(ch, nch, aud->curr_pos);
			}
			while( (ret=aud->decode_frame(frame)) > 0 ) {
				//if( frame->channels != nch ) break;
				aud->init_swr(frame->ch_layout.nb_channels, frame->format, frame->sample_rate);
				float *samples;
				int len = aud->get_samples(samples,
					 &frame->extended_data[0], frame->nb_samples);
				pos = aud->curr_pos;
				if( (aud->curr_pos += len) >= 0 ) {
					if( pos < 0 ) {
						samples += -pos * nch;
						len = aud->curr_pos;
					}
					for( int i=0; i<nch; ++i )
						index_state->put_data(ch+i,nch,samples+i,len);
				}
			}
			break; }
		default: break;
		}
	}
	av_frame_free(&frame);
	return 0;
}

void FFStream::load_markers(IndexMarks &marks, double rate)
{
	int in = 0;
	int64_t sz = marks.size();
	int max_entries = fmt_ctx->max_index_size / sizeof(AVIndexEntry) - 1;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
	int nb_ent = avformat_index_get_entries_count(st);
#endif
#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100)
	int nb_ent = st->nb_index_entries;
#endif
// some formats already have an index
	if( nb_ent > 0 ) {
#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100)
		AVIndexEntry *ep = &st->index_entries[nb_ent-1];
#endif
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
		const AVIndexEntry *ep = avformat_index_get_entry(st, nb_ent-1);
#endif
		int64_t tstmp = ep->timestamp;
		if( nudge != AV_NOPTS_VALUE ) tstmp -= nudge;
		double secs = ffmpeg->to_secs(tstmp, st->time_base);
		int64_t no = secs * rate;
		while( in < sz && marks[in].no <= no ) ++in;
	}
	int64_t len = sz - in;
	int64_t count = max_entries - nb_ent;
	if( count > len ) count = len;
	for( int i=0; i<count; ++i ) {
		int k = in + i * len / count;
		int64_t no = marks[k].no, pos = marks[k].pos;
		double secs = (double)no / rate;
		int64_t tstmp = secs * st->time_base.den / st->time_base.num;
		if( nudge != AV_NOPTS_VALUE ) tstmp += nudge;
		av_add_index_entry(st, pos, tstmp, 0, 0, AVINDEX_KEYFRAME);
	}
}


/*
 * 1) if the format context has a timecode
 *   return fmt_ctx->timecode - 0
 * 2) if the layer/channel has a timecode
 *   return st->timecode - (start_time-nudge)
 * 3) find the 1st program with stream, find 1st program video stream,
 *   if video stream has a timecode, return st->timecode - (start_time-nudge)
 * 4) find timecode in any stream, return st->timecode
 * 5) read 100 packets, save ofs=pkt.pts*st->time_base - st->nudge:
 *   decode frame for video stream of 1st program
 *   if frame->timecode has a timecode, return frame->timecode - ofs
 *   if side_data has gop timecode, return gop->timecode - ofs
 *   if side_data has smpte timecode, return smpte->timecode - ofs
 * 6) if the filename/url scans *date_time.ext, return date_time
 * 7) if stat works on the filename/url, return mtime
 * 8) return -1 failure
*/
double FFMPEG::get_initial_timecode(int data_type, int channel, double frame_rate)
{
	AVRational rate = check_frame_rate(0, frame_rate);
	if( !rate.num ) return -1;
// format context timecode
	AVDictionaryEntry *tc = av_dict_get(fmt_ctx->metadata, "timecode", 0, 0);
	if( tc ) return ff_get_timecode(tc->value, rate, 0);
// stream timecode
	if( open_decoder() ) return -1;
	AVStream *st = 0;
	int64_t nudge = 0;
	int codec_type = -1, fidx = -1;
	switch( data_type ) {
	case TRACK_AUDIO: {
		codec_type = AVMEDIA_TYPE_AUDIO;
		int aidx = astrm_index[channel].st_idx;
		FFAudioStream *aud = ffaudio[aidx];
		fidx = aud->fidx;
		nudge = aud->nudge;
		st = aud->st;
		AVDictionaryEntry *tref = av_dict_get(fmt_ctx->metadata, "time_reference", 0, 0);
		if( tref && aud && aud->sample_rate )
			return strtod(tref->value, 0) / aud->sample_rate;
		break; }
	case TRACK_VIDEO: {
		codec_type = AVMEDIA_TYPE_VIDEO;
		int vidx = vstrm_index[channel].st_idx;
		FFVideoStream *vid = ffvideo[vidx];
		fidx = vid->fidx;
		nudge = vid->nudge;
		st = vid->st;
		break; }
	}
	if( codec_type < 0 ) return -1;
	if( st )
		tc = av_dict_get(st->metadata, "timecode", 0, 0);
	if( !tc ) {
		st = 0;
// find first program which references this stream
		int pidx = -1;
		for( int i=0, m=fmt_ctx->nb_programs; pidx<0 && i<m; ++i ) {
			AVProgram *pgrm = fmt_ctx->programs[i];
			for( int j=0, n=pgrm->nb_stream_indexes; j<n; ++j ) {
				int st_idx = pgrm->stream_index[j];
				if( st_idx == fidx ) { pidx = i;  break; }
			}
		}
		fidx = -1;
		if( pidx >= 0 ) {
			AVProgram *pgrm = fmt_ctx->programs[pidx];
			for( int j=0, n=pgrm->nb_stream_indexes; j<n; ++j ) {
				int st_idx = pgrm->stream_index[j];
				AVStream *tst = fmt_ctx->streams[st_idx];
				if( !tst ) continue;
				if( tst->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ) {
					st = tst;  fidx = st_idx;
					break;
				}
			}
		}
		else {
			for( int i=0, n=fmt_ctx->nb_streams; i<n; ++i ) {
				AVStream *tst = fmt_ctx->streams[i];
				if( !tst ) continue;
				if( tst->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ) {
					st = tst;  fidx = i;
					break;
				}
			}
		}
		if( st )
			tc = av_dict_get(st->metadata, "timecode", 0, 0);
	}

	if( !tc ) {
		// any timecode, includes -data- streams
		for( int i=0, n=fmt_ctx->nb_streams; i<n; ++i ) {
			AVStream *tst = fmt_ctx->streams[i];
			if( !tst ) continue;
			if( (tc = av_dict_get(tst->metadata, "timecode", 0, 0)) ) {
				st = tst;  fidx = i;
				break;
			}
		}
	}

	if( st && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ) {
		if( st->r_frame_rate.num && st->r_frame_rate.den )
			rate = st->r_frame_rate;
		nudge = st->start_time;
		for( int i=0; i<ffvideo.size(); ++i ) {
			if( ffvideo[i]->st == st ) {
				nudge = ffvideo[i]->nudge;
				break;
			}
		}
	}

	if( tc ) { // return timecode
		double secs = st->start_time == AV_NOPTS_VALUE ? 0 :
			to_secs(st->start_time - nudge, st->time_base);
		return ff_get_timecode(tc->value, rate, secs);
	}
	
	if( !st || fidx < 0 ) return -1;

	decode_activate();
	AVCodecContext *av_ctx = activate_decoder(st);
	if( !av_ctx ) {
		fprintf(stderr,"activate_decoder failed\n");
		return -1;
	}
	avCodecContext avctx(av_ctx); // auto deletes
	if( avctx->codec_type == AVMEDIA_TYPE_VIDEO &&
	    avctx->framerate.num && avctx->framerate.den )
		rate = avctx->framerate;

	avPacket pkt;	// auto deletes
	avFrame frame;  // auto deletes
	if( !frame ) {
		fprintf(stderr,"av_frame_alloc failed\n");
		return -1;
	}
	int errs = 0;
	int64_t max_packets = 100;
	char tcbuf[AV_TIMECODE_STR_SIZE];

	for( int64_t count=0; count<max_packets; ++count ) {
		av_packet_unref(pkt);
		pkt->data = 0; pkt->size = 0;

		int ret = av_read_frame(fmt_ctx, pkt);
		if( ret < 0 ) {
			if( ret == AVERROR_EOF ) break;
			if( ++errs > 100 ) {
				fprintf(stderr,"over 100 read_frame errs\n");
				break;
			}
			continue;
		}
		if( !pkt->data ) continue;
		int i = pkt->stream_index;
		if( i != fidx ) continue;
		int64_t tstmp = pkt->pts;
		if( tstmp == AV_NOPTS_VALUE ) tstmp = pkt->dts;
		double secs = to_secs(tstmp - nudge, st->time_base);
		ret = avcodec_send_packet(avctx, pkt);
		if( ret < 0 ) return -1;

		while( (ret = avcodec_receive_frame(avctx, frame)) >= 0 ) {
			if( (tc = av_dict_get(frame->metadata, "timecode", 0, 0)) )
				return ff_get_timecode(tc->value, rate, secs);
			int k = frame->nb_side_data;
			AVFrameSideData *side_data = 0;
			while( --k >= 0 ) {
				side_data = frame->side_data[k];
				switch( side_data->type ) {
				case AV_FRAME_DATA_GOP_TIMECODE: {
					int64_t data = *(int64_t *)side_data->data;
					int sz = sizeof(data);
					if( side_data->size >= sz ) {
						av_timecode_make_mpeg_tc_string(tcbuf, data);
						return ff_get_timecode(tcbuf, rate, secs);
					}
					break; }
				case AV_FRAME_DATA_S12M_TIMECODE: {
					uint32_t *data = (uint32_t *)side_data->data;
					int n = data[0], sz = (n+1)*sizeof(*data);
					if( side_data->size >= sz ) {
						av_timecode_make_smpte_tc_string(tcbuf, data[n], 0);
						return ff_get_timecode(tcbuf, rate, secs);
					}
					break; }
				default:
					break;
				}
			}
		}
	}
	char *path = fmt_ctx->url;
	char *bp = strrchr(path, '/');
	if( !bp ) bp = path; else ++bp;
	char *cp = strrchr(bp, '.');
	if( cp && (cp-=(8+1+6)) >= bp ) {
		char sep[BCSTRLEN];
		int year,mon,day, hour,min,sec, frm=0;
		if( sscanf(cp,"%4d%2d%2d%[_-]%2d%2d%2d",
				&year,&mon,&day, sep, &hour,&min,&sec) == 7 ) {
			int ch = sep[0];
			// year>=1970,mon=1..12,day=1..31, hour=0..23,min=0..59,sec=0..60
			if( (ch=='_' || ch=='-' ) &&
			    year >= 1970 && mon>=1 && mon<=12 && day>=1 && day<=31 &&
			    hour>=0 && hour<24 && min>=0 && min<60 && sec>=0 && sec<=60 ) {
				sprintf(tcbuf,"%d:%02d:%02d:%02d", hour,min,sec, frm);
				return ff_get_timecode(tcbuf, rate, 0);
			}
		}
	}
	struct stat tst;
	if( stat(path, &tst) >= 0 ) {
		time_t t = (time_t)tst.st_mtim.tv_sec;
		struct tm tm;
		localtime_r(&t, &tm);
		int64_t us = tst.st_mtim.tv_nsec / 1000;
		int frm = us/1000000. * frame_rate;
		sprintf(tcbuf,"%d:%02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec, frm);
		return ff_get_timecode(tcbuf, rate, 0);
	}
	return -1;
}

double FFMPEG::ff_get_timecode(char *str, AVRational rate, double pos)
{
	AVTimecode tc;
	if( av_timecode_init_from_string(&tc, rate, str, fmt_ctx) )
		return -1;
	double secs = (double)tc.start / tc.fps - pos;
	if( secs < 0 ) secs = 0;
	return secs;
}

double FFMPEG::get_timecode(const char *path, int data_type, int channel, double rate)
{
	FFMPEG ffmpeg(0);
	if( ffmpeg.init_decoder(path) ) return -1;
	return ffmpeg.get_initial_timecode(data_type, channel, rate);
}

