@@ -98,10 +98,13 @@ int FFmpeg_Input::Open(const char *filepath) {
9898
9999 streams[i].context = avcodec_alloc_context3 (streams[i].codec );
100100 avcodec_parameters_to_context (streams[i].context , input_format_context->streams [i]->codecpar );
101+
101102 if (setup_hwaccel (streams[i].context ,
102- chosen_codec_data, hw_device_ctx, " " , input_format_context->streams [i]->codecpar ->width ,
103+ chosen_codec_data, hw_device_ctx, " " ,
104+ input_format_context->streams [i]->codecpar ->width ,
103105 input_format_context->streams [i]->codecpar ->height
104106 )) {
107+ Warning (" Failed to setup hw_accel" );
105108 continue ;
106109 }
107110
@@ -111,14 +114,16 @@ int FFmpeg_Input::Open(const char *filepath) {
111114 if (error < 0 ) {
112115 Error (" Could not open input codec (error '%s')" ,
113116 av_make_error_string (error).c_str ());
114- avcodec_free_context (&streams[i].context );
117+
118+ avcodec_free_context (&(streams[i].context ));
115119 streams[i].context = nullptr ;
116120 continue ;
117121 }
118122 break ;
119123 } // end foreach codec_data
120124
121125 if (!streams[i].context ) {
126+ Debug (1 , " Failed with known codecs, trying harder" );
122127 if ((streams[i].codec = avcodec_find_decoder (input_format_context->streams [i]->codecpar ->codec_id ))) {
123128 Debug (1 , " Using codec (%s) for stream %d" , streams[i].codec ->name , i);
124129 streams[i].context = avcodec_alloc_context3 (streams[i].codec );
@@ -142,7 +147,7 @@ int FFmpeg_Input::Open(const char *filepath) {
142147 return error;
143148 }
144149 zm_dump_codec (streams[i].context );
145- if (!(streams[i].context ->time_base .num && streams[i].context ->time_base .den )) {
150+ if (0 and !(streams[i].context ->time_base .num && streams[i].context ->time_base .den )) {
146151 Debug (1 , " Setting to default time base" );
147152 streams[i].context ->time_base .num = 1 ;
148153 streams[i].context ->time_base .den = 90000 ;
@@ -189,42 +194,18 @@ AVFrame *FFmpeg_Input::get_frame(int stream_id) {
189194 return nullptr ;
190195 }
191196
197+ AVCodecContext *context = streams[stream_id].context ;
192198
193199 while (!frameComplete) {
194- int ret = av_read_frame (input_format_context, packet.get ());
195- if (ret < 0 ) {
196- if (
197- // Check if EOF.
198- (ret == AVERROR_EOF || (input_format_context->pb && input_format_context->pb ->eof_reached )) ||
199- // Check for Connection failure.
200- (ret == -110 )
201- ) {
202- Info (" av_read_frame returned %s." , av_make_error_string (ret).c_str ());
203- return nullptr ;
204- }
205- Error (" Unable to read packet from stream %d: error %d \" %s\" ." ,
206- packet->stream_index , ret, av_make_error_string (ret).c_str ());
207- return nullptr ;
208- }
209- ZM_DUMP_STREAM_PACKET (input_format_context->streams [packet->stream_index ], packet, " Received packet" );
210-
211- av_packet_guard pkt_guard{packet};
212-
213- if ((stream_id >= 0 ) && (packet->stream_index != stream_id)) {
214- Debug (4 , " Packet is not for our stream (%d)" , packet->stream_index );
215- continue ;
216- }
217-
218- AVCodecContext *context = streams[packet->stream_index ].context ;
219200
220201 frame = av_frame_ptr{zm_av_frame_alloc ()};
221202 if (!frame) {
222203 Error (" Unable to allocate frame." );
223204 return nullptr ;
224205 }
225-
206+
226207 // Since technically sending a packet can result in multiple frames (or buffered_frames) try receive_frame first.
227- ret = avcodec_receive_frame (context, frame.get ());
208+ int ret = avcodec_receive_frame (context, frame.get ());
228209 Debug (1 , " Ret from receive_frame ret: %d %s" , ret, av_make_error_string (ret).c_str ());
229210 if (ret == AVERROR (EAGAIN)) {
230211 // Perfectly normal
@@ -241,6 +222,30 @@ AVFrame *FFmpeg_Input::get_frame(int stream_id) {
241222 break ;
242223 }
243224
225+ ret = av_read_frame (input_format_context, packet.get ());
226+ if (ret < 0 ) {
227+ if (
228+ // Check if EOF.
229+ (ret == AVERROR_EOF || (input_format_context->pb && input_format_context->pb ->eof_reached )) ||
230+ // Check for Connection failure.
231+ (ret == -110 )
232+ ) {
233+ Info (" av_read_frame returned %s." , av_make_error_string (ret).c_str ());
234+ return nullptr ;
235+ }
236+ Error (" Unable to read packet from stream %d: error %d \" %s\" ." ,
237+ packet->stream_index , ret, av_make_error_string (ret).c_str ());
238+ return nullptr ;
239+ }
240+ ZM_DUMP_STREAM_PACKET (input_format_context->streams [packet->stream_index ], packet, " Received packet" );
241+
242+ // av_packet_guard pkt_guard{packet};
243+
244+ if ((stream_id >= 0 ) && (packet->stream_index != stream_id)) {
245+ Debug (4 , " Packet is not for our stream (%d)" , packet->stream_index );
246+ continue ;
247+ }
248+
244249 ret = avcodec_send_packet (context, packet.get ());
245250 if (ret == AVERROR (EAGAIN)) {
246251 Debug (2 , " Unable to send packet %d %s" , ret, av_make_error_string (ret).c_str ());
0 commit comments