2017-06-23 05:58:32 +08:00
|
|
|
#include "zm_ffmpeg_input.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
|
2017-06-27 04:55:49 +08:00
|
|
|
#include "zm_ffmpeg.h"
|
2021-02-04 11:47:28 +08:00
|
|
|
#include "zm_logger.h"
|
2017-06-23 05:58:32 +08:00
|
|
|
|
2017-06-27 04:55:49 +08:00
|
|
|
FFmpeg_Input::FFmpeg_Input() {
|
2020-08-26 07:45:48 +08:00
|
|
|
input_format_context = nullptr;
|
2017-06-23 05:58:32 +08:00
|
|
|
video_stream_id = -1;
|
|
|
|
audio_stream_id = -1;
|
2019-07-12 05:57:11 +08:00
|
|
|
FFMPEGInit();
|
2020-08-26 07:45:48 +08:00
|
|
|
streams = nullptr;
|
|
|
|
frame = nullptr;
|
2019-12-14 07:12:33 +08:00
|
|
|
last_seek_request = -1;
|
2017-06-23 05:58:32 +08:00
|
|
|
}
|
2017-11-19 05:00:10 +08:00
|
|
|
|
2017-06-27 04:55:49 +08:00
|
|
|
FFmpeg_Input::~FFmpeg_Input() {
|
2017-11-21 04:32:40 +08:00
|
|
|
if ( input_format_context ) {
|
|
|
|
Close();
|
|
|
|
}
|
2020-04-27 06:19:30 +08:00
|
|
|
if ( frame ) {
|
|
|
|
av_frame_free(&frame);
|
2020-08-26 07:45:48 +08:00
|
|
|
frame = nullptr;
|
2020-04-27 06:19:30 +08:00
|
|
|
}
|
|
|
|
} // end ~FFmpeg_Input()
|
2017-06-23 05:58:32 +08:00
|
|
|
|
2021-01-27 07:29:34 +08:00
|
|
|
/* Takes streams provided from elsewhere. They might not come from the same source
|
|
|
|
* but we will treat them as if they are. */
|
2020-07-22 05:38:32 +08:00
|
|
|
int FFmpeg_Input::Open(
|
|
|
|
const AVStream * video_in_stream,
|
2021-01-27 07:29:34 +08:00
|
|
|
const AVCodecContext * video_in_ctx,
|
|
|
|
const AVStream * audio_in_stream,
|
|
|
|
const AVCodecContext * audio_in_ctx
|
2020-07-22 05:38:32 +08:00
|
|
|
) {
|
2020-05-03 06:03:42 +08:00
|
|
|
video_stream_id = video_in_stream->index;
|
|
|
|
int max_stream_index = video_in_stream->index;
|
|
|
|
|
|
|
|
if ( audio_in_stream ) {
|
|
|
|
max_stream_index = video_in_stream->index > audio_in_stream->index ? video_in_stream->index : audio_in_stream->index;
|
|
|
|
audio_stream_id = audio_in_stream->index;
|
|
|
|
}
|
2021-01-27 07:29:34 +08:00
|
|
|
streams = new stream[max_stream_index+1];
|
|
|
|
return 1;
|
2020-05-03 06:03:42 +08:00
|
|
|
}
|
|
|
|
|
2019-06-26 02:11:59 +08:00
|
|
|
int FFmpeg_Input::Open(const char *filepath) {
|
2017-06-23 05:58:32 +08:00
|
|
|
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/** Open the input file to read from it. */
|
2020-08-26 07:45:48 +08:00
|
|
|
error = avformat_open_input(&input_format_context, filepath, nullptr, nullptr);
|
2019-06-26 02:11:59 +08:00
|
|
|
if ( error < 0 ) {
|
2018-04-14 23:03:08 +08:00
|
|
|
Error("Could not open input file '%s' (error '%s')",
|
|
|
|
filepath, av_make_error_string(error).c_str());
|
2020-08-26 07:45:48 +08:00
|
|
|
input_format_context = nullptr;
|
2017-06-23 05:58:32 +08:00
|
|
|
return error;
|
2019-06-26 02:11:59 +08:00
|
|
|
}
|
2017-06-23 05:58:32 +08:00
|
|
|
|
|
|
|
/** Get information on the input file (number of streams etc.). */
|
2020-08-26 07:45:48 +08:00
|
|
|
if ( (error = avformat_find_stream_info(input_format_context, nullptr)) < 0 ) {
|
2018-11-20 05:45:56 +08:00
|
|
|
Error(
|
|
|
|
"Could not open find stream info (error '%s')",
|
|
|
|
av_make_error_string(error).c_str()
|
|
|
|
);
|
2017-06-23 05:58:32 +08:00
|
|
|
avformat_close_input(&input_format_context);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-11-19 05:00:10 +08:00
|
|
|
streams = new stream[input_format_context->nb_streams];
|
2019-06-26 02:11:59 +08:00
|
|
|
Debug(2, "Have %d streams", input_format_context->nb_streams);
|
2017-11-19 05:00:10 +08:00
|
|
|
|
2017-06-23 05:58:32 +08:00
|
|
|
for ( unsigned int i = 0; i < input_format_context->nb_streams; i += 1 ) {
|
2019-06-26 02:11:59 +08:00
|
|
|
if ( is_video_stream(input_format_context->streams[i]) ) {
|
2017-08-24 03:05:44 +08:00
|
|
|
zm_dump_stream_format(input_format_context, i, 0, 0);
|
2017-06-23 05:58:32 +08:00
|
|
|
if ( video_stream_id == -1 ) {
|
|
|
|
video_stream_id = i;
|
|
|
|
// if we break, then we won't find the audio stream
|
|
|
|
} else {
|
2018-04-14 23:03:08 +08:00
|
|
|
Warning("Have another video stream.");
|
2017-06-23 05:58:32 +08:00
|
|
|
}
|
2018-04-14 23:03:08 +08:00
|
|
|
} else if ( is_audio_stream(input_format_context->streams[i]) ) {
|
2017-06-23 05:58:32 +08:00
|
|
|
if ( audio_stream_id == -1 ) {
|
2019-06-26 02:11:59 +08:00
|
|
|
Debug(2, "Audio stream is %d", i);
|
2017-06-23 05:58:32 +08:00
|
|
|
audio_stream_id = i;
|
|
|
|
} else {
|
2018-04-14 23:03:08 +08:00
|
|
|
Warning("Have another audio stream.");
|
2017-06-23 05:58:32 +08:00
|
|
|
}
|
2018-11-20 05:45:56 +08:00
|
|
|
} else {
|
|
|
|
Warning("Unknown stream type");
|
2017-06-23 05:58:32 +08:00
|
|
|
}
|
|
|
|
|
2017-08-24 21:53:40 +08:00
|
|
|
streams[i].frame_count = 0;
|
2017-06-23 05:58:32 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
|
2020-08-26 07:45:48 +08:00
|
|
|
streams[i].context = avcodec_alloc_context3(nullptr);
|
2018-04-14 23:03:08 +08:00
|
|
|
avcodec_parameters_to_context(streams[i].context, input_format_context->streams[i]->codecpar);
|
2017-06-23 05:58:32 +08:00
|
|
|
#else
|
|
|
|
streams[i].context = input_format_context->streams[i]->codec;
|
|
|
|
#endif
|
|
|
|
|
2017-08-24 21:53:40 +08:00
|
|
|
if ( !(streams[i].codec = avcodec_find_decoder(streams[i].context->codec_id)) ) {
|
2019-06-26 02:11:59 +08:00
|
|
|
Error("Could not find input codec");
|
2017-06-27 04:55:49 +08:00
|
|
|
avformat_close_input(&input_format_context);
|
2017-06-23 05:58:32 +08:00
|
|
|
return AVERROR_EXIT;
|
2017-08-24 03:05:44 +08:00
|
|
|
} else {
|
2018-04-14 23:03:08 +08:00
|
|
|
Debug(1, "Using codec (%s) for stream %d", streams[i].codec->name, i);
|
2017-06-23 05:58:32 +08:00
|
|
|
}
|
|
|
|
|
2020-08-26 07:45:48 +08:00
|
|
|
error = avcodec_open2(streams[i].context, streams[i].codec, nullptr);
|
2019-06-26 02:11:59 +08:00
|
|
|
if ( error < 0 ) {
|
|
|
|
Error("Could not open input codec (error '%s')",
|
2018-04-14 23:03:08 +08:00
|
|
|
av_make_error_string(error).c_str());
|
2017-08-25 00:00:48 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
|
2018-04-14 23:03:08 +08:00
|
|
|
avcodec_free_context(&streams[i].context);
|
2017-08-25 00:00:48 +08:00
|
|
|
#endif
|
2017-06-27 04:55:49 +08:00
|
|
|
avformat_close_input(&input_format_context);
|
2020-08-26 07:45:48 +08:00
|
|
|
input_format_context = nullptr;
|
2017-06-23 05:58:32 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
} // end foreach stream
|
2017-08-24 03:05:44 +08:00
|
|
|
|
2017-06-23 05:58:32 +08:00
|
|
|
if ( video_stream_id == -1 )
|
2021-04-12 01:29:24 +08:00
|
|
|
Debug(1, "Unable to locate video stream in %s", filepath);
|
2017-06-23 05:58:32 +08:00
|
|
|
if ( audio_stream_id == -1 )
|
2018-04-14 23:03:08 +08:00
|
|
|
Debug(3, "Unable to locate audio stream in %s", filepath);
|
2017-06-23 05:58:32 +08:00
|
|
|
|
2021-03-03 22:54:24 +08:00
|
|
|
return 1;
|
2017-08-24 03:05:44 +08:00
|
|
|
} // end int FFmpeg_Input::Open( const char * filepath )
|
|
|
|
|
2017-11-21 04:32:40 +08:00
|
|
|
int FFmpeg_Input::Close( ) {
|
2020-12-17 23:16:54 +08:00
|
|
|
if ( streams ) {
|
|
|
|
for ( unsigned int i = 0; i < input_format_context->nb_streams; i += 1 ) {
|
2018-04-14 23:03:08 +08:00
|
|
|
avcodec_close(streams[i].context);
|
2017-11-21 04:32:40 +08:00
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
|
2018-04-14 23:03:08 +08:00
|
|
|
avcodec_free_context(&streams[i].context);
|
2020-12-17 23:16:54 +08:00
|
|
|
streams[i].context = nullptr;
|
2017-11-21 04:32:40 +08:00
|
|
|
#endif
|
|
|
|
}
|
2020-12-17 23:16:54 +08:00
|
|
|
delete[] streams;
|
|
|
|
streams = nullptr;
|
2017-11-21 04:32:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( input_format_context ) {
|
|
|
|
#if !LIBAVFORMAT_VERSION_CHECK(53, 17, 0, 25, 0)
|
2018-04-14 23:03:08 +08:00
|
|
|
av_close_input_file(input_format_context);
|
2017-11-21 04:32:40 +08:00
|
|
|
#else
|
2018-04-14 23:03:08 +08:00
|
|
|
avformat_close_input(&input_format_context);
|
2017-11-21 04:32:40 +08:00
|
|
|
#endif
|
2020-12-17 23:16:54 +08:00
|
|
|
input_format_context = nullptr;
|
2017-11-21 04:32:40 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
} // end int FFmpeg_Input::Close()
|
|
|
|
|
2019-06-26 02:11:59 +08:00
|
|
|
AVFrame *FFmpeg_Input::get_frame(int stream_id) {
|
2017-08-24 03:05:44 +08:00
|
|
|
int frameComplete = false;
|
|
|
|
AVPacket packet;
|
2018-04-14 23:03:08 +08:00
|
|
|
av_init_packet(&packet);
|
2017-08-24 03:05:44 +08:00
|
|
|
|
|
|
|
while ( !frameComplete ) {
|
2017-11-21 04:32:40 +08:00
|
|
|
int ret = av_read_frame(input_format_context, &packet);
|
2017-08-24 03:05:44 +08:00
|
|
|
if ( ret < 0 ) {
|
|
|
|
if (
|
|
|
|
// Check if EOF.
|
|
|
|
(ret == AVERROR_EOF || (input_format_context->pb && input_format_context->pb->eof_reached)) ||
|
|
|
|
// Check for Connection failure.
|
|
|
|
(ret == -110)
|
|
|
|
) {
|
2018-04-14 23:03:08 +08:00
|
|
|
Info("av_read_frame returned %s.", av_make_error_string(ret).c_str());
|
2020-08-26 07:45:48 +08:00
|
|
|
return nullptr;
|
2017-08-24 03:05:44 +08:00
|
|
|
}
|
2019-06-26 02:11:59 +08:00
|
|
|
Error("Unable to read packet from stream %d: error %d \"%s\".",
|
2019-06-26 03:34:01 +08:00
|
|
|
packet.stream_index, ret, av_make_error_string(ret).c_str());
|
2020-08-26 07:45:48 +08:00
|
|
|
return nullptr;
|
2017-08-24 03:05:44 +08:00
|
|
|
}
|
2021-02-06 21:46:28 +08:00
|
|
|
ZM_DUMP_STREAM_PACKET(input_format_context->streams[packet.stream_index], packet, "Received packet");
|
2017-08-24 03:05:44 +08:00
|
|
|
|
2018-11-21 01:38:17 +08:00
|
|
|
if ( (stream_id >= 0) && (packet.stream_index != stream_id) ) {
|
|
|
|
Debug(1,"Packet is not for our stream (%d)", packet.stream_index );
|
2020-12-17 23:16:54 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-21 01:38:17 +08:00
|
|
|
|
|
|
|
AVCodecContext *context = streams[packet.stream_index].context;
|
2017-08-24 03:05:44 +08:00
|
|
|
|
2020-12-17 23:16:54 +08:00
|
|
|
if ( frame ) {
|
|
|
|
av_frame_free(&frame);
|
|
|
|
frame = zm_av_frame_alloc();
|
|
|
|
} else {
|
|
|
|
frame = zm_av_frame_alloc();
|
|
|
|
}
|
|
|
|
ret = zm_send_packet_receive_frame(context, frame, packet);
|
|
|
|
if ( ret < 0 ) {
|
|
|
|
Error("Unable to decode frame at frame %d: %d %s, continuing",
|
|
|
|
streams[packet.stream_index].frame_count, ret, av_make_error_string(ret).c_str());
|
|
|
|
zm_av_packet_unref(&packet);
|
|
|
|
av_frame_free(&frame);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if ( is_video_stream(input_format_context->streams[packet.stream_index]) ) {
|
|
|
|
zm_dump_video_frame(frame, "resulting video frame");
|
2018-11-21 01:38:17 +08:00
|
|
|
} else {
|
2020-12-17 23:16:54 +08:00
|
|
|
zm_dump_frame(frame, "resulting frame");
|
2017-08-24 03:05:44 +08:00
|
|
|
}
|
2020-12-17 23:16:54 +08:00
|
|
|
}
|
2017-08-24 03:05:44 +08:00
|
|
|
|
2020-12-17 23:16:54 +08:00
|
|
|
frameComplete = 1;
|
2017-08-24 03:05:44 +08:00
|
|
|
|
2018-04-14 23:03:08 +08:00
|
|
|
zm_av_packet_unref(&packet);
|
2017-08-24 03:05:44 +08:00
|
|
|
|
2018-11-21 01:38:17 +08:00
|
|
|
} // end while !frameComplete
|
2017-08-24 03:05:44 +08:00
|
|
|
return frame;
|
2020-09-29 23:02:40 +08:00
|
|
|
} // end AVFrame *FFmpeg_Input::get_frame
|
2018-09-12 01:21:12 +08:00
|
|
|
|
2019-06-26 02:11:59 +08:00
|
|
|
AVFrame *FFmpeg_Input::get_frame(int stream_id, double at) {
|
2018-09-12 01:21:12 +08:00
|
|
|
Debug(1, "Getting frame from stream %d at %f", stream_id, at);
|
|
|
|
|
2018-11-20 05:45:56 +08:00
|
|
|
int64_t seek_target = (int64_t)(at * AV_TIME_BASE);
|
|
|
|
Debug(1, "Getting frame from stream %d at seektarget: %" PRId64, stream_id, seek_target);
|
2018-09-12 01:21:12 +08:00
|
|
|
seek_target = av_rescale_q(seek_target, AV_TIME_BASE_Q, input_format_context->streams[stream_id]->time_base);
|
2018-09-14 04:18:28 +08:00
|
|
|
Debug(1, "Getting frame from stream %d at %" PRId64, stream_id, seek_target);
|
2018-09-12 01:21:12 +08:00
|
|
|
|
|
|
|
int ret;
|
2019-03-21 22:41:59 +08:00
|
|
|
|
2021-04-20 23:04:27 +08:00
|
|
|
if (!frame) {
|
2019-03-21 22:41:59 +08:00
|
|
|
// Don't have a frame yet, so get a keyframe before the timestamp
|
2019-06-26 02:11:59 +08:00
|
|
|
ret = av_seek_frame(input_format_context, stream_id, seek_target, AVSEEK_FLAG_FRAME);
|
2021-04-20 23:04:27 +08:00
|
|
|
if (ret < 0) {
|
2018-11-20 05:45:56 +08:00
|
|
|
Error("Unable to seek in stream");
|
2020-08-26 07:45:48 +08:00
|
|
|
return nullptr;
|
2018-11-20 05:45:56 +08:00
|
|
|
}
|
2019-03-21 22:41:59 +08:00
|
|
|
// Have to grab a frame to update our current frame to know where we are
|
|
|
|
get_frame(stream_id);
|
2019-12-14 07:12:33 +08:00
|
|
|
|
2021-04-20 23:04:27 +08:00
|
|
|
if (!frame) {
|
|
|
|
Warning("Unable to get frame.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} // end if ! frame
|
2019-12-14 07:12:33 +08:00
|
|
|
|
|
|
|
if (
|
|
|
|
(last_seek_request >= 0)
|
|
|
|
&&
|
2020-10-09 22:21:00 +08:00
|
|
|
(last_seek_request > seek_target)
|
2019-12-14 07:12:33 +08:00
|
|
|
&&
|
|
|
|
(frame->pts > seek_target)
|
|
|
|
) {
|
2019-03-21 22:41:59 +08:00
|
|
|
zm_dump_frame(frame, "frame->pts > seek_target, seek backwards");
|
|
|
|
// our frame must be beyond our seek target. so go backwards to before it
|
2021-04-20 23:04:27 +08:00
|
|
|
if (( ret = av_seek_frame(input_format_context, stream_id, seek_target,
|
2019-03-07 03:50:36 +08:00
|
|
|
AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME
|
2021-04-20 23:04:27 +08:00
|
|
|
) ) < 0) {
|
|
|
|
Error("Unable to seek in stream %d", ret);
|
2020-08-26 07:45:48 +08:00
|
|
|
return nullptr;
|
2018-11-20 05:45:56 +08:00
|
|
|
}
|
2019-03-21 22:41:59 +08:00
|
|
|
// Have to grab a frame to update our current frame to know where we are
|
|
|
|
get_frame(stream_id);
|
2021-01-27 01:21:51 +08:00
|
|
|
if ( is_video_stream(input_format_context->streams[stream_id]) ) {
|
|
|
|
zm_dump_video_frame(frame, "frame->pts > seek_target, got");
|
|
|
|
} else {
|
|
|
|
zm_dump_frame(frame, "frame->pts > seek_target, got");
|
|
|
|
}
|
2020-10-09 22:21:00 +08:00
|
|
|
} else if ( last_seek_request == seek_target ) {
|
|
|
|
// paused case, sending keepalives
|
|
|
|
return frame;
|
2019-03-21 22:41:59 +08:00
|
|
|
} // end if frame->pts > seek_target
|
|
|
|
|
2019-12-14 07:12:33 +08:00
|
|
|
last_seek_request = seek_target;
|
|
|
|
|
2019-03-21 22:41:59 +08:00
|
|
|
// Seeking seems to typically seek to a keyframe, so then we have to decode until we get the frame we want.
|
2019-12-03 04:51:03 +08:00
|
|
|
if ( frame->pts <= seek_target ) {
|
2021-01-27 01:21:51 +08:00
|
|
|
if ( is_video_stream(input_format_context->streams[stream_id]) ) {
|
|
|
|
zm_dump_video_frame(frame, "pts <= seek_target");
|
|
|
|
} else {
|
|
|
|
zm_dump_frame(frame, "pts <= seek_target");
|
|
|
|
}
|
2019-03-21 22:41:59 +08:00
|
|
|
while ( frame && (frame->pts < seek_target) ) {
|
2019-12-03 04:51:03 +08:00
|
|
|
if ( !get_frame(stream_id) ) {
|
|
|
|
Warning("Got no frame. returning nothing");
|
2019-03-21 22:41:59 +08:00
|
|
|
return frame;
|
2019-12-03 04:51:03 +08:00
|
|
|
}
|
2019-03-21 22:41:59 +08:00
|
|
|
}
|
2019-12-03 04:51:03 +08:00
|
|
|
zm_dump_frame(frame, "frame->pts <= seek_target, got");
|
2019-03-21 22:41:59 +08:00
|
|
|
return frame;
|
2018-09-12 01:21:12 +08:00
|
|
|
}
|
2019-03-21 22:41:59 +08:00
|
|
|
|
2018-09-12 01:21:12 +08:00
|
|
|
return get_frame(stream_id);
|
2020-04-27 06:19:30 +08:00
|
|
|
} // end AVFrame *FFmpeg_Input::get_frame( int stream_id, struct timeval at)
|