Browse Source

ffmpeg: fix the ffmpeg API issue

1. fix the omx decoder bug
2. fix omx decode setting pix_fmt bug

Signed-off-by: sw.multimedia <sw.multimedia@starfivetech.com>
Andy Hu 2 years ago
parent
commit
2874a6cd0c

+ 328 - 0
package/ffmpeg/0005-avcodec-fix-docoder-bug.patch

@@ -0,0 +1,328 @@
+From 359fba829644ceab731aefd34d06eae7ec0d2b9e Mon Sep 17 00:00:00 2001
+From: sw.multimedia <sw.multimedia@starfivetech.com>
+Date: Mon, 24 Jan 2022 11:42:27 +0800
+Subject: [PATCH] ffmpeg: fix docoder bug
+
+---
+ libavcodec/omxdec.c | 167 ++++++++++++++++++++++++-------------------------------
+ 1 file changed, 74 insertions(+), 93 deletions(-)
+
+diff --git a/libavcodec/omxdec.c b/libavcodec/omxdec.c
+index 14be242..27a7949 100755
+--- a/libavcodec/omxdec.c
++++ b/libavcodec/omxdec.c
+@@ -46,6 +46,10 @@
+ #include "internal.h"
+ #include "profiles.h"
+ 
++int evnet_bufferflag;
++int dec_out_height;
++int dec_out_width;
++int dec_pix_fmt;
+ 
+ #ifdef OMX_SKIP64BIT
+ static OMX_TICKS to_omx_ticks(int64_t value)
+@@ -224,7 +228,7 @@ typedef struct OMXCodecContext {
+ 
+     int mutex_cond_inited;
+ 
+-    int eos_sent, got_eos, first_get_outbuffer;
++    int eos_sent, got_eos, evnet_bufferflag;
+ 
+ 	int extradata_sent;
+ 
+@@ -275,6 +279,11 @@ static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, O
+     // This uses casts in the printfs, since OMX_U32 actually is a typedef for
+     // unsigned long in official header versions (but there are also modified
+     // versions where it is something else).
++    OMX_PARAM_PORTDEFINITIONTYPE out_port_params = { 0 };
++	OMX_PORT_PARAM_TYPE video_port_params = { 0 };
++	OMX_ERRORTYPE err;
++	int i;
++	
+     switch (event) {
+     case OMX_EventError:
+         pthread_mutex_lock(&s->state_mutex);
+@@ -300,8 +309,36 @@ static OMX_ERRORTYPE event_handler(OMX_HANDLETYPE component, OMX_PTR app_data, O
+         }
+         break;
+     case OMX_EventPortSettingsChanged:
+-        av_log(s->avctx, AV_LOG_VERBOSE, "OMX port %"PRIu32" settings changed\n", (uint32_t) data1);
++        av_log(s->avctx, AV_LOG_ERROR, "OMX port %"PRIu32" settings changed\n", (uint32_t) data1);
++		INIT_STRUCT(video_port_params);
++		err = OMX_GetParameter(s->handle, OMX_IndexParamVideoInit, &video_port_params);
++		if(err != OMX_ErrorNone){
++            av_log(s->avctx, AV_LOG_ERROR, "err %d\n",err);          
++            return AVERROR_UNKNOWN;
++			}
++
++		for (i = 0; i < video_port_params.nPorts; i++) {
++	        int port = video_port_params.nStartPortNumber + i;
++	        OMX_PARAM_PORTDEFINITIONTYPE port_params = { 0 };
++	        INIT_STRUCT(port_params);
++	        port_params.nPortIndex = port;
++	        err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &port_params);
++	        if (err != OMX_ErrorNone) {
++	            av_log(s->avctx, AV_LOG_WARNING, "port %d error %x\n", port, err);
++	            return AVERROR_UNKNOWN;
++	        }
++	        if (port_params.eDir == OMX_DirOutput) {
++				out_port_params = port_params;
++				dec_out_width = out_port_params.format.video.nFrameWidth;
++				dec_out_height = out_port_params.format.video.nFrameHeight;
++				dec_pix_fmt = out_port_params.format.video.eColorFormat;
++	            
++	        } 
++	    }		
+         break;
++	case OMX_EventBufferFlag:
++		av_log(s->avctx, AV_LOG_VERBOSE, "OMX decoder competd set event_bufferflag\n");
++		evnet_bufferflag = 1;
+     default:
+         av_log(s->avctx, AV_LOG_VERBOSE, "OMX event %d %"PRIx32" %"PRIx32"\n",
+                                          event, (uint32_t) data1, (uint32_t) data2);
+@@ -454,7 +491,7 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
+ 
+     in_port_params.format.video.pNativeRender         = NULL;
+     in_port_params.format.video.bFlagErrorConcealment = OMX_FALSE;
+-    //in_port_params.format.video.eColorFormat          = s->color_format;
++    in_port_params.format.video.eColorFormat          = s->color_format;
+     s->stride     = avctx->width;
+     s->plane_size = avctx->height;
+     // If specific codecs need to manually override the stride/plane_size,
+@@ -474,11 +511,9 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
+     err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &in_port_params);
+     CHECK(err);
+ 
+-#if 1
+     s->stride         = in_port_params.format.video.nStride;
+     s->plane_size     = in_port_params.format.video.nSliceHeight;
+     s->num_in_buffers = in_port_params.nBufferCountActual;
+-#endif
+     err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
+     out_port_params.bEnabled   = OMX_TRUE;
+     out_port_params.bPopulated = OMX_FALSE;
+@@ -498,14 +533,13 @@ static av_cold int omx_component_init(AVCodecContext *avctx, const char *role)
+         out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;	
+     else if (avctx->codec->id == AV_CODEC_ID_HEVC)
+         out_port_params.format.video.eCompressionFormat = OMX_VIDEO_CodingHEVC;
+-
++	
+     err = OMX_SetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
+     CHECK(err);
+     err = OMX_GetParameter(s->handle, OMX_IndexParamPortDefinition, &out_port_params);
+     CHECK(err);
+     s->num_out_buffers = out_port_params.nBufferCountActual;
+ 
+-
+     err = OMX_SendCommand(s->handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
+     CHECK(err);
+ 
+@@ -607,8 +641,6 @@ static av_cold int omx_decode_init(AVCodecContext *avctx)
+ 
+     int ret = AVERROR_ENCODER_NOT_FOUND;
+     const char *role;
+-    //OMX_BUFFERHEADERTYPE *buffer;
+-    //OMX_ERRORTYPE err;
+ 
+     s->omx_context = omx_init(avctx, s->libname, s->libprefix);
+     if (!s->omx_context)
+@@ -648,51 +680,6 @@ static av_cold int omx_decode_init(AVCodecContext *avctx)
+     if ((ret = omx_component_init(avctx, role)) < 0)
+         goto fail;
+ 	
+-#if 0
+-    if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
+-        while (1) {
+-            buffer = get_buffer(&s->output_mutex, &s->output_cond,
+-                                &s->num_done_out_buffers, s->done_out_buffers, 1);
+-            if (buffer->nFlags & OMX_BUFFERFLAG_CODECCONFIG) {
+-                if ((ret = av_reallocp(&avctx->extradata, avctx->extradata_size + buffer->nFilledLen + AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
+-                    avctx->extradata_size = 0;
+-                    goto fail;
+-                }
+-                memcpy(avctx->extradata + avctx->extradata_size, buffer->pBuffer + buffer->nOffset, buffer->nFilledLen);
+-                avctx->extradata_size += buffer->nFilledLen;
+-                memset(avctx->extradata + avctx->extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
+-            }
+-            err = OMX_FillThisBuffer(s->handle, buffer);
+-            if (err != OMX_ErrorNone) {
+-                append_buffer(&s->output_mutex, &s->output_cond,
+-                              &s->num_done_out_buffers, s->done_out_buffers, buffer);
+-                av_log(avctx, AV_LOG_ERROR, "OMX_FillThisBuffer failed: %x\n", err);
+-                ret = AVERROR_UNKNOWN;
+-                goto fail;
+-            }
+-            if (avctx->codec->id == AV_CODEC_ID_H264) {
+-                // For H.264, the extradata can be returned in two separate buffers
+-                // (the videocore encoder on raspberry pi does this);
+-                // therefore check that we have got both SPS and PPS before continuing.
+-                int nals[32] = { 0 };
+-                int i;
+-                for (i = 0; i + 4 < avctx->extradata_size; i++) {
+-                     if (!avctx->extradata[i + 0] &&
+-                         !avctx->extradata[i + 1] &&
+-                         !avctx->extradata[i + 2] &&
+-                         avctx->extradata[i + 3] == 1) {
+-                         nals[avctx->extradata[i + 4] & 0x1f]++;
+-                     }
+-                }
+-                if (nals[H264_NAL_SPS] && nals[H264_NAL_PPS])
+-                    break;
+-            } else {
+-                if (avctx->extradata_size > 0)
+-                    break;
+-            }
+-        }
+-    }
+-#endif
+ 	
+     return 0;
+ fail:
+@@ -715,10 +702,6 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
+ 	int linesize[4];
+ 
+     if (pkt->size) {
+-
+-		//VPU init and fill buffer slow, so empty buf sleep to send before get vpu fill buf.
+-		if(!s->first_get_outbuffer)
+-			av_usleep(100000);
+ 	
+         buffer = get_buffer(&s->input_mutex, &s->input_cond,
+                             &s->num_free_in_buffers, s->free_in_buffers, 1);
+@@ -729,7 +712,7 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
+         }
+ 
+ 		//cpy the extradata
+-		if(!s->extradata_sent) {
++		if(!s->extradata_sent && avctx->extradata ) {
+ 
+ 			memcpy(buffer->pBuffer + buffer->nOffset, avctx->extradata, avctx->extradata_size);	
+ 			memcpy(buffer->pBuffer + buffer->nOffset + avctx->extradata_size, pkt->data, pkt->size);
+@@ -741,14 +724,11 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
+ 			memcpy(buffer->pBuffer + buffer->nOffset, pkt->data, pkt->size);
+ 			buffer->nFilledLen = pkt->size;
+ 		}
+-
+-		/* avoid memcpy. point it addr*/
+-		//buffer->pAppPrivate = pkt;
+-		//buffer->pBuffer = pkt->data;
+-		//buffer->nFilledLen = pkt->size;
+ 		
+ 		buffer->pOutputPortPrivate = NULL;
+ 		buffer->pAppPrivate = avctx->priv_data;
++		buffer->nFlags = OMX_BUFFERFLAG_ENDOFFRAME;
++
+ 
+ 	    err = OMX_EmptyThisBuffer(s->handle, buffer);
+ 	    if (err != OMX_ErrorNone) {
+@@ -757,16 +737,19 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
+ 	        return AVERROR_UNKNOWN;
+ 	    }
+     } else if (!s->eos_sent) {
+-
+-		if(!s->first_get_outbuffer)
+-			av_usleep(1000000);
+ 		
+         buffer = get_buffer(&s->input_mutex, &s->input_cond,
+                             &s->num_free_in_buffers, s->free_in_buffers, 1);
+ 
++		if(!buffer) {
++        	av_log(avctx, AV_LOG_ERROR, "get_buffer NULL\n");
++            return AVERROR(ENOMEM);
++		}
++
+         buffer->nFilledLen = 0;
+         buffer->nFlags = OMX_BUFFERFLAG_EOS;
+         buffer->pAppPrivate = buffer->pOutputPortPrivate = NULL;
++
+         err = OMX_EmptyThisBuffer(s->handle, buffer);
+         if (err != OMX_ErrorNone) {
+             append_buffer(&s->input_mutex, &s->input_cond, &s->num_free_in_buffers, s->free_in_buffers, buffer);
+@@ -780,52 +763,49 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
+         // If not flushing, just poll the queue if there's finished packets.
+         // If flushing, do a blocking wait until we either get a completed
+         // packet, or get EOS.
++		
+         buffer = get_buffer(&s->output_mutex, &s->output_cond,
+                             &s->num_done_out_buffers, s->done_out_buffers,
+                             !pkt || had_partial);
+ 
+         if (!buffer) {
+-            /*eos is sent but fill buf still can't get then continue*/
+-            if(!s->first_get_outbuffer && s->eos_sent)
++            /*eos is sent wait for vpu evnet_bufferflag to get all frames*/
++            if(s->eos_sent && !evnet_bufferflag){
+ 				continue; 
+-			else
+-				break;
+-        }
+-		//if (!buffer)
+-           // break;
++       		}	
++			break;
++        } 
+ 
+-		if(!buffer->nFilledLen)
++		if(!buffer->nFilledLen){
++			av_log(avctx, AV_LOG_ERROR, "buffer->nFilledLen %d\n",(int)buffer->nFilledLen);
+ 			goto end;
+-		
+-		if(!s->first_get_outbuffer)
+-			s->first_get_outbuffer = 1;
+-		
++		}
++		avctx->width = dec_out_width;
++		avctx->height = dec_out_height;
++		avctx->pix_fmt = AV_PIX_FMT_YUV420P;
++		s->stride     = avctx->width;
++    	s->plane_size = avctx->height;
++		//avctx->pix_fmt = dec_pix_fmt;
++
+         if (buffer->nFlags & OMX_BUFFERFLAG_EOS)
+             s->got_eos = 1;
+-       
++
+ 		if ((ret = ff_get_buffer(avctx, avframe, 0)) < 0) {
+ 			av_log(avctx, AV_LOG_ERROR, "Unable to allocate buffer\n");
+ 			goto end;
+ 		}
+-		
++
+ 		ret = av_image_fill_arrays(dst, linesize, buffer->pBuffer,
+ 								   avctx->pix_fmt, s->stride, s->plane_size, 1);
+-		if (ret < 0)
++		if (ret < 0){
++			av_log(avctx, AV_LOG_ERROR, "av_image_fill_arrays ret:%d\n", ret);
+ 			goto end;
++		}
++		
+ 		av_image_copy(avframe->data, avframe->linesize, (const uint8_t**)dst, linesize, 
+ 							avctx->pix_fmt, avctx->width, avctx->height);
+-	
+-		//avframe->pts	 = buffer->nTimeStamp;
+-		//avframe->pkt_dts = AV_NOPTS_VALUE;
+-		//avframe->pict_type= AV_PICTURE_TYPE_I;
+-    	//avframe->key_frame= 1;
+-	
++		
+ 		*got_packet = 1;
+-
+-	/*
+-		if ((ret = av_frame_ref(data, avframe)) < 0)
+-         	goto end;
+-         	*/
+ end:     
+         err = OMX_FillThisBuffer(s->handle, buffer);
+         if (err != OMX_ErrorNone) {
+@@ -834,6 +814,7 @@ end:
+             ret = AVERROR_UNKNOWN;
+         }
+     }
++	
+     return ret;
+ }
+ 
+-- 
+2.17.1
+

+ 91 - 0
package/ffmpeg/0006-ffmpeg-fix-omx-decode-setting-pix_fmt-bug.patch

@@ -0,0 +1,91 @@
+From b2d8aa7d8ad754f9e4a203c6931476335f0e15b6 Mon Sep 17 00:00:00 2001
+From: sw.multimedia <sw.multimedia@starfivetech.com>
+Date: Wed, 26 Jan 2022 16:40:10 +0800
+Subject: [PATCH] ffmpeg: fix omx decode setting pix_fmt bug
+
+---
+ libavcodec/omxdec.c | 38 ++++++++++++++++++++++++++++++++++++--
+ 1 file changed, 36 insertions(+), 2 deletions(-)
+
+diff --git a/libavcodec/omxdec.c b/libavcodec/omxdec.c
+index 27a7949..f5da60d 100755
+--- a/libavcodec/omxdec.c
++++ b/libavcodec/omxdec.c
+@@ -28,6 +28,7 @@
+ #include <dlfcn.h>
+ #include <OMX_Core.h>
+ #include <OMX_Component.h>
++#include <OMX_IVCommon.h>
+ #include <pthread.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+@@ -79,6 +80,8 @@ static int64_t from_omx_ticks(OMX_TICKS value)
+             return AVERROR_UNKNOWN;                                       \
+         }                                                                 \
+     } while (0)
++    
++#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
+ 
+ typedef struct OMXContext {
+     void *lib;
+@@ -93,6 +96,37 @@ typedef struct OMXContext {
+     void (*host_init)(void);
+ } OMXContext;
+ 
++
++static const struct {
++
++    int color_format;
++    enum AVPixelFormat pix_fmt;
++
++} color_formats[] = {
++
++    { OMX_COLOR_FormatYUV420Planar,                           AV_PIX_FMT_YUV420P },
++    { OMX_COLOR_FormatYUV420SemiPlanar,                       AV_PIX_FMT_NV12    },
++    { OMX_COLOR_FormatYUV420PackedSemiPlanar,                 AV_PIX_FMT_NV21    },
++    { 0 }
++};
++
++static enum AVPixelFormat omx_map_color_format(AVCodecContext *avctx, int color_format)
++{
++    int i;
++    enum AVPixelFormat ret = AV_PIX_FMT_NONE;
++
++    for (i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
++        if (color_formats[i].color_format == color_format) {
++            return color_formats[i].pix_fmt;
++        }
++    }
++
++    av_log(avctx, AV_LOG_ERROR, "Output color format 0x%x (value=%d) is not supported\n",
++        color_format, color_format);
++
++    return ret;
++}
++
+ static av_cold void *dlsym_prefixed(void *handle, const char *symbol, const char *prefix)
+ {
+     char buf[50];
+@@ -635,6 +669,7 @@ static av_cold void cleanup(OMXCodecContext *s)
+     av_freep(&s->output_buf);
+ }
+ 
++
+ static av_cold int omx_decode_init(AVCodecContext *avctx)
+ {
+     OMXCodecContext *s = avctx->priv_data;
+@@ -782,10 +817,9 @@ static int omx_decode_frame(AVCodecContext *avctx, void *data,
+ 		}
+ 		avctx->width = dec_out_width;
+ 		avctx->height = dec_out_height;
+-		avctx->pix_fmt = AV_PIX_FMT_YUV420P;
++		avctx->pix_fmt = omx_map_color_format(avctx, dec_pix_fmt);
+ 		s->stride     = avctx->width;
+     	s->plane_size = avctx->height;
+-		//avctx->pix_fmt = dec_pix_fmt;
+ 
+         if (buffer->nFlags & OMX_BUFFERFLAG_EOS)
+             s->got_eos = 1;
+-- 
+2.17.1
+