2017-06-23 05:58:32 +08:00
|
|
|
|
|
|
|
#include "zm_ffmpeg_input.h"
|
2017-06-27 04:55:49 +08:00
|
|
|
#include "zm_logger.h"
|
|
|
|
#include "zm_ffmpeg.h"
|
2017-06-23 05:58:32 +08:00
|
|
|
|
2017-06-27 04:55:49 +08:00
|
|
|
FFmpeg_Input::FFmpeg_Input() {
|
2017-06-23 05:58:32 +08:00
|
|
|
input_format_context = NULL;
|
|
|
|
video_stream_id = -1;
|
|
|
|
audio_stream_id = -1;
|
2017-08-24 03:05:44 +08:00
|
|
|
av_register_all();
|
|
|
|
avcodec_register_all();
|
2017-11-19 05:00:10 +08:00
|
|
|
streams = NULL;
|
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-19 05:00:10 +08:00
|
|
|
if ( streams ) {
|
|
|
|
delete streams;
|
|
|
|
streams = NULL;
|
|
|
|
}
|
2017-06-27 04:55:49 +08:00
|
|
|
}
|
2017-06-23 05:58:32 +08:00
|
|
|
|
2017-06-27 04:55:49 +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. */
|
2017-08-24 03:05:44 +08:00
|
|
|
if ( (error = avformat_open_input( &input_format_context, filepath, NULL, NULL)) < 0 ) {
|
2017-06-23 05:58:32 +08:00
|
|
|
|
|
|
|
Error("Could not open input file '%s' (error '%s')\n",
|
|
|
|
filepath, av_make_error_string(error).c_str() );
|
|
|
|
input_format_context = NULL;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Get information on the input file (number of streams etc.). */
|
2017-08-24 03:05:44 +08:00
|
|
|
if ( (error = avformat_find_stream_info(input_format_context, NULL)) < 0 ) {
|
2017-06-23 05:58:32 +08:00
|
|
|
Error( "Could not open find stream info (error '%s')\n",
|
|
|
|
av_make_error_string(error).c_str() );
|
|
|
|
avformat_close_input(&input_format_context);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2017-11-19 05:00:10 +08:00
|
|
|
streams = new stream[input_format_context->nb_streams];
|
|
|
|
|
2017-06-23 05:58:32 +08:00
|
|
|
for ( unsigned int i = 0; i < input_format_context->nb_streams; i += 1 ) {
|
|
|
|
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 {
|
|
|
|
Warning( "Have another video stream." );
|
|
|
|
}
|
2017-08-24 03:05:44 +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 ) {
|
|
|
|
audio_stream_id = i;
|
|
|
|
} else {
|
|
|
|
Warning( "Have another audio stream." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
streams[i].context = avcodec_alloc_context3( NULL );
|
|
|
|
avcodec_parameters_to_context( streams[i].context, input_format_context->streams[i]->codecpar );
|
|
|
|
#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)) ) {
|
2017-06-23 05:58:32 +08:00
|
|
|
Error( "Could not find input codec\n");
|
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 {
|
|
|
|
Debug(1, "Using codec (%s) for stream %d", streams[i].codec->name, i );
|
2017-06-23 05:58:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((error = avcodec_open2( streams[i].context, streams[i].codec, NULL)) < 0) {
|
2017-06-27 04:55:49 +08:00
|
|
|
Error( "Could not open input codec (error '%s')\n",
|
2017-06-23 05:58:32 +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)
|
2017-06-27 04:55:49 +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);
|
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 )
|
|
|
|
Error( "Unable to locate video stream in %s", filepath );
|
|
|
|
if ( audio_stream_id == -1 )
|
|
|
|
Debug( 3, "Unable to locate audio stream in %s", filepath );
|
|
|
|
|
|
|
|
return 0;
|
2017-08-24 03:05:44 +08:00
|
|
|
} // end int FFmpeg_Input::Open( const char * filepath )
|
|
|
|
|
|
|
|
AVFrame *FFmpeg_Input::get_frame( int stream_id ) {
|
|
|
|
Debug(1, "Getting frame from stream %d", stream_id );
|
|
|
|
|
|
|
|
int frameComplete = false;
|
|
|
|
AVPacket packet;
|
|
|
|
av_init_packet( &packet );
|
|
|
|
AVFrame *frame = zm_av_frame_alloc();
|
|
|
|
char errbuf[AV_ERROR_MAX_STRING_SIZE];
|
|
|
|
|
|
|
|
while ( !frameComplete ) {
|
|
|
|
int ret = av_read_frame( input_format_context, &packet );
|
|
|
|
if ( ret < 0 ) {
|
|
|
|
av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);
|
|
|
|
if (
|
|
|
|
// Check if EOF.
|
|
|
|
(ret == AVERROR_EOF || (input_format_context->pb && input_format_context->pb->eof_reached)) ||
|
|
|
|
// Check for Connection failure.
|
|
|
|
(ret == -110)
|
|
|
|
) {
|
|
|
|
Info( "av_read_frame returned %s.", errbuf );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Error( "Unable to read packet from stream %d: error %d \"%s\".", packet.stream_index, ret, errbuf );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (stream_id < 0 ) || ( packet.stream_index == stream_id ) ) {
|
2018-03-03 10:26:07 +08:00
|
|
|
Debug(3,"Packet is for our stream (%d)", packet.stream_index );
|
2017-08-24 03:05:44 +08:00
|
|
|
|
|
|
|
AVCodecContext *context = streams[packet.stream_index].context;
|
|
|
|
|
|
|
|
#if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
|
|
|
|
ret = avcodec_send_packet( context, &packet );
|
|
|
|
if ( ret < 0 ) {
|
|
|
|
av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE );
|
|
|
|
Error( "Unable to send packet at frame %d: %s, continuing", streams[packet.stream_index].frame_count, errbuf );
|
|
|
|
zm_av_packet_unref( &packet );
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
Debug(1, "Success getting a packet");
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_AVUTIL_HWCONTEXT_H
|
|
|
|
if ( hwaccel ) {
|
|
|
|
ret = avcodec_receive_frame( context, hwFrame );
|
|
|
|
if ( ret < 0 ) {
|
|
|
|
av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE );
|
|
|
|
Error( "Unable to receive frame %d: %s, continuing", streams[packet.stream_index].frame_count, errbuf );
|
|
|
|
zm_av_packet_unref( &packet );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ret = av_hwframe_transfer_data(frame, hwFrame, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE );
|
|
|
|
Error( "Unable to transfer frame at frame %d: %s, continuing", streams[packet.stream_index].frame_count, errbuf );
|
|
|
|
zm_av_packet_unref( &packet );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
Debug(1,"Getting a frame?");
|
|
|
|
ret = avcodec_receive_frame( context, frame );
|
|
|
|
if ( ret < 0 ) {
|
|
|
|
av_strerror( ret, errbuf, AV_ERROR_MAX_STRING_SIZE );
|
|
|
|
Error( "Unable to send packet at frame %d: %s, continuing", streams[packet.stream_index].frame_count, errbuf );
|
|
|
|
zm_av_packet_unref( &packet );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_AVUTIL_HWCONTEXT_H
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
frameComplete = 1;
|
|
|
|
# else
|
2018-03-03 10:26:07 +08:00
|
|
|
ret = zm_avcodec_decode_video(context, frame, &frameComplete, &packet);
|
2017-08-24 03:05:44 +08:00
|
|
|
if ( ret < 0 ) {
|
2018-03-03 10:26:07 +08:00
|
|
|
av_strerror(ret, errbuf, AV_ERROR_MAX_STRING_SIZE);
|
2017-08-24 21:53:40 +08:00
|
|
|
Error( "Unable to decode frame at frame %d: %s, continuing", streams[packet.stream_index].frame_count, errbuf );
|
2017-08-24 03:05:44 +08:00
|
|
|
zm_av_packet_unref( &packet );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // end if it's the right stream
|
|
|
|
|
2018-03-03 10:26:07 +08:00
|
|
|
zm_av_packet_unref( &packet );
|
2017-08-24 03:05:44 +08:00
|
|
|
|
|
|
|
} // end while ! frameComplete
|
|
|
|
return frame;
|
2017-06-23 05:58:32 +08:00
|
|
|
|
2017-08-24 03:05:44 +08:00
|
|
|
} // end AVFrame *FFmpeg_Input::get_frame
|