Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement looping, demuxer options #420

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ffmpeg/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ int open_input(input_params *params, struct input_ctx *ctx)
int ret = 0;

ctx->transmuxing = params->transmuxing;
ctx->loop = params->loop;

// open demuxer/ open demuxer
AVDictionary **demuxer_opts = NULL;
Expand Down
2 changes: 2 additions & 0 deletions ffmpeg/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ struct input_ctx {
// Transmuxing mode. Close output in lpms_transcode_stop instead of
// at the end of lpms_transcode call.
int transmuxing;
// How many times should the input be looped? -1 for forever.
int loop;
// In HW transcoding, demuxer is opened once and used,
// so it is necessary to check whether the input pixel format does not change in the middle.
enum AVPixelFormat last_format;
Expand Down
25 changes: 21 additions & 4 deletions ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ type TranscodeOptionsIn struct {
Device string
Transmuxing bool
Profile VideoProfile
Demuxer ComponentOptions
Loop int
}

type TranscodeOptions struct {
Expand Down Expand Up @@ -944,8 +946,16 @@ func (t *Transcoder) Transcode(input *TranscodeOptionsIn, ps []TranscodeOptions)
var demuxerOpts C.component_opts

ext := filepath.Ext(input.Fname)
// If the input has an image file extension setup the image2 demuxer
if ext == ".png" {
if input.Demuxer.Name != "" {
// If we have a specified demuxer, use that
demuxerName := C.CString(input.Demuxer.Name)
defer C.free(unsafe.Pointer(demuxerName))
demuxerOpts = C.component_opts{
name: demuxerName,
opts: newAVOpts(input.Demuxer.Opts),
}
} else if ext == ".png" {
// If the input has an image file extension setup the image2 demuxer
image2 := C.CString("image2")
defer C.free(unsafe.Pointer(image2))

Expand All @@ -966,8 +976,15 @@ func (t *Transcoder) Transcode(input *TranscodeOptionsIn, ps []TranscodeOptions)
}
}

inp := &C.input_params{fname: fname, hw_type: hw_type, device: device, xcoderParams: xcoderParams,
handle: t.handle, demuxer: demuxerOpts}
inp := &C.input_params{
fname: fname,
hw_type: hw_type,
device: device,
xcoderParams: xcoderParams,
handle: t.handle,
demuxer: demuxerOpts,
loop: C.int(input.Loop),
}
if input.Transmuxing {
inp.transmuxing = 1
}
Expand Down
13 changes: 13 additions & 0 deletions ffmpeg/transcoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,19 @@ int transcode(struct transcode_thread *h,
av_frame_unref(dframe);
ret = process_in(ictx, dframe, ipkt, &stream_index);
if (ret == AVERROR_EOF) {
// if we're to loop, mark discontinuity and seek back to 0
if (ictx->loop != 0) {
lpms_transcode_discontinuity(h);
ret = avformat_seek_file(ictx->ic, -1, INT64_MIN, 0, INT64_MAX, 0);
if (ret < 0) {
LPMS_WARN("loop failed");
return ret;
}
if (ictx->loop > 0) {
ictx->loop--;
}
continue;
}
// no more processing, go for flushes
break;
}
Expand Down
2 changes: 2 additions & 0 deletions ffmpeg/transcoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ typedef struct {

// concatenates multiple inputs into the same output
int transmuxing;
// loops input n times. -1 for forever
int loop;
} input_params;

#define MAX_CLASSIFY_SIZE 10
Expand Down