@@ -426,6 +426,8 @@ static int amf_init_encoder(AVCodecContext *avctx)
426
426
res = ctx -> factory -> pVtbl -> CreateComponent (ctx -> factory , ctx -> context , codec_id , & ctx -> encoder );
427
427
AMF_RETURN_IF_FALSE (ctx , res == AMF_OK , AVERROR_ENCODER_NOT_FOUND , "CreateComponent(%ls) failed with error %d\n" , codec_id , res );
428
428
429
+ ctx -> submitted_frame = 0 ;
430
+
429
431
return 0 ;
430
432
}
431
433
@@ -541,7 +543,6 @@ static int amf_copy_buffer(AVCodecContext *avctx, AVPacket *pkt, AMFBuffer *buff
541
543
if ((ctx -> max_b_frames > 0 || ((ctx -> pa_adaptive_mini_gop == 1 ) ? true : false)) && ctx -> dts_delay == 0 ) {
542
544
int64_t timestamp_last = AV_NOPTS_VALUE ;
543
545
size_t can_read = av_fifo_can_read (ctx -> timestamp_list );
544
-
545
546
AMF_RETURN_IF_FALSE (ctx , can_read > 0 , AVERROR_UNKNOWN ,
546
547
"timestamp_list is empty while max_b_frames = %d\n" , avctx -> max_b_frames );
547
548
av_fifo_peek (ctx -> timestamp_list , & timestamp_last , 1 , can_read - 1 );
@@ -826,6 +827,13 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
826
827
827
828
av_frame_unref (frame );
828
829
ret = av_fifo_write (ctx -> timestamp_list , & pts , 1 );
830
+
831
+ if (ctx -> submitted_frame == 0 )
832
+ {
833
+ ctx -> use_b_frame = (ctx -> max_b_frames > 0 || ((ctx -> pa_adaptive_mini_gop == 1 ) ? true : false));
834
+ }
835
+ ctx -> submitted_frame ++ ;
836
+
829
837
if (ret < 0 )
830
838
return ret ;
831
839
}
@@ -835,7 +843,7 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
835
843
do {
836
844
block_and_wait = 0 ;
837
845
// poll data
838
- if (!avpkt -> data && !avpkt -> buf ) {
846
+ if (!avpkt -> data && !avpkt -> buf && ( ctx -> use_b_frame ? ( ctx -> submitted_frame >= 2 ) : true) ) {
839
847
res_query = ctx -> encoder -> pVtbl -> QueryOutput (ctx -> encoder , & data );
840
848
if (data ) {
841
849
// copy data to packet
@@ -845,6 +853,7 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
845
853
data -> pVtbl -> QueryInterface (data , & guid , (void * * )& buffer ); // query for buffer interface
846
854
ret = amf_copy_buffer (avctx , avpkt , buffer );
847
855
856
+ ctx -> submitted_frame ++ ;
848
857
buffer -> pVtbl -> Release (buffer );
849
858
850
859
if (data -> pVtbl -> HasProperty (data , L"av_frame_ref" )) {
@@ -884,6 +893,7 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
884
893
av_frame_unref (ctx -> delayed_frame );
885
894
AMF_RETURN_IF_FALSE (ctx , res_resubmit == AMF_OK , AVERROR_UNKNOWN , "Repeated SubmitInput() failed with error %d\n" , res_resubmit );
886
895
896
+ ctx -> submitted_frame ++ ;
887
897
ret = av_fifo_write (ctx -> timestamp_list , & pts , 1 );
888
898
if (ret < 0 )
889
899
return ret ;
@@ -902,7 +912,12 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
902
912
if (query_output_data_flag == 0 ) {
903
913
if (res_resubmit == AMF_INPUT_FULL || ctx -> delayed_drain || (ctx -> eof && res_query != AMF_EOF ) || (ctx -> hwsurfaces_in_queue >= ctx -> hwsurfaces_in_queue_max )) {
904
914
block_and_wait = 1 ;
905
- av_usleep (1000 );
915
+
916
+ // Only sleep if the driver doesn't support waiting in QueryOutput()
917
+ // or if we already have output data so we will skip calling it.
918
+ if (!ctx -> query_timeout_supported || avpkt -> data || avpkt -> buf ) {
919
+ av_usleep (1000 );
920
+ }
906
921
}
907
922
}
908
923
} while (block_and_wait );
0 commit comments