From 351df57cd987db03d12b2701b3aaf2b450c6dfab Mon Sep 17 00:00:00 2001 From: Malanchi Date: Mon, 2 Sep 2024 17:29:12 +0800 Subject: [PATCH 01/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/Makefile | 2 + mxVision/Ascendffmpeg/libavcodec/allcodecs.c | 1 + .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 963 ++++++++++++++++++ .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.h | 146 +++ 4 files changed, 1112 insertions(+) create mode 100644 mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c create mode 100644 mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h diff --git a/mxVision/Ascendffmpeg/libavcodec/Makefile b/mxVision/Ascendffmpeg/libavcodec/Makefile index fc092e39f..31d43500e 100644 --- a/mxVision/Ascendffmpeg/libavcodec/Makefile +++ b/mxVision/Ascendffmpeg/libavcodec/Makefile @@ -27,6 +27,7 @@ HEADERS = ac3_parser.h \ xvmc.h \ ascend_dec.h \ ascend_enc.h \ + ascend_jpeg_dec.h \ OBJS = ac3_parser.o \ adts_parser.o \ @@ -374,6 +375,7 @@ OBJS-$(CONFIG_H264_DECODER) += h264dec.o h264_cabac.o h264_cavlc.o \ h264_slice.o h264data.o OBJS-$(CONFIG_H264_ASCEND_DECODER) += ascend_dec.o OBJS-$(CONFIG_H264_ASCEND_ENCODER) += ascend_enc.o +OBJS-$(CONFIG_JPEG_ASCEND_DECODER) += ascend_jpeg_dec.o OBJS-$(CONFIG_H264_AMF_ENCODER) += amfenc_h264.o OBJS-$(CONFIG_H264_CUVID_DECODER) += cuviddec.o OBJS-$(CONFIG_H264_MEDIACODEC_DECODER) += mediacodecdec.o diff --git a/mxVision/Ascendffmpeg/libavcodec/allcodecs.c b/mxVision/Ascendffmpeg/libavcodec/allcodecs.c index b6885f8cd..c34405e9b 100644 --- a/mxVision/Ascendffmpeg/libavcodec/allcodecs.c +++ b/mxVision/Ascendffmpeg/libavcodec/allcodecs.c @@ -791,6 +791,7 @@ extern AVCodec ff_h264_ascend_decoder; extern AVCodec ff_h265_ascend_decoder; extern AVCodec ff_h264_ascend_encoder; extern AVCodec ff_h265_ascend_encoder; +extern AVCodec ff_jpeg_ascend_decoder; extern AVCodec ff_h264_amf_encoder; extern AVCodec ff_h264_cuvid_decoder; extern AVCodec ff_h264_mf_encoder; diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c new file mode 100644 index 000000000..35fb89dd2 --- /dev/null +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -0,0 +1,963 @@ +/* + * Copyright(c) 2020. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except int compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include "ascend_jpeg_dec.h" + +static void *get_frame(void *arg) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)arg; + int ret = 0; + int eos_flag = 0; + ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); + return ((void*) (-1)); + } + + hi_video_frame_info frame; + hi_vdec_stream stream; + hi_vdec_supplement_info stSupplement; + + av_log(NULL, AV_LOG_INFO, "Thread start.\n"); + + while (ctx->thread_run_flag) { + ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); + if (ret != 0) { + if (ctx->decoder_flushing && ret == HI_ERR_VDEC_BUF_EMPTY) { + eos_flag = 1; + av_log(ctx, AV_LOG_DEBUG, "Decoder flushing or stream eos.\n"); + } else { + av_log(ctx, AV_LOG_DEBUG, "HiMpi get frame failed, ret is %d.\n", ret); + continue; + } + } + + size_t decResult = frame.v_frame.frame_flag; + if (eos_flag) { + // eos + FrameInfo_t frame_info; + memset(&frame_info, 0, sizeof(FrameInfo_t)); + frame_info.event_type = EVENT_EOS; + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + sem_post(&ctx->eos_sema); + av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); + break; + } + + hi_mpi_dvpp_free(stream.addr); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); + } + if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { + hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); + } + + if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { + ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); + return ((void*) (-1)); + } + continue; + } + FrameInfo_t frame_info; + frame_info.ascend_ctx = ctx; + get_vdec_frame_info(&frame_info, frame); + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); + av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + + ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); + return ((void*) (-1)); + } + + ctx->total_out_frame_count++; + } + return NULL; +} + +static inline int decode_params_checking(AVCodecContext* avctx) +{ + switch (avctx->codec->id) { + case AV_CODEC_ID_H264: + case AV_CODEC_ID_H265: + if (avctx->width < 128 || avctx->height < 128 || + avctx->width > 4096 || avctx->height > 4096) { + av_log(avctx, AV_LOG_ERROR, + "H264 decoder only support resolution: 128x128 ~ 4096x4096, now: %dx%d.\n", + avctx->width, avctx->height); + return -1; + } + break; + + default: + break; + } + + return 0; +} + +static av_cold int ff_himpi_decode_end(AVCodecContext *avctx) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + int ret = 0; + int semvalue = 0; + struct timespec ts; + if (ctx == NULL || avctx->priv_data == NULL) { + av_log(ctx, AV_LOG_ERROR, "HiMpi decode end error, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } + + ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "Set Context failed, ret is %d.\n", ret); + return ret; + } + + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += 3; + if (sem_timedwait(&(ctx->eos_sema), &ts) == -1) { + semvalue = -1; + sem_getvalue(&ctx->eos_sema, &semvalue); + av_log(ctx, AV_LOG_ERROR, "Decode sem_timewait = -1, semvalue = %d.\n", semvalue); + } + // new + hi_video_frame_info frame; + hi_vdec_stream stream; + hi_vdec_supplement_info stSupplement; + ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); + if (ret != 0) { + FrameInfo_t frame_info; + memset(&frame_info, 0, sizeof(FrameInfo_t)); + frame_info.event_type = EVENT_EOS; + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + sem_post(&ctx->eos_sema); + av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); + return ret; + } + + size_t decResult = frame.v_frame.frame_flag; + +// hi_mpi_dvpp_free(stream.addr); +// if (ret != 0) { +// av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); +// return ret; +// } + if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { + hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); + return ret; + } + + if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { + ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); + return ((void*) (-1)); + } + return ret; + } + FrameInfo_t frame_info; + frame_info.ascend_ctx = ctx; + get_vdec_frame_info(&frame_info, frame); + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); + av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + + ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); + return ret; + } + + ctx->total_out_frame_count++; + // new + + if (ctx->hi_mpi_init_flag) { + ret = hi_mpi_vdec_stop_recv_stream(ctx->channel_id); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi stop receive stream failed, ret is %d.\n", ret); + return ret; + } + + ret = hi_mpi_vdec_destroy_chn(ctx->channel_id); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi destroy channel failed, ret is %d.\n", ret); + return ret; + } + + ret = hi_mpi_sys_exit(); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi sys exit failed, ret is %d.\n", ret); + return ret; + } + } + + ctx->hi_mpi_init_flag = 0; + ctx->decode_run_flag = 0; + +// if (ctx->thread_run_flag) { +// ctx->thread_run_flag = 0; +// pthread_join(ctx->thread_id, NULL); +// } + + sem_destroy(&ctx->eos_sema); + + if (ctx->frame_queue) { + av_fifo_freep(&ctx->frame_queue); + ctx->frame_queue = NULL; + } + + ff_mutex_destroy(&ctx->queue_mutex); + if (ctx->bsf) { + av_bsf_free(&ctx->bsf); + ctx->bsf = NULL; + } + + av_buffer_unref(&ctx->hw_frame_ref); + av_buffer_unref(&ctx->hw_device_ref); + + av_log(avctx, AV_LOG_INFO, "Decode hw send packet count is: %llu.\n", ctx->total_out_frame_count); + av_log(avctx, AV_LOG_INFO, "Decode hw out frame count is: %llu.\n", ctx->total_packet_count); + + return 0; +} + +static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + int ret = 0; + if (ctx->first_packet) { + if (avctx->extradata_size) { + uint8_t* streamBuffer = NULL; + ret = hi_mpi_dvpp_malloc(ctx->device_id, &streamBuffer, avctx->extradata_size); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi malloc first packet failed, ret is %d.\n", ret); + return ret; + } + ret = aclrtMemcpy(streamBuffer, avctx->extradata_size, avctx->extradata, avctx->extradata_size, + ACL_MEMCPY_HOST_TO_DEVICE); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy H2D first packet failed, ret is %d.\n", ret); + return ret; + } + + hi_vdec_stream stream; + stream.pts = avpkt->pts; + stream.addr = streamBuffer; + stream.len = avctx->extradata_size; + stream.end_of_frame = HI_TRUE; + stream.end_of_stream = HI_FALSE; + stream.need_display = HI_FALSE; + + hi_vdec_pic_info pic_info; + pic_info.vir_addr = 0; + pic_info.buffer_size = 0; + pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec send first packet failed, ret is %d.\n", ret); + return ret; + } + } + ctx->first_packet = 0; + } + + uint8_t* streamBuffer = NULL; + ret = hi_mpi_dvpp_malloc(ctx->device_id, &streamBuffer, avpkt->size); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi malloc packet failed, ret is %d.\n", ret); + return ret; + } + + ret = aclrtMemcpy(streamBuffer, avpkt->size, avpkt->data, avpkt->size, + ACL_MEMCPY_HOST_TO_DEVICE); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy H2D first packet failed, ret is %d.\n", ret); + return ret; + } + + // create stream info + hi_vdec_stream stream; + stream.pts = avpkt->pts; + stream.addr = streamBuffer; + stream.len = avpkt->size; + stream.end_of_frame = HI_TRUE; + stream.end_of_stream = HI_FALSE; + stream.need_display = HI_TRUE; + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_write(ctx->dts_queue, &avpkt->dts, sizeof(int64_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + + // create frame info + hi_vdec_pic_info pic_info; + pic_info.width = ctx->resize_width; // Output image width, supports resize, set 0 means no resize. + pic_info.height = ctx->resize_height; // Output image height, supports resize, set 0 means no resize. + pic_info.width_stride = FFALIGN(ctx->vdec_width, VDEC_WIDTH_ALIGN); + pic_info.height_stride = FFALIGN(ctx->vdec_height, VDEC_HEIGHT_ALIGN); + if (ctx->resize_str && ctx->resize_width != 0 && ctx->resize_height != 0) { + pic_info.width_stride = FFALIGN(ctx->resize_width, VDEC_WIDTH_ALIGN); + pic_info.height_stride = FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN); + } + uint32_t size = pic_info.width_stride * pic_info.height_stride * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; + + pic_info.buffer_size = size; + pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + void *picBuffer = NULL; + + ret = hi_mpi_dvpp_malloc(ctx->device_id, &picBuffer, size); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi malloc failed, ret is %d.\n", ret); + return ret; + } + pic_info.vir_addr = (uint64_t)picBuffer; + + do { + ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); + if ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL) { + usleep(VDEC_SLEEP_TIME); + } + } while ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL); + + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi send stream failed, ret is %d.\n", ret); + return ret; + } + + ctx->frame_id++; + ctx->total_packet_count++; + return 0; +} + +static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*) avctx->priv_data; + int ret = 0; + AVPacket packet = { 0 }; + AVPacket bsf_packet = { 0 }; + + hi_vdec_stream stream; + stream.addr = NULL; + stream.len = 0; + stream.end_of_frame = HI_FALSE; + stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. + stream.need_display = HI_TRUE; + + hi_vdec_pic_info pic_info; + pic_info.vir_addr = 0; + pic_info.buffer_size = 0; + pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); + if (ret != 0) { + av_packet_unref(avpkt); + av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); + return ret; + } + ctx->decoder_flushing = 1; + +// if (avpkt && avpkt->size && ctx->bsf) { +// ret = av_packet_ref(&packet, avpkt); +// if (ret < 0) { +// av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed, ret(%d).\n", ret); +// return ret; +// } +// ret = av_bsf_send_packet(ctx->bsf, &packet); +// if (ret < 0) { +// av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed, ret(%d).\n", ret); +// av_packet_unref(&packet); +// return ret; +// } +// ret = av_bsf_receive_packet(ctx->bsf, &bsf_packet); +// if (ret < 0) { +// av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed, ret(%d).\n", ret); +// return ret; +// } +// avpkt = &bsf_packet; +// } +// av_packet_unref(&packet); + +// if (avpkt && avpkt->size) { +// ret = malloc_and_send_frame(avctx, avpkt); +// if (ret != 0) { +// av_packet_unref(avpkt); +// return AVERROR(EINVAL); +// } +// } else { +// if (!ctx->decoder_flushing) { +// hi_vdec_stream stream; +// stream.addr = NULL; +// stream.len = 0; +// stream.end_of_frame = HI_FALSE; +// stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. +// stream.need_display = HI_TRUE; +// +// hi_vdec_pic_info pic_info; +// pic_info.vir_addr = 0; +// pic_info.buffer_size = 0; +// pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; +// ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); +// if (ret != 0) { +// av_packet_unref(avpkt); +// av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); +// return ret; +// } +// ctx->decoder_flushing = 1; +// } +// } + av_packet_unref(avpkt); + return 0; +} + +static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + int ret = 0; + if (!ctx->frame_queue) { + return AVERROR(EAGAIN); + } + + FrameInfo_t frame_info; + ff_mutex_lock(&ctx->queue_mutex); + if (av_fifo_size(ctx->frame_queue) != 0) { + av_fifo_generic_read(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + } else { + ff_mutex_unlock(&ctx->queue_mutex); + return AVERROR(EAGAIN); + } + ff_mutex_unlock(&ctx->queue_mutex); + + if (frame_info.event_type == EVENT_EOS) { + return AVERROR_EOF; + } + + if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { + ret = av_hwframe_get_buffer(ctx->hw_frame_ref, frame, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed, ret is %d.\n", ret); + return AVERROR(EINVAL); + } + ret = ff_decode_frame_props(avctx, frame); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed, ret is %d.\n", ret); + return AVERROR(EINVAL); + } + } else { + ret = ff_get_buffer(avctx, frame, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Decode ff_get_buffer failed, ret is %d.\n", ret); + return AVERROR(EINVAL); + } + } + + frame->pkt_pos = -1; + frame->pkt_duration = 0; + frame->pkt_size = -1; + frame->pts = frame_info.pts; + frame->pkt_pts = frame->pts; + frame->pkt_dts = frame_info.dts; + frame->width = frame_info.width_stride; + frame->height = frame_info.height_stride; + + switch (frame_info.format) { + case HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420: + if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { + uint32_t offset = 0; + for (int i = 0; i < 2; i++) { + size_t dstBytes = frame->width * frame->height * (i ? 1.0 / 2 : 1); + ret = aclrtMemcpy(frame->data[i], dstBytes, frame_info.data + offset, dstBytes, + ACL_MEMCPY_DEVICE_TO_DEVICE); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy D2D failed, ret is %d.\n", ret); + hi_mpi_dvpp_free(frame_info.data); + return ret; + } + offset += dstBytes; + } + } else { + uint32_t offset = 0; + for (int i = 0; i < 2; i++) { + size_t dstBytes = frame->width * frame->height * (i ? 1.0 / 2 : 1); + ret = aclrtMemcpy(frame->data[i], dstBytes, frame_info.data + offset, dstBytes, + ACL_MEMCPY_DEVICE_TO_HOST); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy D2H failed, ret is %d.\n", ret); + hi_mpi_dvpp_free(frame_info.data); + return ret; + } + offset += dstBytes; + } + } + ret = hi_mpi_dvpp_free(frame_info.data); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi free data failed, ret is %d.\n", ret); + } + break; + + default: + hi_mpi_dvpp_free(frame_info.data); + av_log(avctx, AV_LOG_ERROR, "Unsupport pixfmt: %d.\n", (int)frame_info.format); + break; + } + + + return 0; +} + +static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + AVPacket pkt = { 0 }; + int send_ret = -1; + int get_ret = -1; + int ret = 0; + + if (avctx == NULL || avctx->priv_data == NULL) { + av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } +// if (!ctx->hi_mpi_init_flag || !ctx->thread_run_flag || !ctx->decode_run_flag) { +// av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); +// return AVERROR_BUG; +// } + + if (!ctx->hi_mpi_init_flag || !ctx->decode_run_flag) { + av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } + + if (ctx->eos_received) { + return AVERROR_EOF; + } + + ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); + return ret; + } + + while (ctx->decode_run_flag) { + if (!ctx->decoder_flushing) { + send_ret = ff_decode_get_packet(avctx, &pkt); + if (send_ret < 0 && send_ret != AVERROR_EOF) { + return send_ret; + } + send_ret = hi_mpi_decode(avctx, &pkt); + av_packet_unref(&pkt); + if (send_ret < 0 && send_ret != AVERROR_EOF) { + av_log(ctx, AV_LOG_ERROR, "Send packet failed, ret is %d.\n", send_ret); + return send_ret; + } + } + + get_ret = himpi_get_frame(avctx, frame); + if (get_ret != 0 && get_ret != AVERROR_EOF) { + if (get_ret != AVERROR(EAGAIN)) { + return get_ret; + } + if (ctx->decoder_flushing) { + av_usleep(2000); + } + } else { + if (get_ret == AVERROR_EOF) { + ctx->eos_received = 1; + } + return get_ret; + } + } + + av_log(avctx, AV_LOG_ERROR, "Decode stop, error.\n"); + return AVERROR_BUG; + +} + +static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + AVASCENDDeviceContext *hw_device_ctx; + AVHWFramesContext *hw_frame_ctx; + const AVBitStreamFilter *bsf; + int ret = 0; + + if (avctx == NULL || avctx->priv_data == NULL) { + av_log(avctx, AV_LOG_ERROR, "HiMpi decoder init failed, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } + + if (ctx->hi_mpi_init_flag == 1) { + av_log(avctx, AV_LOG_ERROR, "Error, himpi decode double init. \n"); + return AVERROR_BUG; + } + + enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_ASCEND, AV_PIX_FMT_NV12, AV_PIX_FMT_NONE }; + avctx->pix_fmt = ff_get_format(avctx, pix_fmts); + if (avctx->pix_fmt < 0) { + av_log(avctx, AV_LOG_ERROR, "Error, ff_get_format failed with format id: %d.\n", avctx->pix_fmt); + return AVERROR_BUG; + } + + ctx->avctx = avctx; + + if (ctx->resize_str) { + ret = av_parse_video_size(&ctx->resize_width, &ctx->resize_height, ctx->resize_str); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be {width}x{height}.\n", + ctx->resize_str); + return AVERROR_BUG; + } + + if (ctx->resize_width != FFALIGN(ctx->resize_width, VDEC_WIDTH_ALIGN) || + ctx->resize_height != FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN)) { + av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be stride by %d and %d.\n", + ctx->resize_str, VDEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); + return AVERROR_BUG; + } + + if (ctx->resize_width < 128 || ctx->resize_height < 128 || + ctx->resize_width > 4096 || ctx->resize_height > 4096) { + av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be in [128x128 ~ 4096x4096].\n", + ctx->resize_str, VDEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); + return AVERROR_BUG; + } + avctx->coded_width = ctx->resize_width; + avctx->coded_height = ctx->resize_height; + } + + if (!ctx->resize_str || (ctx->resize_height == avctx->height && ctx->resize_width == avctx->width)) { + ctx->vdec_width = FFALIGN(avctx->width, VDEC_WIDTH_ALIGN); + ctx->vdec_height = FFALIGN(avctx->height, VDEC_HEIGHT_ALIGN); + ctx->resize_width = ctx->resize_height = 0; + } else { + av_log(avctx, AV_LOG_INFO, "Vdec resize: %dx%d.\n", ctx->resize_width, ctx->resize_height); + } + + ctx->vdec_width = avctx->width; + ctx->vdec_height = avctx->height; + + if (decode_params_checking(avctx) != 0) { + return AVERROR(EINVAL); + } + av_log(avctx, AV_LOG_DEBUG, "Vdec width: %d.\n", ctx->vdec_width); + av_log(avctx, AV_LOG_DEBUG, "Vdec height: %d.\n", ctx->vdec_height); + + if (avctx->hw_frames_ctx) { + av_buffer_unref(&ctx->hw_frame_ref); + ctx->hw_frame_ref = av_buffer_ref(avctx->hw_frames_ctx); + if (!ctx->hw_frame_ref) { + ret = AVERROR(EINVAL); + goto error; + } + + hw_frame_ctx = (AVHWFramesContext*)ctx->hw_frame_ref->data; + if (!hw_frame_ctx->pool || + (ctx->vdec_width != hw_frame_ctx->width && ctx->resize_width != hw_frame_ctx->width)) { + if (hw_frame_ctx->pool) { + av_buffer_pool_uninit(&hw_frame_ctx->pool); + } + hw_frame_ctx->width = ctx->resize_width == 0 ? ctx->vdec_width : ctx->resize_width; + hw_frame_ctx->height = ctx->resize_height == 0 ? ctx->vdec_height : ctx->resize_height; + hw_frame_ctx->initial_pool_size = 2; + hw_frame_ctx->format = AV_PIX_FMT_ASCEND; + hw_frame_ctx->sw_format = avctx->sw_pix_fmt; + + ret = av_hwframe_ctx_init(ctx->hw_frame_ref); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "HWFrame contex init failed.\n"); + return AVERROR(ENAVAIL); + } + } + ctx->hw_device_ref = av_buffer_ref(hw_frame_ctx->device_ref); + if (!ctx->hw_device_ref) { + av_log(avctx, AV_LOG_ERROR, "Get hw_device_ref failed.\n"); + ret = AVERROR(EINVAL); + goto error; + } + } else { + if (avctx->hw_device_ctx) { + ctx->hw_device_ref = av_buffer_ref(avctx->hw_device_ctx); + if (!ctx->hw_device_ref) { + av_log(avctx, AV_LOG_ERROR, "ref hwdevice failed.\n"); + ret = AVERROR(EINVAL); + goto error; + } + } else { + char dev_idx[sizeof(int)]; + sprintf(dev_idx, "%d", ctx->device_id); + av_log(avctx, AV_LOG_INFO, "dev_idx: %s.\n", dev_idx); + ret = av_hwdevice_ctx_create(&ctx->hw_device_ref, AV_HWDEVICE_TYPE_ASCEND, dev_idx, NULL, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "hwdevice contex create failed.\n"); + goto error; + } + } + ctx->hw_frame_ref = av_hwframe_ctx_alloc(ctx->hw_device_ref); + if (!ctx->hw_frame_ref) { + av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed, ret is %d.\n", ret); + ret = AVERROR(EINVAL); + goto error; + } + hw_frame_ctx = (AVHWFramesContext*)ctx->hw_frame_ref->data; + if (!hw_frame_ctx->pool) { + hw_frame_ctx->width = ctx->resize_width == 0 ? ctx->vdec_width : ctx->resize_width; + hw_frame_ctx->height = ctx->resize_height == 0 ? ctx->vdec_height : ctx->resize_height; + hw_frame_ctx->initial_pool_size = 2; + hw_frame_ctx->format = AV_PIX_FMT_ASCEND; + hw_frame_ctx->sw_format = avctx->sw_pix_fmt; + ret = av_hwframe_ctx_init(ctx->hw_frame_ref); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "hwframe ctx init error, ret is %d.\n", ret); + return AVERROR(EINVAL); + } + } + } + hw_device_ctx = ((AVHWDeviceContext*)ctx->hw_device_ref->data)->hwctx; + ctx->hw_device_ctx = hw_device_ctx; + ctx->hw_frames_ctx = hw_frame_ctx; + ctx->ascend_ctx = ctx->hw_device_ctx->ascend_ctx; + + ctx->device_id = ctx->ascend_ctx->device_id; + ctx->frame_id = 0; + ctx->eos_received = 0; + ctx->total_out_frame_count = 0; + ctx->total_packet_count = 0; + ctx->decoder_flushing = 0; + ctx->first_packet = 1; + + ff_mutex_init(&ctx->queue_mutex, NULL); + + switch (avctx->codec->id) + { + case AV_CODEC_ID_H264: + ctx->codec_type = HI_PT_H264; + break; + case AV_CODEC_ID_H265: + ctx->codec_type = HI_PT_H265; + break; + default: + av_log(avctx, AV_LOG_ERROR, "Invalid codec type, %d.\n", avctx->codec->id); + return AVERROR_BUG; + } +// ctx->bsf = NULL; +// if (avctx->codec->id == AV_CODEC_ID_H264 || avctx->codec->id == AV_CODEC_ID_H265) { +// if (avctx->codec->id == AV_CODEC_ID_H264) +// bsf = av_bsf_get_by_name("h264_mp4toannexb"); +// else if (avctx->codec->id == AV_CODEC_ID_H265) +// bsf = av_bsf_get_by_name("hevc_mp4toannexb"); +// if (!bsf) { +// ret = AVERROR_BSF_NOT_FOUND; +// goto error; +// } +// ret = av_bsf_alloc(bsf, &ctx->bsf); +// if (ret < 0) +// goto error; +// +// ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx); +// if (ret < 0) { +// av_bsf_free(&ctx->bsf); +// goto error; +// } +// ret = av_bsf_init(ctx->bsf); +// if (ret < 0) { +// av_bsf_free(&ctx->bsf); +// goto error; +// } +// } else { +// av_log(avctx, AV_LOG_ERROR, "Invalid codec id, %d.\n", avctx->codec->id); +// return AVERROR_BUG; +// } + + ctx->frame_queue = av_fifo_alloc(1000 * sizeof(FrameInfo_t)); + if (!ctx->frame_queue) { + av_log(avctx, AV_LOG_ERROR, "Failed to alloc frame fifo queue.\n"); + goto error; + } + + ctx->dts_queue = av_fifo_alloc(1000 * sizeof(int64_t)); + if (!ctx->dts_queue) { + av_log(avctx, AV_LOG_ERROR, "Failed to alloc dts fifo queue.\n"); + goto error; + } + + ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); + goto error; + } + + ret = hi_mpi_sys_init(); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi sys init failed, ret is %d.\n", ret); + goto error; + } + + ctx->chn_attr_.type = ctx->codec_type; + ctx->chn_attr_.mode = HI_VDEC_SEND_MODE_FRAME; + ctx->chn_attr_.pic_width = ctx->vdec_width; + ctx->chn_attr_.pic_height = ctx->vdec_height; + + // Stream buffer size, Recommended value is width * height * 3 / 2 + ctx->chn_attr_.stream_buf_size = ctx->vdec_width * ctx->vdec_height * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; + ctx->chn_attr_.frame_buf_cnt = REF_FRAME_NUM + DISPLAY_FRAME_NUM + 1; + + // Create buf attribute + ctx->buf_attr_.width = ctx->chn_attr_.pic_width; + ctx->buf_attr_.height = ctx->chn_attr_.pic_height; + ctx->buf_attr_.align = 0; + ctx->buf_attr_.bit_width = HI_DATA_BIT_WIDTH_8; + ctx->buf_attr_.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + ctx->buf_attr_.compress_mode = HI_COMPRESS_MODE_NONE; + + ctx->chn_attr_.frame_buf_size = hi_vdec_get_pic_buf_size(ctx->chn_attr_.type, &ctx->buf_attr_); + + // Configure video decoder channel attribute + ctx->chn_attr_.video_attr.ref_frame_num = REF_FRAME_NUM; + ctx->chn_attr_.video_attr.temporal_mvp_en = HI_TRUE; + ctx->chn_attr_.video_attr.tmv_buf_size = hi_vdec_get_tmv_buf_size(ctx->chn_attr_.type, + ctx->chn_attr_.pic_width, + ctx->chn_attr_.pic_height); + + av_log(avctx, AV_LOG_INFO, "Channel Id is: %d.\n", ctx->channel_id); + ret = hi_mpi_vdec_create_chn(ctx->channel_id, &ctx->chn_attr_); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi create vdec channel failed, ret is %d.\n", ret); + goto error; + } + + // reset channel param. + ret = hi_mpi_vdec_get_chn_param(ctx->channel_id, &ctx->chn_param_); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec get channel param failed, ret is %d.\n", ret); + goto error; + } + + ctx->chn_param_.video_param.dec_mode = HI_VIDEO_DEC_MODE_IPB; + ctx->chn_param_.video_param.compress_mode = HI_COMPRESS_MODE_HFBC; + ctx->chn_param_.video_param.video_format = HI_VIDEO_FORMAT_TILE_64x16; + ctx->chn_param_.display_frame_num = DISPLAY_FRAME_NUM; + ctx->chn_param_.video_param.out_order = HI_VIDEO_OUT_ORDER_DISPLAY; + + ret = hi_mpi_vdec_set_chn_param(ctx->channel_id, &ctx->chn_param_); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec set channel param failed, ret is %d.\n", ret); + goto error; + } + + ret = hi_mpi_vdec_start_recv_stream(ctx->channel_id); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec start receive stream failed, ret is %d.\n", ret); + goto error; + } + ctx->hi_mpi_init_flag = 1; + ctx->decode_run_flag = 1; + + // create callback thread +// ctx->thread_run_flag = 1; +// ret = pthread_create(&ctx->thread_id, NULL, get_frame, (void *)ctx); +// if (ret != 0) { +// av_log(avctx, AV_LOG_ERROR, "pthread_create callback thread failed, ret is %d.\n", ret); +// goto error; +// } + + avctx->pkt_timebase.num = 1; + avctx->pkt_timebase.den = 90000; + if (!avctx->pkt_timebase.num || !avctx->pkt_timebase.den) { + av_log(avctx, AV_LOG_ERROR, "Invalid pkt_timebase.\n"); + } + + sem_init(&ctx->eos_sema, 0, 0); + return 0; + +error: + sem_post(&ctx->eos_sema); + ff_himpi_decode_end(avctx); + return ret; +} + +static void ff_himpi_flush(AVCodecContext *avctx) { + ff_himpi_decode_end(avctx); + ff_himpi_decode_init(avctx); +} + +#define OFFSET(x) offsetof(ASCENDContext_t, x) +#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM +static const AVOption options[] = { + { "device_id", "Use to choose the ascend chip.", OFFSET(device_id), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 8, VD}, + { "channel_id", "Set channelId of decoder.", OFFSET(channel_id), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 255, VD}, + { "resize", "Resize (width)x(height).", OFFSET(resize_str), AV_OPT_TYPE_STRING, { .str = NULL}, 0, 0, VD}, + { NULL } +}; + +static const AVCodecHWConfigInternal* ascend_hw_configs[] = { + &(const AVCodecHWConfigInternal) { + .public = { + .pix_fmt = AV_PIX_FMT_ASCEND, + .methods = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX | AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX | \ + AV_CODEC_HW_CONFIG_METHOD_INTERNAL, + .device_type = AV_HWDEVICE_TYPE_ASCEND + }, + .hwaccel = NULL, + }, + NULL +}; + +#define ASCEND_JPEG_DEC_CODEC() \ + static const AVClass jpeg_ascend_class = { \ + .class_name = "jpeg_ascend_dec", \ + .item_name = av_default_item_name, \ + .option = options, \ + .version = LIBAVUTIL_VERSION_INT, \ + }; \ + AVCodec ff_jpeg_ascend_decoder = { \ + .name = "jpeg_ascend", \ + .long_name = NULL_IF_CONFIG_SMALL("Ascend HiMpi jpeg decoder"), \ + .type = AVMEDIA_TYPE_VIDEO, \ + .id = AV_CODEC_ID_JPEG2000, \ + .priv_data_size = sizeof(ASCENDContext_t), \ + .priv_class = &jpeg_ascend_class, \ + .init = ff_himpi_decode_init, \ + .close = ff_himpi_decode_end, \ + .receive_frame = ff_himpi_receive_frame, \ + .flush = ff_himpi_flush, \ + .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \ + + + .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_ASCEND, \ + AV_PIX_FMT_NV12, \ + AV_PIX_FMT_NONE }, \ + .hw_configs = ascend_hw_configs, \ + .wrapper_name = "ascendjpegdec", \ + }; + +#if CONFIG_JPEG_ASCEND_DECODER +ASCEND_JPEG_DEC_CODEC() +#endif \ No newline at end of file diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h new file mode 100644 index 000000000..ea47d7a75 --- /dev/null +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h @@ -0,0 +1,146 @@ +/* + * Copyright(c) 2020. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except int compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FFMPEG_ASCEND_ASCEND_DEC_H +#define FFMPEG_ASCEND_ASCEND_DEC_H + +#include "libavutil/parseutils.h" +#include "libavutil/buffer.h" +#include "libavutil/mathematics.h" +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_ascend.h" +#include "libavutil/fifo.h" +#include "libavutil/log.h" +#include "libavutil/opt.h" +#include "libavutil/time.h" +#include "libavutil/common.h" +#include "libavutil/pixdesc.h" +#include "libavutil/thread.h" +#include "libavutil/version.h" +#include "config.h" +#include "avcodec.h" +#include "decode.h" +#include "hwaccels.h" +#include "hwconfig.h" +#include "internal.h" +#include "libavutil/avutil.h" + +#include "acl/dvpp/hi_dvpp.h" + +#define VDEC_WIDTH_ALIGN 16 +#define VDEC_HEIGHT_ALIGN 2 + +#define REF_FRAME_NUM 8 +#define DISPLAY_FRAME_NUM 2 +#define VDEC_TIME_OUT 1000 +#define VDEC_GET_TIME_OUT 100 +#define VDEC_SLEEP_TIME 1000 + +#define YUV_BGR_CONVERT_3 3 +#define YUV_BGR_CONVERT_2 2 + +typedef enum { + EVENT_NEW_FRAME = 0, + EVENT_EOS = 1, +} eventType_t; + +typedef struct FrameInfo { + void *ascend_ctx; + eventType_t event_type; + uint8_t *data; + uint32_t data_size; + hi_pixel_format format; + int64_t pts; + int64_t dts; + uint32_t width_stride; + uint32_t height_stride; +} FrameInfo_t; + +typedef struct ASCENDJPEGContext { + AVClass* av_class; + int device_id; + int channel_id; + + volatile int hi_mpi_init_flag; + + /* + struct { + int x; + int y; + int w; + int h; + } crop; + struct { + int width; + int height; + } resize; + */ + + char *output_pixfmt; + sem_t eos_sema; + + AVBufferRef *hw_device_ref; + AVBufferRef *hw_frame_ref; + AVBSFContext *bsf; + AVCodecContext *avctx; + AVFifoBuffer *frame_queue; + AVFifoBuffer *dts_queue; + AVASCENDDeviceContext *hw_device_ctx; + AVHWFramesContext *hw_frames_ctx; + AscendContext *ascend_ctx; + + hi_vdec_chn_attr chn_attr_; + hi_pic_buf_attr buf_attr_; + hi_vdec_chn_param chn_param_; + + AVMutex queue_mutex; + + int max_width; + int max_height; + int vdec_width; + int vdec_height; + int stride_align; + char* resize_str; + int resize_width; + int resize_height; + hi_payload_type codec_type; + + volatile int frame_id; + int first_packet; + volatile int first_seq; + volatile int eos_received; + volatile int decoder_flushing; + volatile int decode_run_flag; + unsigned long long total_packet_count; + unsigned long long total_out_frame_count; + + hi_vdec_stream stream; + hi_vdec_pic_info pic_info; +} ASCENDContext_t; + +static inline void get_vdec_frame_info(FrameInfo_t* frame_info, hi_video_frame_info frame) +{ + uint32_t width_stride = frame.v_frame.width_stride[0]; + uint32_t height_stride = frame.v_frame.height_stride[0]; + frame_info->width_stride = width_stride; + frame_info->height_stride = height_stride; + frame_info->data_size = width_stride * height_stride * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; + frame_info->format = frame.v_frame.pixel_format; + frame_info->pts = frame.v_frame.pts; + frame_info->data = frame.v_frame.virt_addr[0]; +} + +#endif // FFMPEG_ASCEND_ASCEND_DEC_H \ No newline at end of file -- Gitee From 90f77874590cbbaf117efa78698857a05a9b8512 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Mon, 2 Sep 2024 20:09:14 +0800 Subject: [PATCH 02/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 35fb89dd2..89d00e433 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -930,27 +930,55 @@ static const AVCodecHWConfigInternal* ascend_hw_configs[] = { NULL }; -#define ASCEND_JPEG_DEC_CODEC() \ - static const AVClass jpeg_ascend_class = { \ - .class_name = "jpeg_ascend_dec", \ +//#define ASCEND_JPEG_DEC_CODEC() \ +// static const AVClass jpeg_ascend_class = { \ +// .class_name = "jpeg_ascend_dec", \ +// .item_name = av_default_item_name, \ +// .option = options, \ +// .version = LIBAVUTIL_VERSION_INT, \ +// }; \ +// AVCodec ff_jpeg_ascend_decoder = { \ +// .name = "jpeg_ascend", \ +// .long_name = NULL_IF_CONFIG_SMALL("Ascend HiMpi jpeg decoder"), \ +// .type = AVMEDIA_TYPE_VIDEO, \ +// .id = AV_CODEC_ID_JPEG2000, \ +// .priv_data_size = sizeof(ASCENDContext_t), \ +// .priv_class = &jpeg_ascend_class, \ +// .init = ff_himpi_decode_init, \ +// .close = ff_himpi_decode_end, \ +// .receive_frame = ff_himpi_receive_frame, \ +// .flush = ff_himpi_flush, \ +// .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \ +// .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_ASCEND, \ +// AV_PIX_FMT_NV12, \ +// AV_PIX_FMT_NONE }, \ +// .hw_configs = ascend_hw_configs, \ +// .wrapper_name = "ascendjpegdec", \ +// }; +// +//#if CONFIG_JPEG_ASCEND_DECODER +//ASCEND_JPEG_DEC_CODEC() +//#endif + +#define ASCEND_JPEG_DEC_CODEC(x, X) \ + static const AVClass x##_ascend_class = { \ + .class_name = #x "_ascend_dec", \ .item_name = av_default_item_name, \ .option = options, \ .version = LIBAVUTIL_VERSION_INT, \ }; \ - AVCodec ff_jpeg_ascend_decoder = { \ - .name = "jpeg_ascend", \ - .long_name = NULL_IF_CONFIG_SMALL("Ascend HiMpi jpeg decoder"), \ + AVCodec ff_##x##_ascend_decoder = { \ + .name = #x "_ascend", \ + .long_name = NULL_IF_CONFIG_SMALL("Ascend HiMpi " #X " decoder"), \ .type = AVMEDIA_TYPE_VIDEO, \ - .id = AV_CODEC_ID_JPEG2000, \ + .id = AV_CODEC_ID_##X, \ .priv_data_size = sizeof(ASCENDContext_t), \ - .priv_class = &jpeg_ascend_class, \ + .priv_class = &x##_ascend_class, \ .init = ff_himpi_decode_init, \ .close = ff_himpi_decode_end, \ .receive_frame = ff_himpi_receive_frame, \ .flush = ff_himpi_flush, \ .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \ - - .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_ASCEND, \ AV_PIX_FMT_NV12, \ AV_PIX_FMT_NONE }, \ @@ -959,5 +987,5 @@ static const AVCodecHWConfigInternal* ascend_hw_configs[] = { }; #if CONFIG_JPEG_ASCEND_DECODER -ASCEND_JPEG_DEC_CODEC() -#endif \ No newline at end of file +ASCEND_JPEG_DEC_CODEC(jpeg, JPEG) +#endif -- Gitee From e88e56108942212ce03527ca5aa39a3e42d220be Mon Sep 17 00:00:00 2001 From: Malanchi Date: Mon, 2 Sep 2024 20:14:39 +0800 Subject: [PATCH 03/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 89d00e433..fa791dd03 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -971,7 +971,7 @@ static const AVCodecHWConfigInternal* ascend_hw_configs[] = { .name = #x "_ascend", \ .long_name = NULL_IF_CONFIG_SMALL("Ascend HiMpi " #X " decoder"), \ .type = AVMEDIA_TYPE_VIDEO, \ - .id = AV_CODEC_ID_##X, \ + .id = AV_CODEC_ID_JPEG2000, \ .priv_data_size = sizeof(ASCENDContext_t), \ .priv_class = &x##_ascend_class, \ .init = ff_himpi_decode_init, \ -- Gitee From a90c143b47a512f9dccdceb9587b00fafa62e885 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Mon, 2 Sep 2024 20:19:28 +0800 Subject: [PATCH 04/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 158 +++++++++--------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index fa791dd03..efd15340a 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -22,85 +22,85 @@ #include #include "ascend_jpeg_dec.h" -static void *get_frame(void *arg) -{ - ASCENDContext_t *ctx = (ASCENDContext_t*)arg; - int ret = 0; - int eos_flag = 0; - ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); - return ((void*) (-1)); - } - - hi_video_frame_info frame; - hi_vdec_stream stream; - hi_vdec_supplement_info stSupplement; - - av_log(NULL, AV_LOG_INFO, "Thread start.\n"); - - while (ctx->thread_run_flag) { - ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); - if (ret != 0) { - if (ctx->decoder_flushing && ret == HI_ERR_VDEC_BUF_EMPTY) { - eos_flag = 1; - av_log(ctx, AV_LOG_DEBUG, "Decoder flushing or stream eos.\n"); - } else { - av_log(ctx, AV_LOG_DEBUG, "HiMpi get frame failed, ret is %d.\n", ret); - continue; - } - } - - size_t decResult = frame.v_frame.frame_flag; - if (eos_flag) { - // eos - FrameInfo_t frame_info; - memset(&frame_info, 0, sizeof(FrameInfo_t)); - frame_info.event_type = EVENT_EOS; - - ff_mutex_lock(&ctx->queue_mutex); - av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); - ff_mutex_unlock(&ctx->queue_mutex); - sem_post(&ctx->eos_sema); - av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); - break; - } - - hi_mpi_dvpp_free(stream.addr); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); - } - if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { - hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); - } - - if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { - ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); - return ((void*) (-1)); - } - continue; - } - FrameInfo_t frame_info; - frame_info.ascend_ctx = ctx; - get_vdec_frame_info(&frame_info, frame); - - ff_mutex_lock(&ctx->queue_mutex); - av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); - av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); - ff_mutex_unlock(&ctx->queue_mutex); - - ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); - return ((void*) (-1)); - } - - ctx->total_out_frame_count++; - } - return NULL; -} +//static void *get_frame(void *arg) +//{ +// ASCENDContext_t *ctx = (ASCENDContext_t*)arg; +// int ret = 0; +// int eos_flag = 0; +// ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); +// if (ret != 0) { +// av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); +// return ((void*) (-1)); +// } +// +// hi_video_frame_info frame; +// hi_vdec_stream stream; +// hi_vdec_supplement_info stSupplement; +// +// av_log(NULL, AV_LOG_INFO, "Thread start.\n"); +// +// while (ctx->thread_run_flag) { +// ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); +// if (ret != 0) { +// if (ctx->decoder_flushing && ret == HI_ERR_VDEC_BUF_EMPTY) { +// eos_flag = 1; +// av_log(ctx, AV_LOG_DEBUG, "Decoder flushing or stream eos.\n"); +// } else { +// av_log(ctx, AV_LOG_DEBUG, "HiMpi get frame failed, ret is %d.\n", ret); +// continue; +// } +// } +// +// size_t decResult = frame.v_frame.frame_flag; +// if (eos_flag) { +// // eos +// FrameInfo_t frame_info; +// memset(&frame_info, 0, sizeof(FrameInfo_t)); +// frame_info.event_type = EVENT_EOS; +// +// ff_mutex_lock(&ctx->queue_mutex); +// av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); +// ff_mutex_unlock(&ctx->queue_mutex); +// sem_post(&ctx->eos_sema); +// av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); +// break; +// } +// +// hi_mpi_dvpp_free(stream.addr); +// if (ret != 0) { +// av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); +// } +// if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { +// hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); +// } +// +// if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { +// ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); +// if (ret != 0) { +// av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); +// return ((void*) (-1)); +// } +// continue; +// } +// FrameInfo_t frame_info; +// frame_info.ascend_ctx = ctx; +// get_vdec_frame_info(&frame_info, frame); +// +// ff_mutex_lock(&ctx->queue_mutex); +// av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); +// av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); +// ff_mutex_unlock(&ctx->queue_mutex); +// +// ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); +// if (ret != 0) { +// av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); +// return ((void*) (-1)); +// } +// +// ctx->total_out_frame_count++; +// } +// return NULL; +//} static inline int decode_params_checking(AVCodecContext* avctx) { -- Gitee From 7c55d636e30125ee8f1de5fa12168a3020fb05e7 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 09:51:59 +0800 Subject: [PATCH 05/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 99 ++++++++++++------- 1 file changed, 66 insertions(+), 33 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index efd15340a..8fad43da4 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -105,8 +105,7 @@ static inline int decode_params_checking(AVCodecContext* avctx) { switch (avctx->codec->id) { - case AV_CODEC_ID_H264: - case AV_CODEC_ID_H265: + case AV_CODEC_ID_JPEG2000: if (avctx->width < 128 || avctx->height < 128 || avctx->width > 4096 || avctx->height > 4096) { av_log(avctx, AV_LOG_ERROR, @@ -125,6 +124,7 @@ static inline int decode_params_checking(AVCodecContext* avctx) static av_cold int ff_himpi_decode_end(AVCodecContext *avctx) { + av_log(ctx, AV_LOG_ERROR, "here is decode end 0.\n"); ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; int ret = 0; int semvalue = 0; @@ -249,7 +249,6 @@ static av_cold int ff_himpi_decode_end(AVCodecContext *avctx) av_log(avctx, AV_LOG_INFO, "Decode hw send packet count is: %llu.\n", ctx->total_out_frame_count); av_log(avctx, AV_LOG_INFO, "Decode hw out frame count is: %llu.\n", ctx->total_packet_count); - return 0; } @@ -536,6 +535,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) { + av_log(ctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 0.\n"); ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; AVPacket pkt = { 0 }; int send_ret = -1; @@ -566,9 +566,9 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return ret; } - while (ctx->decode_run_flag) { - if (!ctx->decoder_flushing) { - send_ret = ff_decode_get_packet(avctx, &pkt); + // new + + send_ret = ff_decode_get_packet(avctx, &pkt); if (send_ret < 0 && send_ret != AVERROR_EOF) { return send_ret; } @@ -578,26 +578,57 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) av_log(ctx, AV_LOG_ERROR, "Send packet failed, ret is %d.\n", send_ret); return send_ret; } - } - get_ret = himpi_get_frame(avctx, frame); - if (get_ret != 0 && get_ret != AVERROR_EOF) { - if (get_ret != AVERROR(EAGAIN)) { - return get_ret; - } - if (ctx->decoder_flushing) { - av_usleep(2000); - } - } else { - if (get_ret == AVERROR_EOF) { - ctx->eos_received = 1; - } + get_ret = himpi_get_frame(avctx, frame); + av_log(ctx, AV_LOG_ERROR, "get ret is %d.\n", get_ret); + av_log(ctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 1.\n"); + if (get_ret != 0 && get_ret != AVERROR_EOF) { + if (get_ret != AVERROR(EAGAIN)) { return get_ret; } + if (ctx->decoder_flushing) { + av_usleep(2000); + } + } else { + if (get_ret == AVERROR_EOF) { + ctx->eos_received = 1; + } + return get_ret; } - av_log(avctx, AV_LOG_ERROR, "Decode stop, error.\n"); - return AVERROR_BUG; + return get_ret; +// while (ctx->decode_run_flag) { +// if (!ctx->decoder_flushing) { +// send_ret = ff_decode_get_packet(avctx, &pkt); +// if (send_ret < 0 && send_ret != AVERROR_EOF) { +// return send_ret; +// } +// send_ret = hi_mpi_decode(avctx, &pkt); +// av_packet_unref(&pkt); +// if (send_ret < 0 && send_ret != AVERROR_EOF) { +// av_log(ctx, AV_LOG_ERROR, "Send packet failed, ret is %d.\n", send_ret); +// return send_ret; +// } +// } +// +// get_ret = himpi_get_frame(avctx, frame); +// if (get_ret != 0 && get_ret != AVERROR_EOF) { +// if (get_ret != AVERROR(EAGAIN)) { +// return get_ret; +// } +// if (ctx->decoder_flushing) { +// av_usleep(2000); +// } +// } else { +// if (get_ret == AVERROR_EOF) { +// ctx->eos_received = 1; +// } +// return get_ret; +// } +// } +// +// av_log(avctx, AV_LOG_ERROR, "Decode stop, error.\n"); +// return AVERROR_BUG; } @@ -621,6 +652,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_ASCEND, AV_PIX_FMT_NV12, AV_PIX_FMT_NONE }; avctx->pix_fmt = ff_get_format(avctx, pix_fmts); + av_log(avctx, AV_LOG_ERROR, "INFO, ff_get_format failed with format id: %d.\n", avctx->pix_fmt); if (avctx->pix_fmt < 0) { av_log(avctx, AV_LOG_ERROR, "Error, ff_get_format failed with format id: %d.\n", avctx->pix_fmt); return AVERROR_BUG; @@ -754,19 +786,20 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) ctx->first_packet = 1; ff_mutex_init(&ctx->queue_mutex, NULL); + ctx->codec_type = HI_PT_JPEG; - switch (avctx->codec->id) - { - case AV_CODEC_ID_H264: - ctx->codec_type = HI_PT_H264; - break; - case AV_CODEC_ID_H265: - ctx->codec_type = HI_PT_H265; - break; - default: - av_log(avctx, AV_LOG_ERROR, "Invalid codec type, %d.\n", avctx->codec->id); - return AVERROR_BUG; - } +// switch (avctx->codec->id) +// { +// case AV_CODEC_ID_H264: +// ctx->codec_type = HI_PT_H264; +// break; +// case AV_CODEC_ID_H265: +// ctx->codec_type = HI_PT_H265; +// break; +// default: +// av_log(avctx, AV_LOG_ERROR, "Invalid codec type, %d.\n", avctx->codec->id); +// return AVERROR_BUG; +// } // ctx->bsf = NULL; // if (avctx->codec->id == AV_CODEC_ID_H264 || avctx->codec->id == AV_CODEC_ID_H265) { // if (avctx->codec->id == AV_CODEC_ID_H264) -- Gitee From e9c9dae03319f60be415237dd91da9ae3d15d211 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 10:07:25 +0800 Subject: [PATCH 06/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 8fad43da4..13b64491f 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -535,7 +535,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) { - av_log(ctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 0.\n"); + av_log(avctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 0.\n"); ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; AVPacket pkt = { 0 }; int send_ret = -1; @@ -580,8 +580,8 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } get_ret = himpi_get_frame(avctx, frame); - av_log(ctx, AV_LOG_ERROR, "get ret is %d.\n", get_ret); - av_log(ctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 1.\n"); + av_log(avctx, AV_LOG_ERROR, "get ret is %d.\n", get_ret); + av_log(avctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 1.\n"); if (get_ret != 0 && get_ret != AVERROR_EOF) { if (get_ret != AVERROR(EAGAIN)) { return get_ret; -- Gitee From 7a122e88f6c304ede70145c1d17a549504003ee9 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 10:09:12 +0800 Subject: [PATCH 07/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 13b64491f..b976e2956 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -124,7 +124,7 @@ static inline int decode_params_checking(AVCodecContext* avctx) static av_cold int ff_himpi_decode_end(AVCodecContext *avctx) { - av_log(ctx, AV_LOG_ERROR, "here is decode end 0.\n"); + av_log(avctx, AV_LOG_ERROR, "here is decode end 0.\n"); ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; int ret = 0; int semvalue = 0; -- Gitee From 15680c2df187f11279fad754f2b234df0cdb92e0 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 10:16:20 +0800 Subject: [PATCH 08/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index b976e2956..e65d6b32a 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -440,6 +440,7 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) { + av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 0 \n"); ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; int ret = 0; if (!ctx->frame_queue) { @@ -448,29 +449,33 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) FrameInfo_t frame_info; ff_mutex_lock(&ctx->queue_mutex); + av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 1 \n"); if (av_fifo_size(ctx->frame_queue) != 0) { av_fifo_generic_read(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); } else { ff_mutex_unlock(&ctx->queue_mutex); return AVERROR(EAGAIN); } + av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 2 \n"); ff_mutex_unlock(&ctx->queue_mutex); if (frame_info.event_type == EVENT_EOS) { return AVERROR_EOF; } - + av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 3 \n"); if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { ret = av_hwframe_get_buffer(ctx->hw_frame_ref, frame, 0); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed, ret is %d.\n", ret); return AVERROR(EINVAL); } + av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 4 \n"); ret = ff_decode_frame_props(avctx, frame); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed, ret is %d.\n", ret); return AVERROR(EINVAL); } + av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 5 \n"); } else { ret = ff_get_buffer(avctx, frame, 0); if (ret < 0) { @@ -478,7 +483,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) return AVERROR(EINVAL); } } - + av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 6 \n"); frame->pkt_pos = -1; frame->pkt_duration = 0; frame->pkt_size = -1; -- Gitee From cc579193033e8d2adb265dec857bb161f71ccad7 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 10:39:11 +0800 Subject: [PATCH 09/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_dec.c | 1 + 1 file changed, 1 insertion(+) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_dec.c index 1705252fa..7dfc65a19 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_dec.c @@ -542,6 +542,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_ASCEND, AV_PIX_FMT_NV12, AV_PIX_FMT_NONE }; avctx->pix_fmt = ff_get_format(avctx, pix_fmts); + av_log(avctx, AV_LOG_ERROR, "Error, ff_get_format failed with format id: %d.\n", avctx->pix_fmt); if (avctx->pix_fmt < 0) { av_log(avctx, AV_LOG_ERROR, "Error, ff_get_format failed with format id: %d.\n", avctx->pix_fmt); return AVERROR_BUG; -- Gitee From 4741ea92b1540ffba3f7f80bac3393de615ebfc7 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 10:58:50 +0800 Subject: [PATCH 10/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 130 +++++++++--------- 1 file changed, 66 insertions(+), 64 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index e65d6b32a..45bc81280 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -147,61 +147,6 @@ static av_cold int ff_himpi_decode_end(AVCodecContext *avctx) sem_getvalue(&ctx->eos_sema, &semvalue); av_log(ctx, AV_LOG_ERROR, "Decode sem_timewait = -1, semvalue = %d.\n", semvalue); } - // new - hi_video_frame_info frame; - hi_vdec_stream stream; - hi_vdec_supplement_info stSupplement; - ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); - if (ret != 0) { - FrameInfo_t frame_info; - memset(&frame_info, 0, sizeof(FrameInfo_t)); - frame_info.event_type = EVENT_EOS; - - ff_mutex_lock(&ctx->queue_mutex); - av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); - ff_mutex_unlock(&ctx->queue_mutex); - sem_post(&ctx->eos_sema); - av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); - return ret; - } - - size_t decResult = frame.v_frame.frame_flag; - -// hi_mpi_dvpp_free(stream.addr); -// if (ret != 0) { -// av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); -// return ret; -// } - if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { - hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); - return ret; - } - - if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { - ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); - return ((void*) (-1)); - } - return ret; - } - FrameInfo_t frame_info; - frame_info.ascend_ctx = ctx; - get_vdec_frame_info(&frame_info, frame); - - ff_mutex_lock(&ctx->queue_mutex); - av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); - av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); - ff_mutex_unlock(&ctx->queue_mutex); - - ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); - return ret; - } - - ctx->total_out_frame_count++; - // new if (ctx->hi_mpi_init_flag) { ret = hi_mpi_vdec_stop_recv_stream(ctx->channel_id); @@ -571,18 +516,75 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return ret; } + // new + hi_video_frame_info frame; + hi_vdec_stream stream; + hi_vdec_supplement_info stSupplement; + ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); + if (ret != 0) { + FrameInfo_t frame_info; + memset(&frame_info, 0, sizeof(FrameInfo_t)); + frame_info.event_type = EVENT_EOS; + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + sem_post(&ctx->eos_sema); + av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); + return ret; + } + + size_t decResult = frame.v_frame.frame_flag; + +// hi_mpi_dvpp_free(stream.addr); +// if (ret != 0) { +// av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); +// return ret; +// } + if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { + hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); + return ret; + } + + if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { + ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); + return ((void*) (-1)); + } + return ret; + } + FrameInfo_t frame_info; + frame_info.ascend_ctx = ctx; + get_vdec_frame_info(&frame_info, frame); + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); + av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + + ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); + return ret; + } + + ctx->total_out_frame_count++; + // new + + // new send_ret = ff_decode_get_packet(avctx, &pkt); - if (send_ret < 0 && send_ret != AVERROR_EOF) { - return send_ret; - } - send_ret = hi_mpi_decode(avctx, &pkt); - av_packet_unref(&pkt); - if (send_ret < 0 && send_ret != AVERROR_EOF) { - av_log(ctx, AV_LOG_ERROR, "Send packet failed, ret is %d.\n", send_ret); - return send_ret; - } + if (send_ret < 0 && send_ret != AVERROR_EOF) { + return send_ret; + } + send_ret = hi_mpi_decode(avctx, &pkt); + av_packet_unref(&pkt); + if (send_ret < 0 && send_ret != AVERROR_EOF) { + av_log(ctx, AV_LOG_ERROR, "Send packet failed, ret is %d.\n", send_ret); + return send_ret; + } get_ret = himpi_get_frame(avctx, frame); av_log(avctx, AV_LOG_ERROR, "get ret is %d.\n", get_ret); -- Gitee From 35bf886e5b45dbc5068a7f9d74a203fa8f303619 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:03:29 +0800 Subject: [PATCH 11/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 76 +++++++++++-------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 45bc81280..a361f2fb3 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -483,40 +483,9 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) return 0; } -static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) +static int get_frame(ASCENDContext_t* ctx) { - av_log(avctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 0.\n"); - ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; - AVPacket pkt = { 0 }; - int send_ret = -1; - int get_ret = -1; - int ret = 0; - - if (avctx == NULL || avctx->priv_data == NULL) { - av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); - return AVERROR_BUG; - } -// if (!ctx->hi_mpi_init_flag || !ctx->thread_run_flag || !ctx->decode_run_flag) { -// av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); -// return AVERROR_BUG; -// } - - if (!ctx->hi_mpi_init_flag || !ctx->decode_run_flag) { - av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); - return AVERROR_BUG; - } - - if (ctx->eos_received) { - return AVERROR_EOF; - } - - ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); - return ret; - } - - // new +// new hi_video_frame_info frame; hi_vdec_stream stream; hi_vdec_supplement_info stSupplement; @@ -571,8 +540,49 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) ctx->total_out_frame_count++; // new + return 0; +} + +static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) +{ + av_log(avctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 0.\n"); + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + AVPacket pkt = { 0 }; + int send_ret = -1; + int get_ret = -1; + int ret = 0; + + if (avctx == NULL || avctx->priv_data == NULL) { + av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } +// if (!ctx->hi_mpi_init_flag || !ctx->thread_run_flag || !ctx->decode_run_flag) { +// av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); +// return AVERROR_BUG; +// } + + if (!ctx->hi_mpi_init_flag || !ctx->decode_run_flag) { + av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } + + if (ctx->eos_received) { + return AVERROR_EOF; + } + + ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); + return ret; + } + ret = get_frame(ctx); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); + return ret; + } + // new send_ret = ff_decode_get_packet(avctx, &pkt); -- Gitee From a4e9eb3831f0b42db2cff6e71d303814eb2cbc7c Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:04:42 +0800 Subject: [PATCH 12/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 1 + 1 file changed, 1 insertion(+) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index a361f2fb3..67d49755b 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -486,6 +486,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) static int get_frame(ASCENDContext_t* ctx) { // new + int ret; hi_video_frame_info frame; hi_vdec_stream stream; hi_vdec_supplement_info stSupplement; -- Gitee From 73ef7a29e213f304d12c968f4dd9ef7c7b27060b Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:10:40 +0800 Subject: [PATCH 13/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 67d49755b..96b47ec90 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -486,11 +486,13 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) static int get_frame(ASCENDContext_t* ctx) { // new + av_log(ctx, AV_LOG_DEBUG, "start get frame.\n"); int ret; hi_video_frame_info frame; hi_vdec_stream stream; hi_vdec_supplement_info stSupplement; ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); + av_log(ctx, AV_LOG_DEBUG, "start hi_mpi_vdec_get_frame.\n"); if (ret != 0) { FrameInfo_t frame_info; memset(&frame_info, 0, sizeof(FrameInfo_t)); @@ -503,7 +505,7 @@ static int get_frame(ASCENDContext_t* ctx) av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); return ret; } - + av_log(ctx, AV_LOG_DEBUG, "end hi_mpi_vdec_get_frame.\n"); size_t decResult = frame.v_frame.frame_flag; // hi_mpi_dvpp_free(stream.addr); @@ -511,11 +513,12 @@ static int get_frame(ASCENDContext_t* ctx) // av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); // return ret; // } + av_log(ctx, AV_LOG_DEBUG, "start check decResult.\n"); if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); return ret; } - + av_log(ctx, AV_LOG_DEBUG, "end check decResult.\n"); if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); if (ret != 0) { @@ -524,6 +527,7 @@ static int get_frame(ASCENDContext_t* ctx) } return ret; } + av_log(ctx, AV_LOG_DEBUG, "end hi_mpi_vdec_release_frame.\n"); FrameInfo_t frame_info; frame_info.ascend_ctx = ctx; get_vdec_frame_info(&frame_info, frame); @@ -532,13 +536,13 @@ static int get_frame(ASCENDContext_t* ctx) av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); ff_mutex_unlock(&ctx->queue_mutex); - + av_log(ctx, AV_LOG_DEBUG, "start hi_mpi_vdec_release_frame..\n"); ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); if (ret != 0) { av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); return ret; } - + av_log(ctx, AV_LOG_DEBUG, "end hi_mpi_vdec_release_frame..\n"); ctx->total_out_frame_count++; // new return 0; -- Gitee From cbd103909c17518a84ef26208f52ec2d14afe2f7 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:21:55 +0800 Subject: [PATCH 14/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 96b47ec90..d9c8e5f0c 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -483,9 +483,10 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) return 0; } -static int get_frame(ASCENDContext_t* ctx) +static int get_frame(AVCodecContext *avctx) { // new + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; av_log(ctx, AV_LOG_DEBUG, "start get frame.\n"); int ret; hi_video_frame_info frame; @@ -582,7 +583,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } - ret = get_frame(ctx); + ret = get_frame(avctx); if (ret != 0) { av_log(ctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); return ret; -- Gitee From 2a1867619807e0898a8f7ddb92563d6e0d22671f Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:25:44 +0800 Subject: [PATCH 15/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index d9c8e5f0c..d8a1ec9eb 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -487,13 +487,13 @@ static int get_frame(AVCodecContext *avctx) { // new ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; - av_log(ctx, AV_LOG_DEBUG, "start get frame.\n"); + av_log(avctx, AV_LOG_DEBUG, "start get frame.\n"); int ret; hi_video_frame_info frame; hi_vdec_stream stream; hi_vdec_supplement_info stSupplement; ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); - av_log(ctx, AV_LOG_DEBUG, "start hi_mpi_vdec_get_frame.\n"); + av_log(avctx, AV_LOG_DEBUG, "start hi_mpi_vdec_get_frame.\n"); if (ret != 0) { FrameInfo_t frame_info; memset(&frame_info, 0, sizeof(FrameInfo_t)); @@ -503,10 +503,10 @@ static int get_frame(AVCodecContext *avctx) av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); ff_mutex_unlock(&ctx->queue_mutex); sem_post(&ctx->eos_sema); - av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); + av_log(avctx, AV_LOG_DEBUG, "Decode got eos.\n"); return ret; } - av_log(ctx, AV_LOG_DEBUG, "end hi_mpi_vdec_get_frame.\n"); + av_log(avctx, AV_LOG_DEBUG, "end hi_mpi_vdec_get_frame.\n"); size_t decResult = frame.v_frame.frame_flag; // hi_mpi_dvpp_free(stream.addr); @@ -514,21 +514,21 @@ static int get_frame(AVCodecContext *avctx) // av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); // return ret; // } - av_log(ctx, AV_LOG_DEBUG, "start check decResult.\n"); + av_log(avctx, AV_LOG_DEBUG, "start check decResult.\n"); if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); return ret; } - av_log(ctx, AV_LOG_DEBUG, "end check decResult.\n"); + av_log(avctx, AV_LOG_DEBUG, "end check decResult.\n"); if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); + av_log(avctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); return ((void*) (-1)); } return ret; } - av_log(ctx, AV_LOG_DEBUG, "end hi_mpi_vdec_release_frame.\n"); + av_log(avctx, AV_LOG_DEBUG, "end hi_mpi_vdec_release_frame.\n"); FrameInfo_t frame_info; frame_info.ascend_ctx = ctx; get_vdec_frame_info(&frame_info, frame); @@ -537,13 +537,13 @@ static int get_frame(AVCodecContext *avctx) av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); ff_mutex_unlock(&ctx->queue_mutex); - av_log(ctx, AV_LOG_DEBUG, "start hi_mpi_vdec_release_frame..\n"); + av_log(avctx, AV_LOG_DEBUG, "start hi_mpi_vdec_release_frame..\n"); ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); + av_log(avctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); return ret; } - av_log(ctx, AV_LOG_DEBUG, "end hi_mpi_vdec_release_frame..\n"); + av_log(avctx, AV_LOG_DEBUG, "end hi_mpi_vdec_release_frame..\n"); ctx->total_out_frame_count++; // new return 0; -- Gitee From f8ff7f1cfa3e07586fe3fd25c9186eaba8a0ac79 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:28:11 +0800 Subject: [PATCH 16/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index d8a1ec9eb..7b6d77e42 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -379,6 +379,7 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) // ctx->decoder_flushing = 1; // } // } + av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); av_packet_unref(avpkt); return 0; } @@ -585,7 +586,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) ret = get_frame(avctx); if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); + av_log(avctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); return ret; } -- Gitee From c509023e02d3c1933fba25c07565d9b811bbfe38 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:31:19 +0800 Subject: [PATCH 17/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 7b6d77e42..8ed756481 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -583,7 +583,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return ret; } - + av_log(ctx, AV_LOG_ERROR, "start to get frame.\n"); ret = get_frame(avctx); if (ret != 0) { av_log(avctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); -- Gitee From 0e6a3b45521d18883e769b50380ea53475e57c63 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:37:54 +0800 Subject: [PATCH 18/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 8ed756481..bf73aa8a3 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -488,13 +488,13 @@ static int get_frame(AVCodecContext *avctx) { // new ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; - av_log(avctx, AV_LOG_DEBUG, "start get frame.\n"); + av_log(avctx, AV_LOG_ERROR, "start get frame.\n"); int ret; hi_video_frame_info frame; hi_vdec_stream stream; hi_vdec_supplement_info stSupplement; ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); - av_log(avctx, AV_LOG_DEBUG, "start hi_mpi_vdec_get_frame.\n"); + av_log(avctx, AV_LOG_ERROR, "start hi_mpi_vdec_get_frame.\n"); if (ret != 0) { FrameInfo_t frame_info; memset(&frame_info, 0, sizeof(FrameInfo_t)); -- Gitee From f95c9d7eed49b66926729d97c2a9a2f70c07fd27 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:40:30 +0800 Subject: [PATCH 19/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index bf73aa8a3..ede3d7f8b 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -507,7 +507,7 @@ static int get_frame(AVCodecContext *avctx) av_log(avctx, AV_LOG_DEBUG, "Decode got eos.\n"); return ret; } - av_log(avctx, AV_LOG_DEBUG, "end hi_mpi_vdec_get_frame.\n"); + av_log(avctx, AV_LOG_ERROR, "end hi_mpi_vdec_get_frame.\n"); size_t decResult = frame.v_frame.frame_flag; // hi_mpi_dvpp_free(stream.addr); @@ -515,12 +515,12 @@ static int get_frame(AVCodecContext *avctx) // av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); // return ret; // } - av_log(avctx, AV_LOG_DEBUG, "start check decResult.\n"); + av_log(avctx, AV_LOG_ERROR, "start check decResult.\n"); if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); return ret; } - av_log(avctx, AV_LOG_DEBUG, "end check decResult.\n"); + av_log(avctx, AV_LOG_ERROR, "end check decResult.\n"); if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); if (ret != 0) { @@ -529,7 +529,7 @@ static int get_frame(AVCodecContext *avctx) } return ret; } - av_log(avctx, AV_LOG_DEBUG, "end hi_mpi_vdec_release_frame.\n"); + av_log(avctx, AV_LOG_ERROR, "end hi_mpi_vdec_release_frame.\n"); FrameInfo_t frame_info; frame_info.ascend_ctx = ctx; get_vdec_frame_info(&frame_info, frame); @@ -544,7 +544,7 @@ static int get_frame(AVCodecContext *avctx) av_log(avctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); return ret; } - av_log(avctx, AV_LOG_DEBUG, "end hi_mpi_vdec_release_frame..\n"); + av_log(avctx, AV_LOG_ERROR, "end hi_mpi_vdec_release_frame..\n"); ctx->total_out_frame_count++; // new return 0; -- Gitee From a526477a0a1bee862a06db3c1a52e268305d3610 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 11:44:09 +0800 Subject: [PATCH 20/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 1 + 1 file changed, 1 insertion(+) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index ede3d7f8b..40372e5b2 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -494,6 +494,7 @@ static int get_frame(AVCodecContext *avctx) hi_vdec_stream stream; hi_vdec_supplement_info stSupplement; ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); + av_log(avctx, AV_LOG_ERROR, "ret is %d.\n", ret); av_log(avctx, AV_LOG_ERROR, "start hi_mpi_vdec_get_frame.\n"); if (ret != 0) { FrameInfo_t frame_info; -- Gitee From 87b4327361adfb0363c3ea606053bc8e4afe074d Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 14:17:44 +0800 Subject: [PATCH 21/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 40372e5b2..e48b1ac48 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -584,12 +584,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return ret; } - av_log(ctx, AV_LOG_ERROR, "start to get frame.\n"); - ret = get_frame(avctx); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); - return ret; - } + // new @@ -604,6 +599,13 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return send_ret; } + av_log(ctx, AV_LOG_ERROR, "start to get frame.\n"); + ret = get_frame(avctx); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); + return ret; + } + get_ret = himpi_get_frame(avctx, frame); av_log(avctx, AV_LOG_ERROR, "get ret is %d.\n", get_ret); av_log(avctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 1.\n"); -- Gitee From 5b17ecd35dfab56868c140decdfd87ed01e230d8 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 15:39:09 +0800 Subject: [PATCH 22/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 91 ++++++++++--------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index e48b1ac48..f7df6dfdd 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -200,43 +200,44 @@ static av_cold int ff_himpi_decode_end(AVCodecContext *avctx) static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) { ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + av_log(avctx, AV_LOG_ERROR, "extradata_size is %d.\n", avctx->extradata_size); int ret = 0; - if (ctx->first_packet) { - if (avctx->extradata_size) { - uint8_t* streamBuffer = NULL; - ret = hi_mpi_dvpp_malloc(ctx->device_id, &streamBuffer, avctx->extradata_size); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi malloc first packet failed, ret is %d.\n", ret); - return ret; - } - ret = aclrtMemcpy(streamBuffer, avctx->extradata_size, avctx->extradata, avctx->extradata_size, - ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "Mem copy H2D first packet failed, ret is %d.\n", ret); - return ret; - } - - hi_vdec_stream stream; - stream.pts = avpkt->pts; - stream.addr = streamBuffer; - stream.len = avctx->extradata_size; - stream.end_of_frame = HI_TRUE; - stream.end_of_stream = HI_FALSE; - stream.need_display = HI_FALSE; - - hi_vdec_pic_info pic_info; - pic_info.vir_addr = 0; - pic_info.buffer_size = 0; - pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; - ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi vdec send first packet failed, ret is %d.\n", ret); - return ret; - } - } - ctx->first_packet = 0; - } - +// if (ctx->first_packet) { +// if (avctx->extradata_size) { +// uint8_t* streamBuffer = NULL; +// ret = hi_mpi_dvpp_malloc(ctx->device_id, &streamBuffer, avctx->extradata_size); +// if (ret != 0) { +// av_log(avctx, AV_LOG_ERROR, "HiMpi malloc first packet failed, ret is %d.\n", ret); +// return ret; +// } +// ret = aclrtMemcpy(streamBuffer, avctx->extradata_size, avctx->extradata, avctx->extradata_size, +// ACL_MEMCPY_HOST_TO_DEVICE); +// if (ret != 0) { +// av_log(avctx, AV_LOG_ERROR, "Mem copy H2D first packet failed, ret is %d.\n", ret); +// return ret; +// } +// +// hi_vdec_stream stream; +// stream.pts = 0; +// stream.addr = streamBuffer; +// stream.len = avctx->extradata_size; +// stream.end_of_frame = HI_TRUE; +// stream.end_of_stream = HI_FALSE; +// stream.need_display = HI_TRUE; +// +// hi_vdec_pic_info pic_info; +// pic_info.vir_addr = 0; +// pic_info.buffer_size = 0; +// pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; +// ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); +// if (ret != 0) { +// av_log(avctx, AV_LOG_ERROR, "HiMpi vdec send first packet failed, ret is %d.\n", ret); +// return ret; +// } +// } +// ctx->first_packet = 0; +// } + av_log(avctx, AV_LOG_ERROR, "avpkt->size is %d.\n", avpkt->size); uint8_t* streamBuffer = NULL; ret = hi_mpi_dvpp_malloc(ctx->device_id, &streamBuffer, avpkt->size); if (ret != 0) { @@ -253,7 +254,7 @@ static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) // create stream info hi_vdec_stream stream; - stream.pts = avpkt->pts; + stream.pts = 0; stream.addr = streamBuffer; stream.len = avpkt->size; stream.end_of_frame = HI_TRUE; @@ -287,12 +288,14 @@ static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) } pic_info.vir_addr = (uint64_t)picBuffer; - do { - ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); - if ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL) { - usleep(VDEC_SLEEP_TIME); - } - } while ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL); + ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); + +// do { +// ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); +// if ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL) { +// usleep(VDEC_SLEEP_TIME); +// } +// } while ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL); if (ret != 0) { av_log(avctx, AV_LOG_ERROR, "HiMpi send stream failed, ret is %d.\n", ret); @@ -379,7 +382,7 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) // ctx->decoder_flushing = 1; // } // } - av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); + av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d\n", ret); av_packet_unref(avpkt); return 0; } -- Gitee From b5f9efb395698d90b66aedc6eccb0c15174ef778 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 15:44:42 +0800 Subject: [PATCH 23/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 132 +++++++++--------- 1 file changed, 68 insertions(+), 64 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index f7df6dfdd..92e280e36 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -314,74 +314,78 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) AVPacket packet = { 0 }; AVPacket bsf_packet = { 0 }; - hi_vdec_stream stream; - stream.addr = NULL; - stream.len = 0; - stream.end_of_frame = HI_FALSE; - stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. - stream.need_display = HI_TRUE; +// hi_vdec_stream stream; +// stream.addr = NULL; +// stream.len = 0; +// stream.end_of_frame = HI_FALSE; +// stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. +// stream.need_display = HI_TRUE; +// +// hi_vdec_pic_info pic_info; +// pic_info.vir_addr = 0; +// pic_info.buffer_size = 0; +// pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; +// ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); +// if (ret != 0) { +// av_packet_unref(avpkt); +// av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); +// return ret; +// } +// ctx->decoder_flushing = 1; - hi_vdec_pic_info pic_info; - pic_info.vir_addr = 0; - pic_info.buffer_size = 0; - pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; - ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); - if (ret != 0) { - av_packet_unref(avpkt); - av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); - return ret; + + + if (avpkt && avpkt->size && ctx->bsf) { + ret = av_packet_ref(&packet, avpkt); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed, ret(%d).\n", ret); + return ret; + } + ret = av_bsf_send_packet(ctx->bsf, &packet); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed, ret(%d).\n", ret); + av_packet_unref(&packet); + return ret; + } + ret = av_bsf_receive_packet(ctx->bsf, &bsf_packet); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed, ret(%d).\n", ret); + return ret; + } + avpkt = &bsf_packet; } - ctx->decoder_flushing = 1; + av_packet_unref(&packet); -// if (avpkt && avpkt->size && ctx->bsf) { -// ret = av_packet_ref(&packet, avpkt); -// if (ret < 0) { -// av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed, ret(%d).\n", ret); -// return ret; -// } -// ret = av_bsf_send_packet(ctx->bsf, &packet); -// if (ret < 0) { -// av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed, ret(%d).\n", ret); -// av_packet_unref(&packet); -// return ret; -// } -// ret = av_bsf_receive_packet(ctx->bsf, &bsf_packet); -// if (ret < 0) { -// av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed, ret(%d).\n", ret); -// return ret; -// } -// avpkt = &bsf_packet; -// } -// av_packet_unref(&packet); -// if (avpkt && avpkt->size) { -// ret = malloc_and_send_frame(avctx, avpkt); -// if (ret != 0) { -// av_packet_unref(avpkt); -// return AVERROR(EINVAL); -// } -// } else { -// if (!ctx->decoder_flushing) { -// hi_vdec_stream stream; -// stream.addr = NULL; -// stream.len = 0; -// stream.end_of_frame = HI_FALSE; -// stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. -// stream.need_display = HI_TRUE; -// -// hi_vdec_pic_info pic_info; -// pic_info.vir_addr = 0; -// pic_info.buffer_size = 0; -// pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; -// ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); -// if (ret != 0) { -// av_packet_unref(avpkt); -// av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); -// return ret; -// } -// ctx->decoder_flushing = 1; -// } -// } + + if (avpkt && avpkt->size) { + ret = malloc_and_send_frame(avctx, avpkt); + if (ret != 0) { + av_packet_unref(avpkt); + return AVERROR(EINVAL); + } + } else { + if (!ctx->decoder_flushing) { + hi_vdec_stream stream; + stream.addr = NULL; + stream.len = 0; + stream.end_of_frame = HI_FALSE; + stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. + stream.need_display = HI_TRUE; + + hi_vdec_pic_info pic_info; + pic_info.vir_addr = 0; + pic_info.buffer_size = 0; + pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); + if (ret != 0) { + av_packet_unref(avpkt); + av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); + return ret; + } + ctx->decoder_flushing = 1; + } + } av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d\n", ret); av_packet_unref(avpkt); return 0; -- Gitee From ae361edb9ca30cd507ceb588b95709979e4aaca0 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 15:46:27 +0800 Subject: [PATCH 24/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 92e280e36..f472c4813 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -314,24 +314,24 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) AVPacket packet = { 0 }; AVPacket bsf_packet = { 0 }; -// hi_vdec_stream stream; -// stream.addr = NULL; -// stream.len = 0; -// stream.end_of_frame = HI_FALSE; -// stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. -// stream.need_display = HI_TRUE; -// -// hi_vdec_pic_info pic_info; -// pic_info.vir_addr = 0; -// pic_info.buffer_size = 0; -// pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; -// ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); -// if (ret != 0) { -// av_packet_unref(avpkt); -// av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); -// return ret; -// } -// ctx->decoder_flushing = 1; + hi_vdec_stream stream; + stream.addr = NULL; + stream.len = 0; + stream.end_of_frame = HI_FALSE; + stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. + stream.need_display = HI_TRUE; + + hi_vdec_pic_info pic_info; + pic_info.vir_addr = 0; + pic_info.buffer_size = 0; + pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); + if (ret != 0) { + av_packet_unref(avpkt); + av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); + return ret; + } + ctx->decoder_flushing = 1; -- Gitee From f23a58d0102fd75c962ae75e80acb1d527032882 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 15:50:12 +0800 Subject: [PATCH 25/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index f472c4813..5e10fbb87 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -333,7 +333,12 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) } ctx->decoder_flushing = 1; + if (!avpkt) { + av_log(avctx, AV_LOG_ERROR, "avpkt is null.\n"); + return -1; + } + av_log(avctx, AV_LOG_ERROR, "avpkt->size is %d.\n", avpkt->size); if (avpkt && avpkt->size && ctx->bsf) { ret = av_packet_ref(&packet, avpkt); -- Gitee From 69ee313f02119b396d92c61cc219bc47bb0ea1a2 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 16:15:34 +0800 Subject: [PATCH 26/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 5e10fbb87..08228d7f7 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -601,6 +601,8 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) // new send_ret = ff_decode_get_packet(avctx, &pkt); + av_log(ctx, AV_LOG_ERROR, "pkt->size is %d.\n", pkt->size); + if (send_ret < 0 && send_ret != AVERROR_EOF) { return send_ret; } @@ -611,12 +613,12 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return send_ret; } - av_log(ctx, AV_LOG_ERROR, "start to get frame.\n"); - ret = get_frame(avctx); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); - return ret; - } +// av_log(ctx, AV_LOG_ERROR, "start to get frame.\n"); +// ret = get_frame(avctx); +// if (ret != 0) { +// av_log(avctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); +// return ret; +// } get_ret = himpi_get_frame(avctx, frame); av_log(avctx, AV_LOG_ERROR, "get ret is %d.\n", get_ret); -- Gitee From 053b5a7f6d961fae09dfeab729aba225c6f05006 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 16:16:54 +0800 Subject: [PATCH 27/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 08228d7f7..1a02f184e 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -601,7 +601,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) // new send_ret = ff_decode_get_packet(avctx, &pkt); - av_log(ctx, AV_LOG_ERROR, "pkt->size is %d.\n", pkt->size); + av_log(ctx, AV_LOG_ERROR, "pkt->size is %d.\n", pkt.size); if (send_ret < 0 && send_ret != AVERROR_EOF) { return send_ret; -- Gitee From 35936d217209af34a21a49ede3135859e3b69962 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 19:47:32 +0800 Subject: [PATCH 28/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 5 +++-- mxVision/Ascendffmpeg/libavformat/utils.c | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 1a02f184e..40e6c5f88 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -740,10 +740,11 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) if (decode_params_checking(avctx) != 0) { return AVERROR(EINVAL); } - av_log(avctx, AV_LOG_DEBUG, "Vdec width: %d.\n", ctx->vdec_width); - av_log(avctx, AV_LOG_DEBUG, "Vdec height: %d.\n", ctx->vdec_height); + av_log(avctx, AV_LOG_ERROR, "Vdec width: %d.\n", ctx->vdec_width); + av_log(avctx, AV_LOG_ERROR, "Vdec height: %d.\n", ctx->vdec_height); if (avctx->hw_frames_ctx) { + av_log(avctx, AV_LOG_ERROR, "here is hw_frames_ctx is yes\n"); av_buffer_unref(&ctx->hw_frame_ref); ctx->hw_frame_ref = av_buffer_ref(avctx->hw_frames_ctx); if (!ctx->hw_frame_ref) { diff --git a/mxVision/Ascendffmpeg/libavformat/utils.c b/mxVision/Ascendffmpeg/libavformat/utils.c index 75e5350a2..bc2cb9d10 100644 --- a/mxVision/Ascendffmpeg/libavformat/utils.c +++ b/mxVision/Ascendffmpeg/libavformat/utils.c @@ -4122,6 +4122,8 @@ FF_ENABLE_DEPRECATION_WARNINGS if (ret < 0) goto find_stream_info_err; } + av_log(ic, AV_LOG_WARNING, + "st->internal->info->found_decoder is %d\n", st->internal->info->found_decoder); if (!has_codec_parameters(st, &errmsg)) { char buf[256]; avcodec_string(buf, sizeof(buf), st->internal->avctx, 0); -- Gitee From 2c73e213561e020ba33c4f90175e99aeadc7a7b2 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 20:01:29 +0800 Subject: [PATCH 29/60] ffmpeg --- mxVision/Ascendffmpeg/libavformat/utils.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavformat/utils.c b/mxVision/Ascendffmpeg/libavformat/utils.c index bc2cb9d10..919d2a89f 100644 --- a/mxVision/Ascendffmpeg/libavformat/utils.c +++ b/mxVision/Ascendffmpeg/libavformat/utils.c @@ -2968,7 +2968,7 @@ static void estimate_timings(AVFormatContext *ic, int64_t old_offset) static int has_codec_parameters(AVStream *st, const char **errmsg_ptr) { - AVCodecContext *avctx = st->internal->avctx; + AVCodecContext *avctx = st->internal->avctx; // st->internal->avctx->pix_fmt #define FAIL(errmsg) do { \ if (errmsg_ptr) \ @@ -4124,6 +4124,9 @@ FF_ENABLE_DEPRECATION_WARNINGS } av_log(ic, AV_LOG_WARNING, "st->internal->info->found_decoder is %d\n", st->internal->info->found_decoder); + av_log(ic, AV_LOG_WARNING, + "st->internal->avctx->pix_fmt is %d\n", st->internal->avctx->pix_fmt); + if (!has_codec_parameters(st, &errmsg)) { char buf[256]; avcodec_string(buf, sizeof(buf), st->internal->avctx, 0); -- Gitee From 8c563ed06c1d7348b9cab862e66174b9d846628f Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 20:26:09 +0800 Subject: [PATCH 30/60] ffmpeg --- mxVision/Ascendffmpeg/doc/examples/filtering_video.c | 1 + mxVision/Ascendffmpeg/fftools/ffmpeg_opt.c | 1 + 2 files changed, 2 insertions(+) diff --git a/mxVision/Ascendffmpeg/doc/examples/filtering_video.c b/mxVision/Ascendffmpeg/doc/examples/filtering_video.c index 105a200d9..a98b9622a 100644 --- a/mxVision/Ascendffmpeg/doc/examples/filtering_video.c +++ b/mxVision/Ascendffmpeg/doc/examples/filtering_video.c @@ -61,6 +61,7 @@ static int open_input_file(const char *filename) return ret; } + av_log(NULL, AV_LOG_ERROR, "filtering_video.c: Cannot find stream information\n"); if ((ret = avformat_find_stream_info(fmt_ctx, NULL)) < 0) { av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n"); return ret; diff --git a/mxVision/Ascendffmpeg/fftools/ffmpeg_opt.c b/mxVision/Ascendffmpeg/fftools/ffmpeg_opt.c index c2386e4a3..f0b0efce5 100644 --- a/mxVision/Ascendffmpeg/fftools/ffmpeg_opt.c +++ b/mxVision/Ascendffmpeg/fftools/ffmpeg_opt.c @@ -1194,6 +1194,7 @@ static int open_input_file(OptionsContext *o, const char *filename) /* If not enough info to get the stream parameters, we decode the first frames to get it. (used in mpeg case for example) */ + av_log(NULL, AV_LOG_WARNING, "ffmpeg_opt.c: Cannot find stream information\n"); ret = avformat_find_stream_info(ic, opts); for (i = 0; i < orig_nb_streams; i++) -- Gitee From 5b42aae1dbe0663a4d2530868e804525ed1a0f55 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 20:51:24 +0800 Subject: [PATCH 31/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 40e6c5f88..c5c940e71 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -341,6 +341,7 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) av_log(avctx, AV_LOG_ERROR, "avpkt->size is %d.\n", avpkt->size); if (avpkt && avpkt->size && ctx->bsf) { + av_log(avctx, AV_LOG_ERROR, "xxxx"); ret = av_packet_ref(&packet, avpkt); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed, ret(%d).\n", ret); @@ -365,6 +366,7 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) if (avpkt && avpkt->size) { ret = malloc_and_send_frame(avctx, avpkt); + av_log(avctx, AV_LOG_ERROR, "malloc_and_send_frame ret is %d\n", ret); if (ret != 0) { av_packet_unref(avpkt); return AVERROR(EINVAL); @@ -408,6 +410,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) FrameInfo_t frame_info; ff_mutex_lock(&ctx->queue_mutex); av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 1 \n"); + av_log(avctx, AV_LOG_ERROR, "av_fifo_size(ctx->frame_queue) is %d \n", av_fifo_size(ctx->frame_queue)); if (av_fifo_size(ctx->frame_queue) != 0) { av_fifo_generic_read(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); } else { -- Gitee From d48a26e88f6473d288d57ab61f6ad57842f4c67f Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 21:08:54 +0800 Subject: [PATCH 32/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index c5c940e71..3c3ed590b 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -526,12 +526,13 @@ static int get_frame(AVCodecContext *avctx) av_log(avctx, AV_LOG_ERROR, "end hi_mpi_vdec_get_frame.\n"); size_t decResult = frame.v_frame.frame_flag; -// hi_mpi_dvpp_free(stream.addr); -// if (ret != 0) { -// av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); -// return ret; -// } + hi_mpi_dvpp_free(stream.addr); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); + return ret; + } av_log(avctx, AV_LOG_ERROR, "start check decResult.\n"); + av_log(avctx, AV_LOG_ERROR, "decResult is %d.\n", decResult); if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); return ret; @@ -616,12 +617,12 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return send_ret; } -// av_log(ctx, AV_LOG_ERROR, "start to get frame.\n"); -// ret = get_frame(avctx); -// if (ret != 0) { -// av_log(avctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); -// return ret; -// } + av_log(ctx, AV_LOG_ERROR, "start to get frame.\n"); + ret = get_frame(avctx); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); + return ret; + } get_ret = himpi_get_frame(avctx, frame); av_log(avctx, AV_LOG_ERROR, "get ret is %d.\n", get_ret); -- Gitee From 55b2fcf78771d909aeb46623d01f6f1c66155da1 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 3 Sep 2024 21:16:39 +0800 Subject: [PATCH 33/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 3c3ed590b..7357992ca 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -271,12 +271,18 @@ static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) pic_info.height = ctx->resize_height; // Output image height, supports resize, set 0 means no resize. pic_info.width_stride = FFALIGN(ctx->vdec_width, VDEC_WIDTH_ALIGN); pic_info.height_stride = FFALIGN(ctx->vdec_height, VDEC_HEIGHT_ALIGN); + if (ctx->resize_str && ctx->resize_width != 0 && ctx->resize_height != 0) { pic_info.width_stride = FFALIGN(ctx->resize_width, VDEC_WIDTH_ALIGN); pic_info.height_stride = FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN); } - uint32_t size = pic_info.width_stride * pic_info.height_stride * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; + av_log(avctx, AV_LOG_ERROR, "output width is %d.\n", pic_info.width); + av_log(avctx, AV_LOG_ERROR, "output height is %d.\n", pic_info.height); + av_log(avctx, AV_LOG_ERROR, "output width_stride is %d.\n", pic_info.width_stride); + av_log(avctx, AV_LOG_ERROR, "output height_stride is %d.\n", pic_info.height_stride); + uint32_t size = pic_info.width_stride * pic_info.height_stride * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; + av_log(avctx, AV_LOG_ERROR, "output size is %d.\n", size); pic_info.buffer_size = size; pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; void *picBuffer = NULL; -- Gitee From f31950f236825ed03e400d832a808b02313c3ca4 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 09:01:53 +0800 Subject: [PATCH 34/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 14 +++++++------- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 7357992ca..f5c38ec42 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -269,14 +269,14 @@ static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) hi_vdec_pic_info pic_info; pic_info.width = ctx->resize_width; // Output image width, supports resize, set 0 means no resize. pic_info.height = ctx->resize_height; // Output image height, supports resize, set 0 means no resize. - pic_info.width_stride = FFALIGN(ctx->vdec_width, VDEC_WIDTH_ALIGN); + pic_info.width_stride = FFALIGN(ctx->vdec_width, JPEG_DEC_WIDTH_ALIGN); pic_info.height_stride = FFALIGN(ctx->vdec_height, VDEC_HEIGHT_ALIGN); if (ctx->resize_str && ctx->resize_width != 0 && ctx->resize_height != 0) { - pic_info.width_stride = FFALIGN(ctx->resize_width, VDEC_WIDTH_ALIGN); + pic_info.width_stride = FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN); pic_info.height_stride = FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN); } - + av_log(avctx, AV_LOG_ERROR, "output width is %d.\n", pic_info.width); av_log(avctx, AV_LOG_ERROR, "output height is %d.\n", pic_info.height); av_log(avctx, AV_LOG_ERROR, "output width_stride is %d.\n", pic_info.width_stride); @@ -719,17 +719,17 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) return AVERROR_BUG; } - if (ctx->resize_width != FFALIGN(ctx->resize_width, VDEC_WIDTH_ALIGN) || + if (ctx->resize_width != FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN) || ctx->resize_height != FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN)) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be stride by %d and %d.\n", - ctx->resize_str, VDEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); return AVERROR_BUG; } if (ctx->resize_width < 128 || ctx->resize_height < 128 || ctx->resize_width > 4096 || ctx->resize_height > 4096) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be in [128x128 ~ 4096x4096].\n", - ctx->resize_str, VDEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); return AVERROR_BUG; } avctx->coded_width = ctx->resize_width; @@ -737,7 +737,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) } if (!ctx->resize_str || (ctx->resize_height == avctx->height && ctx->resize_width == avctx->width)) { - ctx->vdec_width = FFALIGN(avctx->width, VDEC_WIDTH_ALIGN); + ctx->vdec_width = FFALIGN(avctx->width, JPEG_DEC_WIDTH_ALIGN); ctx->vdec_height = FFALIGN(avctx->height, VDEC_HEIGHT_ALIGN); ctx->resize_width = ctx->resize_height = 0; } else { diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h index ea47d7a75..daaf010a2 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h @@ -40,8 +40,8 @@ #include "acl/dvpp/hi_dvpp.h" -#define VDEC_WIDTH_ALIGN 16 -#define VDEC_HEIGHT_ALIGN 2 +#define JPEG_DEC_WIDTH_ALIGN 16 +#define JEPG_DEC_HEIGHT_ALIGN 2 #define REF_FRAME_NUM 8 #define DISPLAY_FRAME_NUM 2 -- Gitee From 43a259eb1a52a0f091ed9efb73f92bf869d81d90 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 09:04:43 +0800 Subject: [PATCH 35/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index f5c38ec42..8c02b5cac 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -270,11 +270,11 @@ static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) pic_info.width = ctx->resize_width; // Output image width, supports resize, set 0 means no resize. pic_info.height = ctx->resize_height; // Output image height, supports resize, set 0 means no resize. pic_info.width_stride = FFALIGN(ctx->vdec_width, JPEG_DEC_WIDTH_ALIGN); - pic_info.height_stride = FFALIGN(ctx->vdec_height, VDEC_HEIGHT_ALIGN); + pic_info.height_stride = FFALIGN(ctx->vdec_height, JEPG_DEC_HEIGHT_ALIGN); if (ctx->resize_str && ctx->resize_width != 0 && ctx->resize_height != 0) { pic_info.width_stride = FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN); - pic_info.height_stride = FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN); + pic_info.height_stride = FFALIGN(ctx->resize_height, JEPG_DEC_HEIGHT_ALIGN); } av_log(avctx, AV_LOG_ERROR, "output width is %d.\n", pic_info.width); @@ -720,16 +720,16 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) } if (ctx->resize_width != FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN) || - ctx->resize_height != FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN)) { + ctx->resize_height != FFALIGN(ctx->resize_height, JEPG_DEC_HEIGHT_ALIGN)) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be stride by %d and %d.\n", - ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JEPG_DEC_HEIGHT_ALIGN); return AVERROR_BUG; } if (ctx->resize_width < 128 || ctx->resize_height < 128 || ctx->resize_width > 4096 || ctx->resize_height > 4096) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be in [128x128 ~ 4096x4096].\n", - ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JEPG_DEC_HEIGHT_ALIGN); return AVERROR_BUG; } avctx->coded_width = ctx->resize_width; @@ -738,7 +738,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) if (!ctx->resize_str || (ctx->resize_height == avctx->height && ctx->resize_width == avctx->width)) { ctx->vdec_width = FFALIGN(avctx->width, JPEG_DEC_WIDTH_ALIGN); - ctx->vdec_height = FFALIGN(avctx->height, VDEC_HEIGHT_ALIGN); + ctx->vdec_height = FFALIGN(avctx->height, JEPG_DEC_HEIGHT_ALIGN); ctx->resize_width = ctx->resize_height = 0; } else { av_log(avctx, AV_LOG_INFO, "Vdec resize: %dx%d.\n", ctx->resize_width, ctx->resize_height); -- Gitee From a3d55a261d440e376b943ca0963756b28eda5676 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 09:06:20 +0800 Subject: [PATCH 36/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 12 ++++++------ mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 8c02b5cac..b9c188cfa 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -270,11 +270,11 @@ static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) pic_info.width = ctx->resize_width; // Output image width, supports resize, set 0 means no resize. pic_info.height = ctx->resize_height; // Output image height, supports resize, set 0 means no resize. pic_info.width_stride = FFALIGN(ctx->vdec_width, JPEG_DEC_WIDTH_ALIGN); - pic_info.height_stride = FFALIGN(ctx->vdec_height, JEPG_DEC_HEIGHT_ALIGN); + pic_info.height_stride = FFALIGN(ctx->vdec_height, JPEG_DEC_HEIGHT_ALIGN); if (ctx->resize_str && ctx->resize_width != 0 && ctx->resize_height != 0) { pic_info.width_stride = FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN); - pic_info.height_stride = FFALIGN(ctx->resize_height, JEPG_DEC_HEIGHT_ALIGN); + pic_info.height_stride = FFALIGN(ctx->resize_height, JPEG_DEC_HEIGHT_ALIGN); } av_log(avctx, AV_LOG_ERROR, "output width is %d.\n", pic_info.width); @@ -720,16 +720,16 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) } if (ctx->resize_width != FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN) || - ctx->resize_height != FFALIGN(ctx->resize_height, JEPG_DEC_HEIGHT_ALIGN)) { + ctx->resize_height != FFALIGN(ctx->resize_height, JPEG_DEC_HEIGHT_ALIGN)) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be stride by %d and %d.\n", - ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JEPG_DEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JPEG_DEC_HEIGHT_ALIGN); return AVERROR_BUG; } if (ctx->resize_width < 128 || ctx->resize_height < 128 || ctx->resize_width > 4096 || ctx->resize_height > 4096) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be in [128x128 ~ 4096x4096].\n", - ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JEPG_DEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JPEG_DEC_HEIGHT_ALIGN); return AVERROR_BUG; } avctx->coded_width = ctx->resize_width; @@ -738,7 +738,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) if (!ctx->resize_str || (ctx->resize_height == avctx->height && ctx->resize_width == avctx->width)) { ctx->vdec_width = FFALIGN(avctx->width, JPEG_DEC_WIDTH_ALIGN); - ctx->vdec_height = FFALIGN(avctx->height, JEPG_DEC_HEIGHT_ALIGN); + ctx->vdec_height = FFALIGN(avctx->height, JPEG_DEC_HEIGHT_ALIGN); ctx->resize_width = ctx->resize_height = 0; } else { av_log(avctx, AV_LOG_INFO, "Vdec resize: %dx%d.\n", ctx->resize_width, ctx->resize_height); diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h index daaf010a2..ceaf4314c 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.h @@ -41,7 +41,7 @@ #include "acl/dvpp/hi_dvpp.h" #define JPEG_DEC_WIDTH_ALIGN 16 -#define JEPG_DEC_HEIGHT_ALIGN 2 +#define JPEG_DEC_HEIGHT_ALIGN 2 #define REF_FRAME_NUM 8 #define DISPLAY_FRAME_NUM 2 -- Gitee From acba7f52b6eecb07743bc8a669e5830a9a814e2f Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 09:41:32 +0800 Subject: [PATCH 37/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index b9c188cfa..d7f10ba21 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -274,7 +274,10 @@ static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) if (ctx->resize_str && ctx->resize_width != 0 && ctx->resize_height != 0) { pic_info.width_stride = FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN); - pic_info.height_stride = FFALIGN(ctx->resize_height, JPEG_DEC_HEIGHT_ALIGN); + pic_info.height_stride = FFALIGN(ctx->resize_height, + + + ); } av_log(avctx, AV_LOG_ERROR, "output width is %d.\n", pic_info.width); @@ -646,7 +649,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } return get_ret; } - + ctx->eos_received = 1; return get_ret; // while (ctx->decode_run_flag) { // if (!ctx->decoder_flushing) { -- Gitee From 3dbe3759354fd5b86f6c2b8eede70337c9e85f3c Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 10:18:55 +0800 Subject: [PATCH 38/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index d7f10ba21..005f8fc0c 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -982,7 +982,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) av_log(avctx, AV_LOG_ERROR, "Invalid pkt_timebase.\n"); } - sem_init(&ctx->eos_sema, 0, 0); + sem_post(&ctx->eos_sema); return 0; error: -- Gitee From 98feabd690236d521a0dcfa71be308b4e28e9824 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 10:22:21 +0800 Subject: [PATCH 39/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 005f8fc0c..6a55a5337 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -274,12 +274,9 @@ static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) if (ctx->resize_str && ctx->resize_width != 0 && ctx->resize_height != 0) { pic_info.width_stride = FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN); - pic_info.height_stride = FFALIGN(ctx->resize_height, - - - ); + pic_info.height_stride = FFALIGN(ctx->resize_height, JPEG_DEC_HEIGHT_ALIGN); } - + av_log(avctx, AV_LOG_ERROR, "output width is %d.\n", pic_info.width); av_log(avctx, AV_LOG_ERROR, "output height is %d.\n", pic_info.height); av_log(avctx, AV_LOG_ERROR, "output width_stride is %d.\n", pic_info.width_stride); @@ -649,7 +646,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } return get_ret; } - ctx->eos_received = 1; + return get_ret; // while (ctx->decode_run_flag) { // if (!ctx->decoder_flushing) { @@ -723,16 +720,16 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) } if (ctx->resize_width != FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN) || - ctx->resize_height != FFALIGN(ctx->resize_height, JPEG_DEC_HEIGHT_ALIGN)) { + ctx->resize_height != FFALIGN(ctx->resize_height, JEPG_DEC_HEIGHT_ALIGN)) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be stride by %d and %d.\n", - ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JPEG_DEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JEPG_DEC_HEIGHT_ALIGN); return AVERROR_BUG; } if (ctx->resize_width < 128 || ctx->resize_height < 128 || ctx->resize_width > 4096 || ctx->resize_height > 4096) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be in [128x128 ~ 4096x4096].\n", - ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JPEG_DEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JEPG_DEC_HEIGHT_ALIGN); return AVERROR_BUG; } avctx->coded_width = ctx->resize_width; @@ -741,7 +738,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) if (!ctx->resize_str || (ctx->resize_height == avctx->height && ctx->resize_width == avctx->width)) { ctx->vdec_width = FFALIGN(avctx->width, JPEG_DEC_WIDTH_ALIGN); - ctx->vdec_height = FFALIGN(avctx->height, JPEG_DEC_HEIGHT_ALIGN); + ctx->vdec_height = FFALIGN(avctx->height, JEPG_DEC_HEIGHT_ALIGN); ctx->resize_width = ctx->resize_height = 0; } else { av_log(avctx, AV_LOG_INFO, "Vdec resize: %dx%d.\n", ctx->resize_width, ctx->resize_height); -- Gitee From 272e602a7b6b300236a8cc212ea9fe54ba2711b7 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 10:25:01 +0800 Subject: [PATCH 40/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 6a55a5337..b2117729e 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -720,16 +720,16 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) } if (ctx->resize_width != FFALIGN(ctx->resize_width, JPEG_DEC_WIDTH_ALIGN) || - ctx->resize_height != FFALIGN(ctx->resize_height, JEPG_DEC_HEIGHT_ALIGN)) { + ctx->resize_height != FFALIGN(ctx->resize_height, JPEG_DEC_HEIGHT_ALIGN)) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be stride by %d and %d.\n", - ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JEPG_DEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JPEG_DEC_HEIGHT_ALIGN); return AVERROR_BUG; } if (ctx->resize_width < 128 || ctx->resize_height < 128 || ctx->resize_width > 4096 || ctx->resize_height > 4096) { av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be in [128x128 ~ 4096x4096].\n", - ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JEPG_DEC_HEIGHT_ALIGN); + ctx->resize_str, JPEG_DEC_WIDTH_ALIGN, JPEG_DEC_HEIGHT_ALIGN); return AVERROR_BUG; } avctx->coded_width = ctx->resize_width; @@ -738,7 +738,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) if (!ctx->resize_str || (ctx->resize_height == avctx->height && ctx->resize_width == avctx->width)) { ctx->vdec_width = FFALIGN(avctx->width, JPEG_DEC_WIDTH_ALIGN); - ctx->vdec_height = FFALIGN(avctx->height, JEPG_DEC_HEIGHT_ALIGN); + ctx->vdec_height = FFALIGN(avctx->height, JPEG_DEC_HEIGHT_ALIGN); ctx->resize_width = ctx->resize_height = 0; } else { av_log(avctx, AV_LOG_INFO, "Vdec resize: %dx%d.\n", ctx->resize_width, ctx->resize_height); -- Gitee From 5395be40e13fd2a61023a52673d9039406341db6 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 10:33:51 +0800 Subject: [PATCH 41/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index b2117729e..dee025a7b 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -646,7 +646,8 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } return get_ret; } - + sem_post(&ctx->eos_sema); + ff_himpi_decode_end(avctx); return get_ret; // while (ctx->decode_run_flag) { // if (!ctx->decoder_flushing) { @@ -979,7 +980,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) av_log(avctx, AV_LOG_ERROR, "Invalid pkt_timebase.\n"); } - sem_post(&ctx->eos_sema); + sem_init(&ctx->eos_sema, 0, 0); return 0; error: -- Gitee From b9a5be6ee54e162403467a034fda5ef2f2755a8f Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 10:37:23 +0800 Subject: [PATCH 42/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 1 + 1 file changed, 1 insertion(+) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index dee025a7b..31ce62f2f 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -646,6 +646,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } return get_ret; } + ctx->eos_received = 1; sem_post(&ctx->eos_sema); ff_himpi_decode_end(avctx); return get_ret; -- Gitee From 8b7428c45bfc27b7a94e3b0789f583dcc2598ee1 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 10:45:12 +0800 Subject: [PATCH 43/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 31ce62f2f..78e71ba4c 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -648,8 +648,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } ctx->eos_received = 1; sem_post(&ctx->eos_sema); - ff_himpi_decode_end(avctx); - return get_ret; + return AVERROR_EOF; // while (ctx->decode_run_flag) { // if (!ctx->decoder_flushing) { // send_ret = ff_decode_get_packet(avctx, &pkt); -- Gitee From 1f2aa9950b13ea763b91447db8345697021238d2 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 10:56:00 +0800 Subject: [PATCH 44/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 78e71ba4c..2240b0d4e 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -647,7 +647,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return get_ret; } ctx->eos_received = 1; - sem_post(&ctx->eos_sema); + ctx->decode_run_flag = 0; return AVERROR_EOF; // while (ctx->decode_run_flag) { // if (!ctx->decoder_flushing) { -- Gitee From 6e365d1a55bc4d110a4d42f7ffd5be1ed555a731 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 11:07:19 +0800 Subject: [PATCH 45/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 2240b0d4e..a781eb031 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -596,6 +596,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return AVERROR_BUG; } + av_log(ctx, AV_LOG_ERROR, "ctx->eos_received %d.\n", ctx->eos_received); if (ctx->eos_received) { return AVERROR_EOF; } @@ -606,8 +607,6 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) return ret; } - - // new send_ret = ff_decode_get_packet(avctx, &pkt); @@ -648,7 +647,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } ctx->eos_received = 1; ctx->decode_run_flag = 0; - return AVERROR_EOF; + return get_ret; // while (ctx->decode_run_flag) { // if (!ctx->decoder_flushing) { // send_ret = ff_decode_get_packet(avctx, &pkt); -- Gitee From 9f57dce2b602589f1d9096e906af71f0c6e9410c Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 11:15:01 +0800 Subject: [PATCH 46/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/decode.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mxVision/Ascendffmpeg/libavcodec/decode.c b/mxVision/Ascendffmpeg/libavcodec/decode.c index 936e5d63d..08def7235 100644 --- a/mxVision/Ascendffmpeg/libavcodec/decode.c +++ b/mxVision/Ascendffmpeg/libavcodec/decode.c @@ -543,7 +543,9 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) av_assert0(!frame->buf[0]); if (avctx->codec->receive_frame) { + av_log(avctx, AV_LOG_ERROR, "Here is 546 decode.c.\n"); ret = avctx->codec->receive_frame(avctx, frame); + av_log(avctx, AV_LOG_ERROR, "receive_frame ret is %d.\n", ret); if (ret != AVERROR(EAGAIN)) av_packet_unref(avci->last_pkt_props); } else -- Gitee From 6af7155303221b2d14a4f2584cc65b31bfc2ff86 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 11:28:34 +0800 Subject: [PATCH 47/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/decode.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/decode.c b/mxVision/Ascendffmpeg/libavcodec/decode.c index 08def7235..b1dd115ce 100644 --- a/mxVision/Ascendffmpeg/libavcodec/decode.c +++ b/mxVision/Ascendffmpeg/libavcodec/decode.c @@ -550,7 +550,7 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) av_packet_unref(avci->last_pkt_props); } else ret = decode_simple_receive_frame(avctx, frame); - + av_log(avctx, AV_LOG_ERROR, "AVERROR_EOF is %d.\n", AVERROR_EOF); if (ret == AVERROR_EOF) avci->draining_done = 1; @@ -616,6 +616,7 @@ int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacke } if (!avci->buffer_frame->buf[0]) { + av_log(avctx, AV_LOG_ERROR, "Here is avcodec_send_packet.\n"); ret = decode_receive_frame_internal(avctx, avci->buffer_frame); if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) return ret; @@ -664,6 +665,7 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr if (avci->buffer_frame->buf[0]) { av_frame_move_ref(frame, avci->buffer_frame); } else { + av_log(avctx, AV_LOG_ERROR, "here is avcodec_receive_frame "); ret = decode_receive_frame_internal(avctx, frame); if (ret < 0) return ret; -- Gitee From 95765e601f1eee55863b44776f40f70061119a45 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 11:41:28 +0800 Subject: [PATCH 48/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/decode.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/decode.c b/mxVision/Ascendffmpeg/libavcodec/decode.c index b1dd115ce..df521ebba 100644 --- a/mxVision/Ascendffmpeg/libavcodec/decode.c +++ b/mxVision/Ascendffmpeg/libavcodec/decode.c @@ -550,7 +550,7 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) av_packet_unref(avci->last_pkt_props); } else ret = decode_simple_receive_frame(avctx, frame); - av_log(avctx, AV_LOG_ERROR, "AVERROR_EOF is %d.\n", AVERROR_EOF); +// av_log(avctx, AV_LOG_ERROR, "AVERROR_EOF is %d.\n", AVERROR_EOF); if (ret == AVERROR_EOF) avci->draining_done = 1; @@ -616,7 +616,7 @@ int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacke } if (!avci->buffer_frame->buf[0]) { - av_log(avctx, AV_LOG_ERROR, "Here is avcodec_send_packet.\n"); +// av_log(avctx, AV_LOG_ERROR, "Here is avcodec_send_packet.\n"); ret = decode_receive_frame_internal(avctx, avci->buffer_frame); if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) return ret; @@ -665,7 +665,7 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr if (avci->buffer_frame->buf[0]) { av_frame_move_ref(frame, avci->buffer_frame); } else { - av_log(avctx, AV_LOG_ERROR, "here is avcodec_receive_frame "); +// av_log(avctx, AV_LOG_ERROR, "here is avcodec_receive_frame "); ret = decode_receive_frame_internal(avctx, frame); if (ret < 0) return ret; -- Gitee From 2e9a06cccb618bf49e142636900a0a886a19f9c4 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 11:47:03 +0800 Subject: [PATCH 49/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index a781eb031..63e929060 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -647,7 +647,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } ctx->eos_received = 1; ctx->decode_run_flag = 0; - return get_ret; + return AVERROR_EOF; // while (ctx->decode_run_flag) { // if (!ctx->decoder_flushing) { // send_ret = ff_decode_get_packet(avctx, &pkt); -- Gitee From fbc205460dee924bcb320d7a09256851c9b5978b Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 14:27:11 +0800 Subject: [PATCH 50/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 63e929060..d079cc6c1 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -632,21 +632,22 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) get_ret = himpi_get_frame(avctx, frame); av_log(avctx, AV_LOG_ERROR, "get ret is %d.\n", get_ret); av_log(avctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 1.\n"); - if (get_ret != 0 && get_ret != AVERROR_EOF) { - if (get_ret != AVERROR(EAGAIN)) { - return get_ret; - } - if (ctx->decoder_flushing) { - av_usleep(2000); - } - } else { - if (get_ret == AVERROR_EOF) { - ctx->eos_received = 1; - } - return get_ret; - } +// if (get_ret != 0 && get_ret != AVERROR_EOF) { +// if (get_ret != AVERROR(EAGAIN)) { +// return get_ret; +// } +// if (ctx->decoder_flushing) { +// av_usleep(2000); +// } +// } else { +// if (get_ret == AVERROR_EOF) { +// ctx->eos_received = 1; +// } +// return get_ret; +// } ctx->eos_received = 1; ctx->decode_run_flag = 0; + return AVERROR_EOF; // while (ctx->decode_run_flag) { // if (!ctx->decoder_flushing) { -- Gitee From b745100c4909082d9a9fc6aeaf330776708e358c Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 14:40:01 +0800 Subject: [PATCH 51/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/decode.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/decode.c b/mxVision/Ascendffmpeg/libavcodec/decode.c index df521ebba..3a7cf1fef 100644 --- a/mxVision/Ascendffmpeg/libavcodec/decode.c +++ b/mxVision/Ascendffmpeg/libavcodec/decode.c @@ -555,11 +555,15 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame) avci->draining_done = 1; if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS) && - IS_EMPTY(avci->last_pkt_props) && av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) - av_fifo_generic_read(avci->pkt_props, - avci->last_pkt_props, sizeof(*avci->last_pkt_props), NULL); + IS_EMPTY(avci->last_pkt_props) && av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) { + av_log(avctx, AV_LOG_ERROR, "Here is 559 decode.c.\n"); + av_fifo_generic_read(avci->pkt_props, + avci->last_pkt_props, sizeof(*avci->last_pkt_props), NULL); + } + if (!ret) { + av_log(avctx, AV_LOG_ERROR, "Here is 566 decode.c.\n"); frame->best_effort_timestamp = guess_correct_pts(avctx, frame->pts, frame->pkt_dts); @@ -616,7 +620,7 @@ int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacke } if (!avci->buffer_frame->buf[0]) { -// av_log(avctx, AV_LOG_ERROR, "Here is avcodec_send_packet.\n"); + av_log(avctx, AV_LOG_ERROR, "Here is avcodec_send_packet.\n"); ret = decode_receive_frame_internal(avctx, avci->buffer_frame); if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) return ret; @@ -665,7 +669,7 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr if (avci->buffer_frame->buf[0]) { av_frame_move_ref(frame, avci->buffer_frame); } else { -// av_log(avctx, AV_LOG_ERROR, "here is avcodec_receive_frame "); + av_log(avctx, AV_LOG_ERROR, "here is avcodec_receive_frame "); ret = decode_receive_frame_internal(avctx, frame); if (ret < 0) return ret; -- Gitee From 5ae77c88622474508277b8997030e884834a0df9 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 14:54:48 +0800 Subject: [PATCH 52/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/decode.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/decode.c b/mxVision/Ascendffmpeg/libavcodec/decode.c index 3a7cf1fef..b3eb3b743 100644 --- a/mxVision/Ascendffmpeg/libavcodec/decode.c +++ b/mxVision/Ascendffmpeg/libavcodec/decode.c @@ -819,6 +819,7 @@ static int compat_decode(AVCodecContext *avctx, AVFrame *frame, while (ret >= 0) { ret = avcodec_receive_frame(avctx, frame); + av_log(avctx, AV_LOG_ERROR, "compat_decode.\n"); if (ret < 0) { if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) ret = 0; @@ -851,8 +852,11 @@ static int compat_decode(AVCodecContext *avctx, AVFrame *frame, finish: if (ret == 0) { /* if there are any bsfs then assume full packet is always consumed */ - if (avctx->codec->bsfs) + if (avctx->codec->bsfs) { + av_log(avctx, AV_LOG_ERROR, "856.\n"); ret = pkt->size; + } + else ret = FFMIN(avci->compat_decode_consumed, pkt->size); } -- Gitee From 77765ed24b4aab76efc8363117cf33bf80bb6ca4 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 15:01:14 +0800 Subject: [PATCH 53/60] ffmpeg --- mxVision/Ascendffmpeg/libavcodec/decode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/decode.c b/mxVision/Ascendffmpeg/libavcodec/decode.c index b3eb3b743..666fb0a05 100644 --- a/mxVision/Ascendffmpeg/libavcodec/decode.c +++ b/mxVision/Ascendffmpeg/libavcodec/decode.c @@ -862,7 +862,7 @@ finish: } avci->compat_decode_consumed = 0; avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0; - + av_log(avctx, AV_LOG_ERROR, "ret is %d.\n", ret); return ret; } @@ -870,6 +870,7 @@ int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *pi int *got_picture_ptr, const AVPacket *avpkt) { + av_log(avctx, AV_LOG_ERROR, "avcodec_decode_video2.\n"); return compat_decode(avctx, picture, got_picture_ptr, avpkt); } -- Gitee From 572dd0b810f98130e59cf0e643b5190474129d86 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Wed, 4 Sep 2024 19:02:38 +0800 Subject: [PATCH 54/60] ffmpeg --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index d079cc6c1..46493fef7 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -632,23 +632,22 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) get_ret = himpi_get_frame(avctx, frame); av_log(avctx, AV_LOG_ERROR, "get ret is %d.\n", get_ret); av_log(avctx, AV_LOG_ERROR, "here is ff_himpi_receive_frame 1.\n"); -// if (get_ret != 0 && get_ret != AVERROR_EOF) { -// if (get_ret != AVERROR(EAGAIN)) { -// return get_ret; -// } -// if (ctx->decoder_flushing) { -// av_usleep(2000); -// } -// } else { -// if (get_ret == AVERROR_EOF) { -// ctx->eos_received = 1; -// } -// return get_ret; -// } + if (get_ret != 0 && get_ret != AVERROR_EOF) { + if (get_ret != AVERROR(EAGAIN)) { + return get_ret; + } + if (ctx->decoder_flushing) { + av_usleep(2000); + } + } else { + if (get_ret == AVERROR_EOF) { + ctx->eos_received = 1; + } + return get_ret; + } ctx->eos_received = 1; - ctx->decode_run_flag = 0; - return AVERROR_EOF; + return AVERROR_BUG; // while (ctx->decode_run_flag) { // if (!ctx->decoder_flushing) { // send_ret = ff_decode_get_packet(avctx, &pkt); @@ -882,13 +881,13 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) // return AVERROR_BUG; // } - ctx->frame_queue = av_fifo_alloc(1000 * sizeof(FrameInfo_t)); + ctx->frame_queue = av_fifo_alloc(1 * sizeof(FrameInfo_t)); if (!ctx->frame_queue) { av_log(avctx, AV_LOG_ERROR, "Failed to alloc frame fifo queue.\n"); goto error; } - ctx->dts_queue = av_fifo_alloc(1000 * sizeof(int64_t)); + ctx->dts_queue = av_fifo_alloc(1 * sizeof(int64_t)); if (!ctx->dts_queue) { av_log(avctx, AV_LOG_ERROR, "Failed to alloc dts fifo queue.\n"); goto error; -- Gitee From 7c8cdd4738e5d89f9faa33a41be4144afe5c14cd Mon Sep 17 00:00:00 2001 From: Malanchi Date: Thu, 5 Sep 2024 16:00:19 +0800 Subject: [PATCH 55/60] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 46493fef7..4ab6f716b 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -624,7 +624,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) av_log(ctx, AV_LOG_ERROR, "start to get frame.\n"); ret = get_frame(avctx); - if (ret != 0) { + if (ret != 0 && ret != HI_ERR_VDEC_BUF_EMPTY) { av_log(avctx, AV_LOG_ERROR, "get_frame failed, ret is %d.\n", ret); return ret; } -- Gitee From 115a4b6d95929f17548c2fe6f53531a700561906 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Fri, 6 Sep 2024 10:07:15 +0800 Subject: [PATCH 56/60] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mxVision/Ascendffmpeg/libavcodec/Makefile | 2 + mxVision/Ascendffmpeg/libavcodec/allcodecs.c | 1 + .../libavcodec/ascend_mjpeg_dec.c | 884 ++++++++++++++++++ .../libavcodec/ascend_mjpeg_dec.h | 148 +++ 4 files changed, 1035 insertions(+) create mode 100644 mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c create mode 100644 mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.h diff --git a/mxVision/Ascendffmpeg/libavcodec/Makefile b/mxVision/Ascendffmpeg/libavcodec/Makefile index 31d43500e..91c3bfdf9 100644 --- a/mxVision/Ascendffmpeg/libavcodec/Makefile +++ b/mxVision/Ascendffmpeg/libavcodec/Makefile @@ -28,6 +28,7 @@ HEADERS = ac3_parser.h \ ascend_dec.h \ ascend_enc.h \ ascend_jpeg_dec.h \ + ascend_mjpeg_dec.h \ OBJS = ac3_parser.o \ adts_parser.o \ @@ -376,6 +377,7 @@ OBJS-$(CONFIG_H264_DECODER) += h264dec.o h264_cabac.o h264_cavlc.o \ OBJS-$(CONFIG_H264_ASCEND_DECODER) += ascend_dec.o OBJS-$(CONFIG_H264_ASCEND_ENCODER) += ascend_enc.o OBJS-$(CONFIG_JPEG_ASCEND_DECODER) += ascend_jpeg_dec.o +OBJS-$(CONFIG_MJPEG_ASCEND_DECODER) += ascend_mjpeg_dec.o OBJS-$(CONFIG_H264_AMF_ENCODER) += amfenc_h264.o OBJS-$(CONFIG_H264_CUVID_DECODER) += cuviddec.o OBJS-$(CONFIG_H264_MEDIACODEC_DECODER) += mediacodecdec.o diff --git a/mxVision/Ascendffmpeg/libavcodec/allcodecs.c b/mxVision/Ascendffmpeg/libavcodec/allcodecs.c index c34405e9b..f650814a9 100644 --- a/mxVision/Ascendffmpeg/libavcodec/allcodecs.c +++ b/mxVision/Ascendffmpeg/libavcodec/allcodecs.c @@ -792,6 +792,7 @@ extern AVCodec ff_h265_ascend_decoder; extern AVCodec ff_h264_ascend_encoder; extern AVCodec ff_h265_ascend_encoder; extern AVCodec ff_jpeg_ascend_decoder; +extern AVCodec ff_mjpeg_ascend_decoder; extern AVCodec ff_h264_amf_encoder; extern AVCodec ff_h264_cuvid_decoder; extern AVCodec ff_h264_mf_encoder; diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c new file mode 100644 index 000000000..44f99dd79 --- /dev/null +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c @@ -0,0 +1,884 @@ +/* + * Copyright(c) 2020. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except int compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include "ascend_dec.h" + +static void *get_frame(void *arg) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)arg; + int ret = 0; + int eos_flag = 0; + ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); + return ((void*) (-1)); + } + + hi_video_frame_info frame; + hi_vdec_stream stream; + hi_vdec_supplement_info stSupplement; + + av_log(NULL, AV_LOG_INFO, "Thread start.\n"); + + while (ctx->thread_run_flag) { + ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); + if (ret != 0) { + if (ctx->decoder_flushing && ret == HI_ERR_VDEC_BUF_EMPTY) { + eos_flag = 1; + av_log(ctx, AV_LOG_DEBUG, "Decoder flushing or stream eos.\n"); + } else { + av_log(ctx, AV_LOG_DEBUG, "HiMpi get frame failed, ret is %d.\n", ret); + continue; + } + } + + size_t decResult = frame.v_frame.frame_flag; + if (eos_flag) { + // eos + FrameInfo_t frame_info; + memset(&frame_info, 0, sizeof(FrameInfo_t)); + frame_info.event_type = EVENT_EOS; + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + sem_post(&ctx->eos_sema); + av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); + break; + } + + hi_mpi_dvpp_free(stream.addr); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); + } + if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { + hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); + } + + if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { + ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); + return ((void*) (-1)); + } + continue; + } + FrameInfo_t frame_info; + frame_info.ascend_ctx = ctx; + get_vdec_frame_info(&frame_info, frame); + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); + av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + + ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); + return ((void*) (-1)); + } + + ctx->total_out_frame_count++; + } + return NULL; +} + +static inline int decode_params_checking(AVCodecContext* avctx) +{ + switch (avctx->codec->id) { + case AV_CODEC_ID_MJPEG: + if (avctx->width < 128 || avctx->height < 128 || + avctx->width > 4096 || avctx->height > 4096) { + av_log(avctx, AV_LOG_ERROR, + "H264 decoder only support resolution: 128x128 ~ 4096x4096, now: %dx%d.\n", + avctx->width, avctx->height); + return -1; + } + break; + + default: + break; + } + + return 0; +} + +static av_cold int ff_himpi_decode_end(AVCodecContext *avctx) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + int ret = 0; + int semvalue = 0; + struct timespec ts; + if (ctx == NULL || avctx->priv_data == NULL) { + av_log(ctx, AV_LOG_ERROR, "HiMpi decode end error, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } + + ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "Set Context failed, ret is %d.\n", ret); + return ret; + } + + clock_gettime(CLOCK_REALTIME, &ts); + ts.tv_sec += 3; + if (sem_timedwait(&(ctx->eos_sema), &ts) == -1) { + semvalue = -1; + sem_getvalue(&ctx->eos_sema, &semvalue); + av_log(ctx, AV_LOG_ERROR, "Decode sem_timewait = -1, semvalue = %d.\n", semvalue); + } + + if (ctx->hi_mpi_init_flag) { + ret = hi_mpi_vdec_stop_recv_stream(ctx->channel_id); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi stop receive stream failed, ret is %d.\n", ret); + return ret; + } + + ret = hi_mpi_vdec_destroy_chn(ctx->channel_id); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi destroy channel failed, ret is %d.\n", ret); + return ret; + } + + ret = hi_mpi_sys_exit(); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "HiMpi sys exit failed, ret is %d.\n", ret); + return ret; + } + } + + ctx->hi_mpi_init_flag = 0; + ctx->decode_run_flag = 0; + + if (ctx->thread_run_flag) { + ctx->thread_run_flag = 0; + pthread_join(ctx->thread_id, NULL); + } + + sem_destroy(&ctx->eos_sema); + + if (ctx->frame_queue) { + av_fifo_freep(&ctx->frame_queue); + ctx->frame_queue = NULL; + } + + ff_mutex_destroy(&ctx->queue_mutex); + if (ctx->bsf) { + av_bsf_free(&ctx->bsf); + ctx->bsf = NULL; + } + + av_buffer_unref(&ctx->hw_frame_ref); + av_buffer_unref(&ctx->hw_device_ref); + + av_log(avctx, AV_LOG_INFO, "Decode hw send packet count is: %llu.\n", ctx->total_out_frame_count); + av_log(avctx, AV_LOG_INFO, "Decode hw out frame count is: %llu.\n", ctx->total_packet_count); + + return 0; +} + +static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + int ret = 0; + if (ctx->first_packet) { + if (avctx->extradata_size) { + uint8_t* streamBuffer = NULL; + ret = hi_mpi_dvpp_malloc(ctx->device_id, &streamBuffer, avctx->extradata_size); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi malloc first packet failed, ret is %d.\n", ret); + return ret; + } + ret = aclrtMemcpy(streamBuffer, avctx->extradata_size, avctx->extradata, avctx->extradata_size, + ACL_MEMCPY_HOST_TO_DEVICE); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy H2D first packet failed, ret is %d.\n", ret); + return ret; + } + + hi_vdec_stream stream; + stream.pts = avpkt->pts; + stream.addr = streamBuffer; + stream.len = avctx->extradata_size; + stream.end_of_frame = HI_TRUE; + stream.end_of_stream = HI_FALSE; + stream.need_display = HI_FALSE; + + hi_vdec_pic_info pic_info; + pic_info.vir_addr = 0; + pic_info.buffer_size = 0; + pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec send first packet failed, ret is %d.\n", ret); + return ret; + } + } + ctx->first_packet = 0; + } + + uint8_t* streamBuffer = NULL; + ret = hi_mpi_dvpp_malloc(ctx->device_id, &streamBuffer, avpkt->size); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi malloc packet failed, ret is %d.\n", ret); + return ret; + } + + ret = aclrtMemcpy(streamBuffer, avpkt->size, avpkt->data, avpkt->size, + ACL_MEMCPY_HOST_TO_DEVICE); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy H2D first packet failed, ret is %d.\n", ret); + return ret; + } + + // create stream info + hi_vdec_stream stream; + stream.pts = avpkt->pts; + stream.addr = streamBuffer; + stream.len = avpkt->size; + stream.end_of_frame = HI_TRUE; + stream.end_of_stream = HI_FALSE; + stream.need_display = HI_TRUE; + + ff_mutex_lock(&ctx->queue_mutex); + av_fifo_generic_write(ctx->dts_queue, &avpkt->dts, sizeof(int64_t), NULL); + ff_mutex_unlock(&ctx->queue_mutex); + + // create frame info + hi_vdec_pic_info pic_info; + pic_info.width = ctx->resize_width; // Output image width, supports resize, set 0 means no resize. + pic_info.height = ctx->resize_height; // Output image height, supports resize, set 0 means no resize. + pic_info.width_stride = FFALIGN(ctx->vdec_width, VDEC_WIDTH_ALIGN); + pic_info.height_stride = FFALIGN(ctx->vdec_height, VDEC_HEIGHT_ALIGN); + if (ctx->resize_str && ctx->resize_width != 0 && ctx->resize_height != 0) { + pic_info.width_stride = FFALIGN(ctx->resize_width, VDEC_WIDTH_ALIGN); + pic_info.height_stride = FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN); + } + uint32_t size = pic_info.width_stride * pic_info.height_stride * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; + + pic_info.buffer_size = size; + pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + void *picBuffer = NULL; + + ret = hi_mpi_dvpp_malloc(ctx->device_id, &picBuffer, size); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi malloc failed, ret is %d.\n", ret); + return ret; + } + pic_info.vir_addr = (uint64_t)picBuffer; + + do { + ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); + if ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL) { + usleep(VDEC_SLEEP_TIME); + } + } while ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL); + + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi send stream failed, ret is %d.\n", ret); + return ret; + } + + ctx->frame_id++; + ctx->total_packet_count++; + return 0; +} + +static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*) avctx->priv_data; + int ret = 0; + AVPacket packet = { 0 }; + AVPacket bsf_packet = { 0 }; + + if (avpkt && avpkt->size && ctx->bsf) { + ret = av_packet_ref(&packet, avpkt); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed, ret(%d).\n", ret); + return ret; + } + ret = av_bsf_send_packet(ctx->bsf, &packet); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed, ret(%d).\n", ret); + av_packet_unref(&packet); + return ret; + } + ret = av_bsf_receive_packet(ctx->bsf, &bsf_packet); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed, ret(%d).\n", ret); + return ret; + } + avpkt = &bsf_packet; + } + av_packet_unref(&packet); + + if (avpkt && avpkt->size) { + ret = malloc_and_send_frame(avctx, avpkt); + if (ret != 0) { + av_packet_unref(avpkt); + return AVERROR(EINVAL); + } + } else { + if (!ctx->decoder_flushing) { + hi_vdec_stream stream; + stream.addr = NULL; + stream.len = 0; + stream.end_of_frame = HI_FALSE; + stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. + stream.need_display = HI_TRUE; + + hi_vdec_pic_info pic_info; + pic_info.vir_addr = 0; + pic_info.buffer_size = 0; + pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); + if (ret != 0) { + av_packet_unref(avpkt); + av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); + return ret; + } + ctx->decoder_flushing = 1; + } + } + av_packet_unref(avpkt); + return 0; +} + +static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + int ret = 0; + if (!ctx->frame_queue) { + return AVERROR(EAGAIN); + } + + FrameInfo_t frame_info; + ff_mutex_lock(&ctx->queue_mutex); + if (av_fifo_size(ctx->frame_queue) != 0) { + av_fifo_generic_read(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); + } else { + ff_mutex_unlock(&ctx->queue_mutex); + return AVERROR(EAGAIN); + } + ff_mutex_unlock(&ctx->queue_mutex); + + if (frame_info.event_type == EVENT_EOS) { + return AVERROR_EOF; + } + + if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { + ret = av_hwframe_get_buffer(ctx->hw_frame_ref, frame, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed, ret is %d.\n", ret); + return AVERROR(EINVAL); + } + ret = ff_decode_frame_props(avctx, frame); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed, ret is %d.\n", ret); + return AVERROR(EINVAL); + } + } else { + ret = ff_get_buffer(avctx, frame, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Decode ff_get_buffer failed, ret is %d.\n", ret); + return AVERROR(EINVAL); + } + } + + frame->pkt_pos = -1; + frame->pkt_duration = 0; + frame->pkt_size = -1; + frame->pts = frame_info.pts; + frame->pkt_pts = frame->pts; + frame->pkt_dts = frame_info.dts; + frame->width = frame_info.width_stride; + frame->height = frame_info.height_stride; + + switch (frame_info.format) { + case HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420: + if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { + uint32_t offset = 0; + for (int i = 0; i < 2; i++) { + size_t dstBytes = frame->width * frame->height * (i ? 1.0 / 2 : 1); + ret = aclrtMemcpy(frame->data[i], dstBytes, frame_info.data + offset, dstBytes, + ACL_MEMCPY_DEVICE_TO_DEVICE); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy D2D failed, ret is %d.\n", ret); + hi_mpi_dvpp_free(frame_info.data); + return ret; + } + offset += dstBytes; + } + } else { + uint32_t offset = 0; + for (int i = 0; i < 2; i++) { + size_t dstBytes = frame->width * frame->height * (i ? 1.0 / 2 : 1); + ret = aclrtMemcpy(frame->data[i], dstBytes, frame_info.data + offset, dstBytes, + ACL_MEMCPY_DEVICE_TO_HOST); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy D2H failed, ret is %d.\n", ret); + hi_mpi_dvpp_free(frame_info.data); + return ret; + } + offset += dstBytes; + } + } + ret = hi_mpi_dvpp_free(frame_info.data); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi free data failed, ret is %d.\n", ret); + } + break; + + default: + hi_mpi_dvpp_free(frame_info.data); + av_log(avctx, AV_LOG_ERROR, "Unsupport pixfmt: %d.\n", (int)frame_info.format); + break; + } + + + return 0; +} + +static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + AVPacket pkt = { 0 }; + int send_ret = -1; + int get_ret = -1; + int ret = 0; + + if (avctx == NULL || avctx->priv_data == NULL) { + av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } + if (!ctx->hi_mpi_init_flag || !ctx->thread_run_flag || !ctx->decode_run_flag) { + av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } + + if (ctx->eos_received) { + return AVERROR_EOF; + } + + ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); + if (ret != 0) { + av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); + return ret; + } + + while (ctx->decode_run_flag) { + if (!ctx->decoder_flushing) { + send_ret = ff_decode_get_packet(avctx, &pkt); + if (send_ret < 0 && send_ret != AVERROR_EOF) { + return send_ret; + } + send_ret = hi_mpi_decode(avctx, &pkt); + av_packet_unref(&pkt); + if (send_ret < 0 && send_ret != AVERROR_EOF) { + av_log(ctx, AV_LOG_ERROR, "Send packet failed, ret is %d.\n", send_ret); + return send_ret; + } + } + + get_ret = himpi_get_frame(avctx, frame); + if (get_ret != 0 && get_ret != AVERROR_EOF) { + if (get_ret != AVERROR(EAGAIN)) { + return get_ret; + } + if (ctx->decoder_flushing) { + av_usleep(2000); + } + } else { + if (get_ret == AVERROR_EOF) { + ctx->eos_received = 1; + } + return get_ret; + } + } + + av_log(avctx, AV_LOG_ERROR, "Decode stop, error.\n"); + return AVERROR_BUG; + +} + +static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) +{ + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; + AVASCENDDeviceContext *hw_device_ctx; + AVHWFramesContext *hw_frame_ctx; + const AVBitStreamFilter *bsf; + int ret = 0; + + if (avctx == NULL || avctx->priv_data == NULL) { + av_log(avctx, AV_LOG_ERROR, "HiMpi decoder init failed, AVCodecContext is NULL.\n"); + return AVERROR_BUG; + } + + if (ctx->hi_mpi_init_flag == 1) { + av_log(avctx, AV_LOG_ERROR, "Error, himpi decode double init. \n"); + return AVERROR_BUG; + } + + enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_ASCEND, AV_PIX_FMT_NV12, AV_PIX_FMT_NONE }; + avctx->pix_fmt = ff_get_format(avctx, pix_fmts); + av_log(avctx, AV_LOG_ERROR, "Error, ff_get_format failed with format id: %d.\n", avctx->pix_fmt); + if (avctx->pix_fmt < 0) { + av_log(avctx, AV_LOG_ERROR, "Error, ff_get_format failed with format id: %d.\n", avctx->pix_fmt); + return AVERROR_BUG; + } + + ctx->avctx = avctx; + + if (ctx->resize_str) { + ret = av_parse_video_size(&ctx->resize_width, &ctx->resize_height, ctx->resize_str); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be {width}x{height}.\n", + ctx->resize_str); + return AVERROR_BUG; + } + + if (ctx->resize_width != FFALIGN(ctx->resize_width, VDEC_WIDTH_ALIGN) || + ctx->resize_height != FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN)) { + av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be stride by %d and %d.\n", + ctx->resize_str, VDEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); + return AVERROR_BUG; + } + + if (ctx->resize_width < 128 || ctx->resize_height < 128 || + ctx->resize_width > 4096 || ctx->resize_height > 4096) { + av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be in [128x128 ~ 4096x4096].\n", + ctx->resize_str, VDEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); + return AVERROR_BUG; + } + avctx->coded_width = ctx->resize_width; + avctx->coded_height = ctx->resize_height; + } + + if (!ctx->resize_str || (ctx->resize_height == avctx->height && ctx->resize_width == avctx->width)) { + ctx->vdec_width = FFALIGN(avctx->width, VDEC_WIDTH_ALIGN); + ctx->vdec_height = FFALIGN(avctx->height, VDEC_HEIGHT_ALIGN); + ctx->resize_width = ctx->resize_height = 0; + } else { + av_log(avctx, AV_LOG_INFO, "Vdec resize: %dx%d.\n", ctx->resize_width, ctx->resize_height); + } + + ctx->vdec_width = avctx->width; + ctx->vdec_height = avctx->height; + + if (decode_params_checking(avctx) != 0) { + return AVERROR(EINVAL); + } + av_log(avctx, AV_LOG_DEBUG, "Vdec width: %d.\n", ctx->vdec_width); + av_log(avctx, AV_LOG_DEBUG, "Vdec height: %d.\n", ctx->vdec_height); + + if (avctx->hw_frames_ctx) { + av_buffer_unref(&ctx->hw_frame_ref); + ctx->hw_frame_ref = av_buffer_ref(avctx->hw_frames_ctx); + if (!ctx->hw_frame_ref) { + ret = AVERROR(EINVAL); + goto error; + } + + hw_frame_ctx = (AVHWFramesContext*)ctx->hw_frame_ref->data; + if (!hw_frame_ctx->pool || + (ctx->vdec_width != hw_frame_ctx->width && ctx->resize_width != hw_frame_ctx->width)) { + if (hw_frame_ctx->pool) { + av_buffer_pool_uninit(&hw_frame_ctx->pool); + } + hw_frame_ctx->width = ctx->resize_width == 0 ? ctx->vdec_width : ctx->resize_width; + hw_frame_ctx->height = ctx->resize_height == 0 ? ctx->vdec_height : ctx->resize_height; + hw_frame_ctx->initial_pool_size = 2; + hw_frame_ctx->format = AV_PIX_FMT_ASCEND; + hw_frame_ctx->sw_format = avctx->sw_pix_fmt; + + ret = av_hwframe_ctx_init(ctx->hw_frame_ref); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "HWFrame contex init failed.\n"); + return AVERROR(ENAVAIL); + } + } + ctx->hw_device_ref = av_buffer_ref(hw_frame_ctx->device_ref); + if (!ctx->hw_device_ref) { + av_log(avctx, AV_LOG_ERROR, "Get hw_device_ref failed.\n"); + ret = AVERROR(EINVAL); + goto error; + } + } else { + if (avctx->hw_device_ctx) { + ctx->hw_device_ref = av_buffer_ref(avctx->hw_device_ctx); + if (!ctx->hw_device_ref) { + av_log(avctx, AV_LOG_ERROR, "ref hwdevice failed.\n"); + ret = AVERROR(EINVAL); + goto error; + } + } else { + char dev_idx[sizeof(int)]; + sprintf(dev_idx, "%d", ctx->device_id); + av_log(avctx, AV_LOG_INFO, "dev_idx: %s.\n", dev_idx); + ret = av_hwdevice_ctx_create(&ctx->hw_device_ref, AV_HWDEVICE_TYPE_ASCEND, dev_idx, NULL, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "hwdevice contex create failed.\n"); + goto error; + } + } + ctx->hw_frame_ref = av_hwframe_ctx_alloc(ctx->hw_device_ref); + if (!ctx->hw_frame_ref) { + av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed, ret is %d.\n", ret); + ret = AVERROR(EINVAL); + goto error; + } + hw_frame_ctx = (AVHWFramesContext*)ctx->hw_frame_ref->data; + if (!hw_frame_ctx->pool) { + hw_frame_ctx->width = ctx->resize_width == 0 ? ctx->vdec_width : ctx->resize_width; + hw_frame_ctx->height = ctx->resize_height == 0 ? ctx->vdec_height : ctx->resize_height; + hw_frame_ctx->initial_pool_size = 2; + hw_frame_ctx->format = AV_PIX_FMT_ASCEND; + hw_frame_ctx->sw_format = avctx->sw_pix_fmt; + ret = av_hwframe_ctx_init(ctx->hw_frame_ref); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "hwframe ctx init error, ret is %d.\n", ret); + return AVERROR(EINVAL); + } + } + } + hw_device_ctx = ((AVHWDeviceContext*)ctx->hw_device_ref->data)->hwctx; + ctx->hw_device_ctx = hw_device_ctx; + ctx->hw_frames_ctx = hw_frame_ctx; + ctx->ascend_ctx = ctx->hw_device_ctx->ascend_ctx; + + ctx->device_id = ctx->ascend_ctx->device_id; + ctx->frame_id = 0; + ctx->eos_received = 0; + ctx->total_out_frame_count = 0; + ctx->total_packet_count = 0; + ctx->decoder_flushing = 0; + ctx->first_packet = 1; + + ff_mutex_init(&ctx->queue_mutex, NULL); + ctx->codec_type = HI_PT_JPEG; + +// switch (avctx->codec->id) +// { +// case AV_CODEC_ID_H264: +// ctx->codec_type = HI_PT_H264; +// break; +// case AV_CODEC_ID_H265: +// ctx->codec_type = HI_PT_H265; +// break; +// default: +// av_log(avctx, AV_LOG_ERROR, "Invalid codec type, %d.\n", avctx->codec->id); +// return AVERROR_BUG; +// } +// ctx->bsf = NULL; +// if (avctx->codec->id == AV_CODEC_ID_H264 || avctx->codec->id == AV_CODEC_ID_H265) { +// if (avctx->codec->id == AV_CODEC_ID_H264) +// bsf = av_bsf_get_by_name("h264_mp4toannexb"); +// else if (avctx->codec->id == AV_CODEC_ID_H265) +// bsf = av_bsf_get_by_name("hevc_mp4toannexb"); +// if (!bsf) { +// ret = AVERROR_BSF_NOT_FOUND; +// goto error; +// } +// ret = av_bsf_alloc(bsf, &ctx->bsf); +// if (ret < 0) +// goto error; +// +// ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx); +// if (ret < 0) { +// av_bsf_free(&ctx->bsf); +// goto error; +// } +// ret = av_bsf_init(ctx->bsf); +// if (ret < 0) { +// av_bsf_free(&ctx->bsf); +// goto error; +// } +// } else { +// av_log(avctx, AV_LOG_ERROR, "Invalid codec id, %d.\n", avctx->codec->id); +// return AVERROR_BUG; +// } + + ctx->frame_queue = av_fifo_alloc(1000 * sizeof(FrameInfo_t)); + if (!ctx->frame_queue) { + av_log(avctx, AV_LOG_ERROR, "Failed to alloc frame fifo queue.\n"); + goto error; + } + + ctx->dts_queue = av_fifo_alloc(1000 * sizeof(int64_t)); + if (!ctx->dts_queue) { + av_log(avctx, AV_LOG_ERROR, "Failed to alloc dts fifo queue.\n"); + goto error; + } + + ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); + goto error; + } + + ret = hi_mpi_sys_init(); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi sys init failed, ret is %d.\n", ret); + goto error; + } + + ctx->chn_attr_.type = ctx->codec_type; + ctx->chn_attr_.mode = HI_VDEC_SEND_MODE_FRAME; + ctx->chn_attr_.pic_width = ctx->vdec_width; + ctx->chn_attr_.pic_height = ctx->vdec_height; + + // Stream buffer size, Recommended value is width * height * 3 / 2 + ctx->chn_attr_.stream_buf_size = ctx->vdec_width * ctx->vdec_height * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; + ctx->chn_attr_.frame_buf_cnt = REF_FRAME_NUM + DISPLAY_FRAME_NUM + 1; + + // Create buf attribute + ctx->buf_attr_.width = ctx->chn_attr_.pic_width; + ctx->buf_attr_.height = ctx->chn_attr_.pic_height; + ctx->buf_attr_.align = 0; + ctx->buf_attr_.bit_width = HI_DATA_BIT_WIDTH_8; + ctx->buf_attr_.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + ctx->buf_attr_.compress_mode = HI_COMPRESS_MODE_NONE; + + ctx->chn_attr_.frame_buf_size = hi_vdec_get_pic_buf_size(ctx->chn_attr_.type, &ctx->buf_attr_); + + // Configure video decoder channel attribute + ctx->chn_attr_.video_attr.ref_frame_num = REF_FRAME_NUM; + ctx->chn_attr_.video_attr.temporal_mvp_en = HI_TRUE; + ctx->chn_attr_.video_attr.tmv_buf_size = hi_vdec_get_tmv_buf_size(ctx->chn_attr_.type, + ctx->chn_attr_.pic_width, + ctx->chn_attr_.pic_height); + + av_log(avctx, AV_LOG_INFO, "Channel Id is: %d.\n", ctx->channel_id); + ret = hi_mpi_vdec_create_chn(ctx->channel_id, &ctx->chn_attr_); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi create vdec channel failed, ret is %d.\n", ret); + goto error; + } + + // reset channel param. + ret = hi_mpi_vdec_get_chn_param(ctx->channel_id, &ctx->chn_param_); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec get channel param failed, ret is %d.\n", ret); + goto error; + } + + ctx->chn_param_.video_param.dec_mode = HI_VIDEO_DEC_MODE_IPB; + ctx->chn_param_.video_param.compress_mode = HI_COMPRESS_MODE_HFBC; + ctx->chn_param_.video_param.video_format = HI_VIDEO_FORMAT_TILE_64x16; + ctx->chn_param_.display_frame_num = DISPLAY_FRAME_NUM; + ctx->chn_param_.video_param.out_order = HI_VIDEO_OUT_ORDER_DISPLAY; + + ret = hi_mpi_vdec_set_chn_param(ctx->channel_id, &ctx->chn_param_); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec set channel param failed, ret is %d.\n", ret); + goto error; + } + + ret = hi_mpi_vdec_start_recv_stream(ctx->channel_id); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec start receive stream failed, ret is %d.\n", ret); + goto error; + } + ctx->hi_mpi_init_flag = 1; + ctx->decode_run_flag = 1; + + // create callback thread + ctx->thread_run_flag = 1; + ret = pthread_create(&ctx->thread_id, NULL, get_frame, (void *)ctx); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "pthread_create callback thread failed, ret is %d.\n", ret); + goto error; + } + + avctx->pkt_timebase.num = 1; + avctx->pkt_timebase.den = 90000; + if (!avctx->pkt_timebase.num || !avctx->pkt_timebase.den) { + av_log(avctx, AV_LOG_ERROR, "Invalid pkt_timebase.\n"); + } + + sem_init(&ctx->eos_sema, 0, 0); + return 0; + +error: + sem_post(&ctx->eos_sema); + ff_himpi_decode_end(avctx); + return ret; +} + +static void ff_himpi_flush(AVCodecContext *avctx) { + ff_himpi_decode_end(avctx); + ff_himpi_decode_init(avctx); +} + +#define OFFSET(x) offsetof(ASCENDContext_t, x) +#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM +static const AVOption options[] = { + { "device_id", "Use to choose the ascend chip.", OFFSET(device_id), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 8, VD}, + { "channel_id", "Set channelId of decoder.", OFFSET(channel_id), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 255, VD}, + { "resize", "Resize (width)x(height).", OFFSET(resize_str), AV_OPT_TYPE_STRING, { .str = NULL}, 0, 0, VD}, + { NULL } +}; + +static const AVCodecHWConfigInternal* ascend_hw_configs[] = { + &(const AVCodecHWConfigInternal) { + .public = { + .pix_fmt = AV_PIX_FMT_ASCEND, + .methods = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX | AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX | \ + AV_CODEC_HW_CONFIG_METHOD_INTERNAL, + .device_type = AV_HWDEVICE_TYPE_ASCEND + }, + .hwaccel = NULL, + }, + NULL +}; + +#define ASCEND_DEC_CODEC(x, X) \ + static const AVClass x##_ascend_class = { \ + .class_name = #x "_ascend_dec", \ + .item_name = av_default_item_name, \ + .option = options, \ + .version = LIBAVUTIL_VERSION_INT, \ + }; \ + AVCodec ff_##x##_ascend_decoder = { \ + .name = #x "_ascend", \ + .long_name = NULL_IF_CONFIG_SMALL("Ascend HiMpi " #X " decoder"), \ + .type = AVMEDIA_TYPE_VIDEO, \ + .id = AV_CODEC_ID_MJPEG, \ + .priv_data_size = sizeof(ASCENDContext_t), \ + .priv_class = &x##_ascend_class, \ + .init = ff_himpi_decode_init, \ + .close = ff_himpi_decode_end, \ + .receive_frame = ff_himpi_receive_frame, \ + .flush = ff_himpi_flush, \ + .capabilities = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP | + FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_SETS_PKT_DTS, \ + .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_ASCEND, \ + AV_PIX_FMT_NV12, \ + AV_PIX_FMT_NONE }, \ + .hw_configs = ascend_hw_configs, \ + .wrapper_name = "ascendmjpegdec", \ + }; + +#if CONFIG_MJPEG_ASCEND_DECODER +ASCEND_DEC_CODEC(mjpeg, MJPEG) +#endif \ No newline at end of file diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.h b/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.h new file mode 100644 index 000000000..cc8578152 --- /dev/null +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.h @@ -0,0 +1,148 @@ +/* + * Copyright(c) 2020. Huawei Technologies Co.,Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except int compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FFMPEG_ASCEND_ASCEND_DEC_H +#define FFMPEG_ASCEND_ASCEND_DEC_H + +#include "libavutil/parseutils.h" +#include "libavutil/buffer.h" +#include "libavutil/mathematics.h" +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_ascend.h" +#include "libavutil/fifo.h" +#include "libavutil/log.h" +#include "libavutil/opt.h" +#include "libavutil/time.h" +#include "libavutil/common.h" +#include "libavutil/pixdesc.h" +#include "libavutil/thread.h" +#include "libavutil/version.h" +#include "config.h" +#include "avcodec.h" +#include "decode.h" +#include "hwaccels.h" +#include "hwconfig.h" +#include "internal.h" +#include "libavutil/avutil.h" + +#include "acl/dvpp/hi_dvpp.h" + +#define VDEC_WIDTH_ALIGN 16 +#define VDEC_HEIGHT_ALIGN 2 + +#define REF_FRAME_NUM 8 +#define DISPLAY_FRAME_NUM 2 +#define VDEC_TIME_OUT 1000 +#define VDEC_GET_TIME_OUT 100 +#define VDEC_SLEEP_TIME 1000 + +#define YUV_BGR_CONVERT_3 3 +#define YUV_BGR_CONVERT_2 2 + +typedef enum { + EVENT_NEW_FRAME = 0, + EVENT_EOS = 1, +} eventType_t; + +typedef struct FrameInfo { + void *ascend_ctx; + eventType_t event_type; + uint8_t *data; + uint32_t data_size; + hi_pixel_format format; + int64_t pts; + int64_t dts; + uint32_t width_stride; + uint32_t height_stride; +} FrameInfo_t; + +typedef struct ASCENDContext { + AVClass* av_class; + int device_id; + int channel_id; + + pthread_t thread_id; + volatile int thread_run_flag; + volatile int hi_mpi_init_flag; + + /* + struct { + int x; + int y; + int w; + int h; + } crop; + struct { + int width; + int height; + } resize; + */ + + char *output_pixfmt; + sem_t eos_sema; + + AVBufferRef *hw_device_ref; + AVBufferRef *hw_frame_ref; + AVBSFContext *bsf; + AVCodecContext *avctx; + AVFifoBuffer *frame_queue; + AVFifoBuffer *dts_queue; + AVASCENDDeviceContext *hw_device_ctx; + AVHWFramesContext *hw_frames_ctx; + AscendContext *ascend_ctx; + + hi_vdec_chn_attr chn_attr_; + hi_pic_buf_attr buf_attr_; + hi_vdec_chn_param chn_param_; + + AVMutex queue_mutex; + + int max_width; + int max_height; + int vdec_width; + int vdec_height; + int stride_align; + char* resize_str; + int resize_width; + int resize_height; + hi_payload_type codec_type; + + volatile int frame_id; + int first_packet; + volatile int first_seq; + volatile int eos_received; + volatile int decoder_flushing; + volatile int decode_run_flag; + unsigned long long total_packet_count; + unsigned long long total_out_frame_count; + + hi_vdec_stream stream; + hi_vdec_pic_info pic_info; +} ASCENDContext_t; + +static inline void get_vdec_frame_info(FrameInfo_t* frame_info, hi_video_frame_info frame) +{ + uint32_t width_stride = frame.v_frame.width_stride[0]; + uint32_t height_stride = frame.v_frame.height_stride[0]; + frame_info->width_stride = width_stride; + frame_info->height_stride = height_stride; + frame_info->data_size = width_stride * height_stride * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; + frame_info->format = frame.v_frame.pixel_format; + frame_info->pts = frame.v_frame.pts; + frame_info->data = frame.v_frame.virt_addr[0]; +} + +#endif // FFMPEG_ASCEND_ASCEND_DEC_H \ No newline at end of file -- Gitee From 6432974af82afbcde78da0e220515dd61ad563a2 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 10 Sep 2024 09:43:19 +0800 Subject: [PATCH 57/60] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Ascendffmpeg/libavcodec/ascend_jpeg_dec.c | 14 ++--- .../libavcodec/ascend_mjpeg_dec.c | 55 +++++++++++-------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c index 4ab6f716b..a9ca3aee1 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_jpeg_dec.c @@ -406,7 +406,7 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) { - av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 0 \n"); + ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; int ret = 0; if (!ctx->frame_queue) { @@ -415,7 +415,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) FrameInfo_t frame_info; ff_mutex_lock(&ctx->queue_mutex); - av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 1 \n"); + av_log(avctx, AV_LOG_ERROR, "av_fifo_size(ctx->frame_queue) is %d \n", av_fifo_size(ctx->frame_queue)); if (av_fifo_size(ctx->frame_queue) != 0) { av_fifo_generic_read(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); @@ -423,26 +423,26 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) ff_mutex_unlock(&ctx->queue_mutex); return AVERROR(EAGAIN); } - av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 2 \n"); + ff_mutex_unlock(&ctx->queue_mutex); if (frame_info.event_type == EVENT_EOS) { return AVERROR_EOF; } - av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 3 \n"); + if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { ret = av_hwframe_get_buffer(ctx->hw_frame_ref, frame, 0); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed, ret is %d.\n", ret); return AVERROR(EINVAL); } - av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 4 \n"); + ret = ff_decode_frame_props(avctx, frame); if (ret < 0) { av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed, ret is %d.\n", ret); return AVERROR(EINVAL); } - av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 5 \n"); + } else { ret = ff_get_buffer(avctx, frame, 0); if (ret < 0) { @@ -450,7 +450,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) return AVERROR(EINVAL); } } - av_log(avctx, AV_LOG_ERROR, "here is himpi_get_frame. 6 \n"); + frame->pkt_pos = -1; frame->pkt_duration = 0; frame->pkt_size = -1; diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c index 44f99dd79..7507c1a08 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c @@ -309,27 +309,27 @@ static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) ASCENDContext_t *ctx = (ASCENDContext_t*) avctx->priv_data; int ret = 0; AVPacket packet = { 0 }; - AVPacket bsf_packet = { 0 }; +// AVPacket bsf_packet = { 0 }; - if (avpkt && avpkt->size && ctx->bsf) { - ret = av_packet_ref(&packet, avpkt); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed, ret(%d).\n", ret); - return ret; - } - ret = av_bsf_send_packet(ctx->bsf, &packet); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed, ret(%d).\n", ret); - av_packet_unref(&packet); - return ret; - } - ret = av_bsf_receive_packet(ctx->bsf, &bsf_packet); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed, ret(%d).\n", ret); - return ret; - } - avpkt = &bsf_packet; - } +// if (avpkt && avpkt->size && ctx->bsf) { +// ret = av_packet_ref(&packet, avpkt); +// if (ret < 0) { +// av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed, ret(%d).\n", ret); +// return ret; +// } +// ret = av_bsf_send_packet(ctx->bsf, &packet); +// if (ret < 0) { +// av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed, ret(%d).\n", ret); +// av_packet_unref(&packet); +// return ret; +// } +// ret = av_bsf_receive_packet(ctx->bsf, &bsf_packet); +// if (ret < 0) { +// av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed, ret(%d).\n", ret); +// return ret; +// } +// avpkt = &bsf_packet; +// } av_packet_unref(&packet); if (avpkt && avpkt->size) { @@ -380,12 +380,13 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) ff_mutex_unlock(&ctx->queue_mutex); return AVERROR(EAGAIN); } + av_log(avctx, AV_LOG_ERROR, "himpi_get_frame 0\n"); ff_mutex_unlock(&ctx->queue_mutex); if (frame_info.event_type == EVENT_EOS) { return AVERROR_EOF; } - + av_log(avctx, AV_LOG_ERROR, "avctx->pix_fmt is %d\n", avctx->pix_fmt); if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { ret = av_hwframe_get_buffer(ctx->hw_frame_ref, frame, 0); if (ret < 0) { @@ -404,7 +405,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) return AVERROR(EINVAL); } } - + av_log(avctx, AV_LOG_ERROR, "himpi_get_frame 1\n"); frame->pkt_pos = -1; frame->pkt_duration = 0; frame->pkt_size = -1; @@ -416,6 +417,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) switch (frame_info.format) { case HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420: + av_log(avctx, AV_LOG_ERROR, "himpi_get_frame 2\n"); if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { uint32_t offset = 0; for (int i = 0; i < 2; i++) { @@ -430,6 +432,7 @@ static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) offset += dstBytes; } } else { + av_log(avctx, AV_LOG_ERROR, "himpi_get_frame 3\n"); uint32_t offset = 0; for (int i = 0; i < 2; i++) { size_t dstBytes = frame->width * frame->height * (i ? 1.0 / 2 : 1); @@ -489,10 +492,13 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) while (ctx->decode_run_flag) { if (!ctx->decoder_flushing) { send_ret = ff_decode_get_packet(avctx, &pkt); + av_log(ctx, AV_LOG_ERROR, "ff_decode_get_packet ret is %d.\n", send_ret); + av_log(ctx, AV_LOG_ERROR, "pkt->size is %d.\n", pkt.size); if (send_ret < 0 && send_ret != AVERROR_EOF) { return send_ret; } send_ret = hi_mpi_decode(avctx, &pkt); + av_log(ctx, AV_LOG_ERROR, "hi_mpi_decode ret is %d.\n", send_ret); av_packet_unref(&pkt); if (send_ret < 0 && send_ret != AVERROR_EOF) { av_log(ctx, AV_LOG_ERROR, "Send packet failed, ret is %d.\n", send_ret); @@ -501,6 +507,7 @@ static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) } get_ret = himpi_get_frame(avctx, frame); + av_log(ctx, AV_LOG_ERROR, "himpi_get_frame ret is %d.\n", get_ret); if (get_ret != 0 && get_ret != AVERROR_EOF) { if (get_ret != AVERROR(EAGAIN)) { return get_ret; @@ -815,7 +822,7 @@ static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) if (!avctx->pkt_timebase.num || !avctx->pkt_timebase.den) { av_log(avctx, AV_LOG_ERROR, "Invalid pkt_timebase.\n"); } - + av_log(avctx, AV_LOG_ERROR, "Invalid pkt_timebase.\n"); sem_init(&ctx->eos_sema, 0, 0); return 0; @@ -870,7 +877,7 @@ static const AVCodecHWConfigInternal* ascend_hw_configs[] = { .close = ff_himpi_decode_end, \ .receive_frame = ff_himpi_receive_frame, \ .flush = ff_himpi_flush, \ - .capabilities = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP | + .capabilities = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP | \ FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_SETS_PKT_DTS, \ .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_ASCEND, \ AV_PIX_FMT_NV12, \ -- Gitee From 20f39df2cbb06e6d27425f12612f470914e4ed92 Mon Sep 17 00:00:00 2001 From: Malanchi Date: Thu, 26 Sep 2024 11:02:56 +0800 Subject: [PATCH 58/60] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mxVision/Ascendffmpeg/libavcodec/Makefile | 2 + .../libavcodec/ascend_dec_mjpeg.c | 86 +++++++++++++++++++ .../libavcodec/ascend_dec_mjpeg.h | 85 ++++++++++++++++++ mxVision/Ascendffmpeg/libavcodec/hwaccels.h | 1 + mxVision/Ascendffmpeg/libavcodec/mjpegdec.c | 18 ++++ 5 files changed, 192 insertions(+) create mode 100644 mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.c create mode 100644 mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.h diff --git a/mxVision/Ascendffmpeg/libavcodec/Makefile b/mxVision/Ascendffmpeg/libavcodec/Makefile index 91c3bfdf9..36933e5b4 100644 --- a/mxVision/Ascendffmpeg/libavcodec/Makefile +++ b/mxVision/Ascendffmpeg/libavcodec/Makefile @@ -29,6 +29,7 @@ HEADERS = ac3_parser.h \ ascend_enc.h \ ascend_jpeg_dec.h \ ascend_mjpeg_dec.h \ + ascend_dec_mjpeg.h \ OBJS = ac3_parser.o \ adts_parser.o \ @@ -378,6 +379,7 @@ OBJS-$(CONFIG_H264_ASCEND_DECODER) += ascend_dec.o OBJS-$(CONFIG_H264_ASCEND_ENCODER) += ascend_enc.o OBJS-$(CONFIG_JPEG_ASCEND_DECODER) += ascend_jpeg_dec.o OBJS-$(CONFIG_MJPEG_ASCEND_DECODER) += ascend_mjpeg_dec.o +OBJS-$(CONFIG_MJPEG_ASCEND_HWACCEL) += ascend_dec_mjpeg.o OBJS-$(CONFIG_H264_AMF_ENCODER) += amfenc_h264.o OBJS-$(CONFIG_H264_CUVID_DECODER) += cuviddec.o OBJS-$(CONFIG_H264_MEDIACODEC_DECODER) += mediacodecdec.o diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.c b/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.c new file mode 100644 index 000000000..c16073026 --- /dev/null +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.c @@ -0,0 +1,86 @@ +/* + * MJPEG HW decode acceleration through NVDEC + * + * Copyright (c) 2017 Philip Langdale + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "avcodec.h" +#include "internal.h" +#include "mjpegdec.h" +#include "nvdec.h" +#include "decode.h" + +static int nvdec_mjpeg_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) +{ + MJpegDecodeContext *s = avctx->priv_data; + + NVDECContext *ctx = avctx->internal->hwaccel_priv_data; + CUVIDPICPARAMS *pp = &ctx->pic_params; + FrameDecodeData *fdd; + NVDECFrame *cf; + AVFrame *cur_frame = s->picture; + + int ret; + + ret = ff_nvdec_start_frame(avctx, cur_frame); + if (ret < 0) + return ret; + + fdd = (FrameDecodeData*)cur_frame->private_ref->data; + cf = (NVDECFrame*)fdd->hwaccel_priv; + + *pp = (CUVIDPICPARAMS) { + .PicWidthInMbs = (cur_frame->width + 15) / 16, + .FrameHeightInMbs = (cur_frame->height + 15) / 16, + .CurrPicIdx = cf->idx, + + .intra_pic_flag = 1, + .ref_pic_flag = 0, + }; + + return ff_nvdec_simple_decode_slice(avctx, buffer, size); +} + +static int nvdec_mjpeg_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) +{ + return 0; +} + +static int nvdec_mjpeg_frame_params(AVCodecContext *avctx, + AVBufferRef *hw_frames_ctx) +{ + // Only need storage for the current frame + return ff_nvdec_frame_params(avctx, hw_frames_ctx, 1, 0); +} + +#if CONFIG_MJPEG_NVDEC_HWACCEL +AVHWAccel ff_mjpeg_nvdec_hwaccel = { + .name = "mjpeg_ascend_dec", + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_MJPEG, + .pix_fmt = AV_PIX_FMT_ASCEND, + .start_frame = nvdec_mjpeg_start_frame, + .end_frame = ff_nvdec_simple_end_frame, + .decode_slice = nvdec_mjpeg_decode_slice, + .frame_params = nvdec_mjpeg_frame_params, + .init = ff_nvdec_decode_init, + .uninit = ff_nvdec_decode_uninit, + .priv_data_size = sizeof(NVDECContext), +}; +#endif diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.h b/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.h new file mode 100644 index 000000000..66f3ca59e --- /dev/null +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.h @@ -0,0 +1,85 @@ +/* + * HW decode acceleration through NVDEC + * + * Copyright (c) 2016 Anton Khirnov + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_NVDEC_H +#define AVCODEC_NVDEC_H + +#include "compat/cuda/dynlink_loader.h" + +#include + +#include "libavutil/buffer.h" +#include "libavutil/frame.h" + +#include "avcodec.h" + +#if defined(NVDECAPI_MAJOR_VERSION) && defined(NVDECAPI_MINOR_VERSION) +# define NVDECAPI_CHECK_VERSION(major, minor) \ + ((major) < NVDECAPI_MAJOR_VERSION || ((major) == NVDECAPI_MAJOR_VERSION && (minor) <= NVDECAPI_MINOR_VERSION)) +#else +/* version macros were added in SDK 8.1 ffnvcodec */ +# define NVDECAPI_CHECK_VERSION(major, minor) \ + ((major) < 8 || ((major) == 8 && (minor) <= 0)) +#endif + +typedef struct NVDECFrame { + unsigned int idx; + unsigned int ref_idx; + AVBufferRef *idx_ref; + AVBufferRef *ref_idx_ref; + AVBufferRef *decoder_ref; +} NVDECFrame; + +typedef struct NVDECContext { + CUVIDPICPARAMS pic_params; + + AVBufferPool *decoder_pool; + + AVBufferRef *decoder_ref; + + uint8_t *bitstream; + int bitstream_len; + unsigned int bitstream_allocated; + uint8_t *bitstream_internal; + + unsigned *slice_offsets; + int nb_slices; + unsigned int slice_offsets_allocated; + + int supports_444; +} NVDECContext; + +int ff_nvdec_decode_init(AVCodecContext *avctx); +int ff_nvdec_decode_uninit(AVCodecContext *avctx); +int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame); +int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref); +int ff_nvdec_end_frame(AVCodecContext *avctx); +int ff_nvdec_simple_end_frame(AVCodecContext *avctx); +int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, + uint32_t size); +int ff_nvdec_frame_params(AVCodecContext *avctx, + AVBufferRef *hw_frames_ctx, + int dpb_size, + int supports_444); +int ff_nvdec_get_ref_idx(AVFrame *frame); + +#endif /* AVCODEC_NVDEC_H */ diff --git a/mxVision/Ascendffmpeg/libavcodec/hwaccels.h b/mxVision/Ascendffmpeg/libavcodec/hwaccels.h index 879caa659..62689a341 100644 --- a/mxVision/Ascendffmpeg/libavcodec/hwaccels.h +++ b/mxVision/Ascendffmpeg/libavcodec/hwaccels.h @@ -45,6 +45,7 @@ extern const AVHWAccel ff_hevc_vdpau_hwaccel; extern const AVHWAccel ff_hevc_videotoolbox_hwaccel; extern const AVHWAccel ff_mjpeg_nvdec_hwaccel; extern const AVHWAccel ff_mjpeg_vaapi_hwaccel; +extern const AVHWAccel ff_mjpeg_ascend_hwaccel; extern const AVHWAccel ff_mpeg1_nvdec_hwaccel; extern const AVHWAccel ff_mpeg1_vdpau_hwaccel; extern const AVHWAccel ff_mpeg1_videotoolbox_hwaccel; diff --git a/mxVision/Ascendffmpeg/libavcodec/mjpegdec.c b/mxVision/Ascendffmpeg/libavcodec/mjpegdec.c index afb117cfc..c908d63ba 100644 --- a/mxVision/Ascendffmpeg/libavcodec/mjpegdec.c +++ b/mxVision/Ascendffmpeg/libavcodec/mjpegdec.c @@ -2947,6 +2947,20 @@ static const AVClass mjpegdec_class = { .version = LIBAVUTIL_VERSION_INT, }; + +//static const AVCodecHWConfigInternal* ascend_hw_configs[] = { +// &(const AVCodecHWConfigInternal) { +// .public = { +// .pix_fmt = AV_PIX_FMT_ASCEND, +// .methods = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX | AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX | \ +// AV_CODEC_HW_CONFIG_METHOD_INTERNAL, +// .device_type = AV_HWDEVICE_TYPE_ASCEND +// }, +// .hwaccel = NULL, +// }, +// NULL +//}; + AVCodec ff_mjpeg_decoder = { .name = "mjpeg", .long_name = NULL_IF_CONFIG_SMALL("MJPEG (Motion JPEG)"), @@ -2970,6 +2984,10 @@ AVCodec ff_mjpeg_decoder = { #if CONFIG_MJPEG_VAAPI_HWACCEL HWACCEL_VAAPI(mjpeg), #endif + +#if CONFIG_MJPEG_ASCEND_HWACCEL + HWACCEL_ASCEND(mjpeg), +#endif NULL }, }; -- Gitee From 9367dc1f45b0f92b2d98f30a9b12143eba323dbb Mon Sep 17 00:00:00 2001 From: Malanchi Date: Mon, 9 Dec 2024 20:08:29 +0800 Subject: [PATCH 59/60] =?UTF-8?q?=E6=96=B0=E5=A2=9EmjpegAscend=E6=8F=92?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mxVision/Ascendffmpeg/libavcodec/allcodecs.c | 1 + .../libavcodec/ascend_dec_mjpeg.c | 530 ++++++++++++++++-- .../libavcodec/ascend_dec_mjpeg.h | 97 ++-- mxVision/Ascendffmpeg/libavcodec/mjpegdec.c | 4 - 4 files changed, 516 insertions(+), 116 deletions(-) diff --git a/mxVision/Ascendffmpeg/libavcodec/allcodecs.c b/mxVision/Ascendffmpeg/libavcodec/allcodecs.c index f650814a9..2a263c29c 100644 --- a/mxVision/Ascendffmpeg/libavcodec/allcodecs.c +++ b/mxVision/Ascendffmpeg/libavcodec/allcodecs.c @@ -792,6 +792,7 @@ extern AVCodec ff_h265_ascend_decoder; extern AVCodec ff_h264_ascend_encoder; extern AVCodec ff_h265_ascend_encoder; extern AVCodec ff_jpeg_ascend_decoder; +extern AVCodec ff_ascend_mjpeg_decoder; extern AVCodec ff_mjpeg_ascend_decoder; extern AVCodec ff_h264_amf_encoder; extern AVCodec ff_h264_cuvid_decoder; diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.c b/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.c index c16073026..99827ebaf 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.c +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.c @@ -1,86 +1,506 @@ /* - * MJPEG HW decode acceleration through NVDEC + * Copyright(c) 2024. Huawei Technologies Co.,Ltd. All rights reserved. * - * Copyright (c) 2017 Philip Langdale + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except int compliance with the License. + * You may obtain a copy of the License at * - * This file is part of FFmpeg. + * http://www.apache.org/licenses/LICENSE-2.0 * - * FFmpeg is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * FFmpeg is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with FFmpeg; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ +#include "libavutil/imgutils.h" +#include "libavutil/avassert.h" +#include "libavutil/opt.h" #include "avcodec.h" +#include "blockdsp.h" +#include "copy_block.h" +#include "decode.h" +#include "hwconfig.h" +#include "idctdsp.h" #include "internal.h" +#include "jpegtables.h" +#include "mjpeg.h" #include "mjpegdec.h" -#include "nvdec.h" -#include "decode.h" +#include "jpeglsdec.h" +#include "profiles.h" +#include "put_bits.h" +#include "tiff.h" +#include "exif.h" +#include "bytestream.h" + +static AVBufferRef *hw_device_ref = NULL; + +static const uint8_t jpeg_header2[] = { + 0xff, 0xd8, // SOI + 0xff, 0xe0, // APP0 + 0x00, 0x10, // APP0 header size + 0x4a, 0x46, 0x49, 0x46, 0x00, // ID string 'JFIF\0' + 0x01, 0x01, // version + 0x00, // bits per type + 0x00, 0x00, // X density + 0x00, 0x00, // Y density + 0x00, // X thumbnail size + 0x00, // Y thumbnail size +}; + +static const int dht_segment_size2 = 420; + +static const uint8_t dht_segment_head2[] = {0xFF, 0xC4, 0x01, 0xA2, 0x00}; +static const uint8_t dht_segment_frag2[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x0a, 0x0b, 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, +}; -static int nvdec_mjpeg_start_frame(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) +static uint8_t *append2(uint8_t *buf, const uint8_t *src, int size) { - MJpegDecodeContext *s = avctx->priv_data; + memcpy(buf, src, size); + return buf + size; +} - NVDECContext *ctx = avctx->internal->hwaccel_priv_data; - CUVIDPICPARAMS *pp = &ctx->pic_params; - FrameDecodeData *fdd; - NVDECFrame *cf; - AVFrame *cur_frame = s->picture; +static uint8_t *append_dht_segment2(uint8_t *buf) +{ + buf = append2(buf, dht_segment_head2, sizeof(dht_segment_head2)); + buf = append2(buf, avpriv_mjpeg_bits_dc_luminance + 1, 16); + buf = append2(buf, dht_segment_frag2, sizeof(dht_segment_frag2)); + buf = append2(buf, avpriv_mjpeg_val_dc, 12); + *(buf++) = 0x10; + buf = append2(buf, avpriv_mjpeg_bits_ac_luminance + 1, 16); + buf = append2(buf, avpriv_mjpeg_val_ac_luminance, 162); + *(buf++) = 0x11; + buf = append2(buf, avpriv_mjpeg_bits_ac_chrominance + 1, 16); + buf = append2(buf, avpriv_mjpeg_val_ac_chrominance, 162); + return buf; +} +#define REF_FRAME_NUM 8 +#define DISPLAY_FRAME_NUM 2 +#define FFALIGNMJPEG(x, a) (((x) + (a) - 1) &~ ((a) - 1)) +#define WIDTH_ALIGN 2 +#define HEIGHT_ALIGN 16 + +av_cold int ff_mjpeg_ascend_decode_init(AvCodecContext* avctx) +{ + AscendMjpegDecodeContext *s = avctx->priv_data; int ret; - ret = ff_nvdec_start_frame(avctx, cur_frame); + enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_ASCEND, AV_PIX_FMT_NV12, AV_PIX_FMT_NONE }; + avctx->pix_fmt = ff_get_format(avctx, pix_fmts); + if (avctx->pix_fmt != AV_PIX_FMT_ASCEND) { + av_log(avctx, AV_LOG_ERROR, "Perhaps the command \"-hwaccel ascend\" is missing. Please check it.\n"); + return AVERROR(EINVAL); + } + + if (avctx->width < 128 || avctx->height < 128 || avctx->width > 4096 || avctx->height > 4096) { + av_log(avctx, AV_LOG_ERROR, "MJPEG decoder only support resolution: 128x128 ~ 4096x4096, now: %dx%d.\n", avctx->width, avctx->height); + return AVERROR(EINVAL); + } + + char device_id[sizeof(int)]; + sprintf(device_id, "%d", s->device_id); + + AVASCENDDeviceContext* hw_device_ctx; + AVHWFramesContext* hw_frame_ctx; + if (avctx->hw_frame_ctx) { + av_buffer_unref(&s->hw_frame_ref); + s->hw_frame_ref = av_buffer_ref(avctx->hw_frame_ctx); + if (!s->hw_frame_ref) { + ret = AVERROR(EINVAL); + goto #error; + } + + hw_frame_ctx = (AVHWFramesContext*)s->hw_frame_ref->data; + if (!hw_frame_ctx->pool || (avctx->width != hw_frame_ctx->width)) { + if (hw_frame_ctx->pool) { + av_buffer_pool_uninit(&hw_frame_ctx->pool); + } + hw_frame_ctx->width = avctx->width; + hw_frame_ctx->height = avctx->height; + hw_frame_ctx->initial_pool_size = 2; + hw_frame_ctx->format = AV_PIX_FMT_ASCEND; + hw_frame_ctx->sw_format = AV_PIX_FMT_NV12; + } + ret = av_hwframe_ctx_init(s->hw_frame_ref); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "HWFrame context init failed, ret is %d.\n", ret); + return AVERROR(ENAVAIL); + } + + s->hw_device_ref = av_buffer_ref(hw_frame_ctx->device_ref); + if (!s->hw_device_ref) { + av_log(avctx, AV_LOG_ERROR, "Get hw_device_ref failed.\n"); + ret = AVERROR(EINVAL); + goto error; + } + } else { + if (avctx->hw_device_ctx) { + s->hw_device_ref = av_buffer_ref(avctx->hw_device_ctx); + if (!s->hw_device_ref) { + av_log(avctx, AV_LOG_ERROR, "ref hwdevice failed.\n"); + ret = AVERROR(EINVAL); + goto error; + } + } else { + ret = av_hwdevice_ctx_create(&s->hw_device_ref, AV_HWDEVICE_TYPE_ASCEND, device_id, NULL, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "hwdevice context create failed.\n"); + goto error; + } + } + s->hw_frame_ref = av_hwframe_ctx_alloc(s->hw_device_ref); + if (!s->hw_frame_ref) { + av_log(avctx, AV_LOG_ERROR, "hwframe ctx alloc falied.\n"); + ret = AVERROR(EINVAL); + goto error; + } + hw_frame_ctx = (AVHWFramesContext*)s->hw_frame_ref->data; + if (!hw_frame_ctx->pool) { + hw_frame_ctx->width = avctx_width; + hw_frame_ctx->height = avctx->height; + hw_frame_ctx->initial_pool_size = 2; + hw_frame_ctx->format = AV_PIX_FMT_ASCEND; + hw_frame_ctx->sw_format = AV_PIX_FMT_NV12; + ret = av_hwframe_ctx_init(s->hw_frame_ref); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "hwframe ctx init error, ret is %d.\n", ret); + ret = AVERROR(EINVAL); + goto error; + } + } + } + + hw_device_ctx = ((AVHWDeviceContext*)s->hw_device_ref->data)->hwctx; + s->hw_device_ctx = hw_device_ctx; + s->hw_frame_ctx = hw_frame_ctx; + s->ascend_ctx = s->hw_device_ctx->ascend_ctx; + + ret = aclrtSetCurrentContext(s->ascend_ctx->context); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Set context failed at line(%d) in func(%s), ret is %d.\n", __LINE__, __func__, ret); + return ret; + } + + ret = hi_mpi_sys_init(); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi sys init failed, ret is %d.\n", ret); + return ret; + } + + hi_vdec_chn_attr chn_attr_; + chn_attr_.type = HI_PT_JPEG; + chn_attr_.mode = HI_VDEC_SEND_MODE_FRAME; + chn_attr_.pic_width = 1920; + chn_attr_.pic_height = 1080; + chn_attr_.stream_buf_size = chn_attr_.pic_width * chn_attr_.height * 3 / 2; + chn_attr_.frame_buf_cnt = REF_FRAME_NUM + DISPLAY_FRAME_NUM + 1; + + hi_pic_buf_attr buf_attr_; + buf_attr_.width = chn_attr_.pic_width; + buf_attr_.height = chn_attr_.pic_height; + buf_attr_.align = 0; + buf_attr_.bit_width = HI_DATA_BIT_WIDTH_8; + buf_attr_.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + buf_attr_.compress_mode = HI_COMPRESS_MODE_NONE; + + chn_attr_.frame_buf_size = hi_vdec_get_pic_buf_size(chn_attr_.type, &buf_attr_); + chn_attr_.video_attr.ref_frame_num = REF_FRAME_NUM; + chn_attr_.video_attr.temporal_mvp_en = HI_TRUE; + chn_attr_.video_attr.tmv_buf_size = hi_vdec_get_tmv_buf_size(chn_attr_.type, + chn_attr_.pic_width, + chn_attr_.pic_height); + s->channel_id = 0; + uint32_t channel_id = s->channel_id; + ret = hi_mpi_vdec_create_chn(channel_id, &chn_attr_); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi create vdec channel failed, ret is %d.\n", ret); + return ret; + } + + hi_vdec_chn_param chn_param_; + ret = hi_mpi_vdec_get_chn_param(channel_id, &chn_param_); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec get channel param failed, ret is %d.\n", ret); + return ret; + } + + chn_param_.video_param.dec_mode = HI_VIDEO_DEC_MODE_IPB; + chn_param_.video_param.compress_mode = HI_COMPRESS_MODE_HFBC; + chn_param_.video_param.video_format = HI_VIDEO_FORMAT_TILE_64x16; + chn_param_.display_frame_num = DISPLAY_FRAME_NUM; + chn_param_.video_param.out_order = HI_VIDEO_OUT_ORDER_DISPLAY; + + ret = hi_mpi_vdec_set_chn_param(channel_id, &chn_param_); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec set channel param failed, ret is %d.\n", ret); + return ret; + } + + ret = hi_mpi_vdec_start_recv_stream(channel_id); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi vdec start receive stream failed, ret is %d.\n", ret); + return ret; + } + + s->pkt = av_packet_alloc(); + if (!s->pkt) + return AVERROR(ENOMEM); + s->avctx = avctx; + return 0; +} + +int ff_mjpeg_receive_frame(AvCodecContext* avctx, AVFrame* frame) +{ + AscendMjpegDecodeContext *s = avctx->priv_data; + int ret = 0; + ret = mjpeg_get_packet(avctx); + if (ret < 0) { + return ret; + } + + /* GET JPEG */ + AVPacket *out = av_packet_alloc(); + uint8_t* output; + int input_skip, output_size; + AVPacket *in = s->pkt; + + if (in->size < 12) { + av_log(avctx, AV_LOG_ERROR, "Input is truncate.\n"); + return AVERROR_INVALIDDATA; + } + + if (AV_RB16(int->data) != 0xffd8) { + av_log(avctx, AV_LOG_ERROR, "Input is not MJPEG.\n"); + return AVERROR_INVALIDDATA; + } + + if (in->data[2] == 0xff && in->data[3] == APP) { + input_skip = (in->data[4] << 8) + in->data[5] + 4; + } else { + input_skip = 2; + } + + if (in->size < input_skip) { + av_log(avctx, AV_LOG_ERROR, "Input is truncate.\n"); + return AVERROR_INVALIDDATA; + } + + output_size = in->size - input_skip + sizeof(jpeg_header2) + dht_segment_size2; + ret = av_new_packet(out, output_size); if (ret < 0) + return AVERROR_INVALIDDATA; + output = out->data; + output = append2(output, jpeg_header2, sizeof(jpeg_header2)); + output = append_dht_segment2(output); + output = append2(output, in->data + input_skip, in->size - input_skip); + + /* JPEG to YUV420 By Ascend */ + ret = aclrtSetCurrentContext(s->ascend_ctx->context); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); + return ret; + } + + uint8_t* streamBuffer = NULL; + int device_id = 0; + ret = hi_mpi_dvpp_malloc(device_id, &streamBuffer, output_size); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi malloc packet failed, ret is %d.\n", ret); + return ret; + } + + ret = aclrtMemcpy(streamBuffer, output_size, output_size, out->data, output_size, ACL_MEMCPY_HOST_TO_DEVICE); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy H2D failed. ret is %d.\n", ret); + return ret; + } + + hi_vdec_stream stream; + stream.pts = 0; + stream.addr = streamBuffer; + stream.len = output_size; + stream.end_of_frame = HI_TRUE; + stream.end_of_stream = HI_FALSE; + stream.need_display = HI_TRUE; + + hi_vdec_pic_info pic_info; + pic_info.width = s->avctx->width; + pic_info.height = s->avctx->height; + pic_info.width_stride = FFALIGNMJPEG(pic_info.width, WIDTH_ALIGN); + pic_info.height_stride = FFALIGNMJPEG(pic_info.height, HEIGHT_ALIGN); + pic_info.offset_top = 0; + pic_info.offset_bottom = 0; + pic_info.offset_left = 0; + pic_info.offset_right = 0; + + uint32_t size = pic_info.width_stride * pic_info.height_stride * 3 / 2; + pic_info.buffer_size = size; + pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; + + void* picBuffer = NULL; + ret = hi_mpi_dvpp_malloc(device_id, &picBuffer, size); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi malloc falied, ret is %d.\n", ret); + return ret; + } + + pic_info.vir_addr = (uint64_t)picBuffer; + + ret = hi_mpi_vdec_send_stream(s->channel_id, &stream, &pic_info, 1000); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Send stream failed, ret is %d.\n", ret); return ret; + } - fdd = (FrameDecodeData*)cur_frame->private_ref->data; - cf = (NVDECFrame*)fdd->hwaccel_priv; + hi_video_frame_info got_frame; + hi_vdec_stream got_stream; + hi_vdec_supplement_info stSupplement; + ret = hi_mpi_vdec_get_frame(s->channel_id, &got_frame, &stSupplement, &got_stream, 100); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Get frame failed, ret is %d.\n", ret); + return ret; + } + size_t decResult = got_frame.v_frame.frame_flag; + hi_mpi_dvpp_free(got_stream.addr); + if (decResult != 0 && got_frame.v_frame.virt_addr[0] != NULL) { + hi_mpi_dvpp_free(got_frame.v_frame.virt_addr[0]); + return ret; + } + if (decResult != 0 || got_frame.v_frame.virt_addr[0] == NULL || got_stream.need_display = HI_FALSE) { + ret = hi_mpi_vdec_release_frame(s->channel_id, &got_frame); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); + return ret; + } + return -1; + } + ret = hi_mpi_vdec_release_frame(s->channel_id, &got_frame); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); + return ret; + } - *pp = (CUVIDPICPARAMS) { - .PicWidthInMbs = (cur_frame->width + 15) / 16, - .FrameHeightInMbs = (cur_frame->height + 15) / 16, - .CurrPicIdx = cf->idx, + avctx->pix_fmt = (int)AV_PIX_FMT_NV12; + ret = av_hwframe_get_buffer(s->hw_frame_ref, frame, 0); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "Frame get buffer failed, ret is %d.\n", ret); + return AVERROR(EINVAL); + } + ret = ff_deocde_frame_props(avctx, frame); + if (ret < 0) { + av_log(avctx, AV_LOG_ERROR, "") + } - .intra_pic_flag = 1, - .ref_pic_flag = 0, - }; + frame->pkt_pos = -1; + frame->pkt_duration = 0; + frame->pkt_size = -1; + frame->pts = got_frame.v_frame.pts; + frame->pkt_pts = frame->pts; + frame->pkt_dts = out->dts; - return ff_nvdec_simple_decode_slice(avctx, buffer, size); + uint32_t offset = 0; + for (int i = 0; i < 2; i++) { + size_t dstBytes = got_frame.v_frame.width_stride[0] * got_frame.v_frame.height_stride[0] * (i ? 1.0 / 2 : 1); + ret = aclrtMemcpy(frame->data[i], dstBytes, got_frame.v_frame.virt_addr[0] + offset, dstBytes, + ACL_MEMCPY_DEVICE_TO_DEVICE); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Mem copy D2D failed, ret is %d.\n", ret); + hi_mpi_dvpp_free(got_frame.v_frame.virt_addr[0]); + return ret; + } + offset += dstBytes; + } + hi_mpi_dvpp_free(got_frame.v_frame.virt_addr[0]); + av_packet_free(&out); + return ret; } -static int nvdec_mjpeg_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, uint32_t size) +av_cold int ff_mjpeg_decode_end(AvCodecContext* avctx) { + AscendMjpegDecodeContext *s = avctx->priv_data; + int i, j, ret; + ret = aclrtSetCurrentContext(s->ascend_ctx->context); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); + return ret; + } + + ret = hi_mpi_vdec_stop_recv_stream(s->channel_id); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi stop receive stream failed, ret is %d.\n", ret); + return ret; + } + + ret = hi_mpi_vdec_destroy_chn(s->channel_id); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi destroy channel failed, ret is %d.\n", ret); + return ret; + } + + ret = hi_mpi_sys_exit(); + if (ret != 0) { + av_log(avctx, AV_LOG_ERROR, "HiMpi sys exit failed, ret is %d.\n", ret); + return ret; + } + + av_buffer_unref(&s->hw_device_ref); return 0; } -static int nvdec_mjpeg_frame_params(AVCodecContext *avctx, - AVBufferRef *hw_frames_ctx) +static void ascend_decode_flush(AvCodecContext* avctx) { - // Only need storage for the current frame - return ff_nvdec_frame_params(avctx, hw_frames_ctx, 1, 0); + ff_mjpeg_decode_end(avctx); + ff_mjpeg_decode_init(avctx); } -#if CONFIG_MJPEG_NVDEC_HWACCEL -AVHWAccel ff_mjpeg_nvdec_hwaccel = { - .name = "mjpeg_ascend_dec", - .type = AVMEDIA_TYPE_VIDEO, - .id = AV_CODEC_ID_MJPEG, - .pix_fmt = AV_PIX_FMT_ASCEND, - .start_frame = nvdec_mjpeg_start_frame, - .end_frame = ff_nvdec_simple_end_frame, - .decode_slice = nvdec_mjpeg_decode_slice, - .frame_params = nvdec_mjpeg_frame_params, - .init = ff_nvdec_decode_init, - .uninit = ff_nvdec_decode_uninit, - .priv_data_size = sizeof(NVDECContext), +#if CONFIG_MJPEG_ASCEND_HWACCEL + +#define OFFSET(x) offsetof(AscendMjpegDecodeContext, x) +#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM +static_cast const AVOption options[] = { + { "device_id", "Use to choose the ascend chip.", OFFSET(device_id), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 8, VD }, + { "channel_id", "Set channelId of decoder.", OFFSET(channel_id), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 255, VD }, + { NULL } +}; + +static const AVClass mjpegdec_class = { + .class_name = "Ascend MJPEG decoder", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, }; -#endif + +static const AVCodecHWConfigInternal* ascend_hw_config2[] = { + &(const AVCodecHWConfigInternal) { + .public = { + .pix_fmt = AV_PIX_FMT_ASCEND, + .methods = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX | AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX | + AV_CODEC_HW_CONFIG_METHOD_INTERNAL, + .device_type = AV_HWDEVICE_TYPE_ASCEND + }, + .hwaccel = NULL, + }, + NULL +}; + +AVCodec ff_ascend_mjpeg_decoder = { + .name = "mjpeg_ascend_dec", + .long_name = NULL_IF_CONFIG_SMALL("Ascend MJPEG (MOTION JPEG)"), + .type = AVMEDIA_TYPE_VIDEO, + .id = AV_CODEC_ID_MJPEG, + .priv_data_size = sizeof(AscendMjpegDecodeContext), + .init = ff_mjpeg_ascend_decode_init, + .close = ff_mjpeg_ascend_decode_end, + .receive_frame = ff_mjpeg_ascend_receive_frame, + .flush = ascend_decode_flush, + .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, + .priv_class = &mjpegdec_class, + .hw_configs = ascend_hw_config2, +}; + +##endif \ No newline at end of file diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.h b/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.h index 66f3ca59e..f269c876d 100644 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.h +++ b/mxVision/Ascendffmpeg/libavcodec/ascend_dec_mjpeg.h @@ -1,7 +1,8 @@ /* - * HW decode acceleration through NVDEC - * - * Copyright (c) 2016 Anton Khirnov + * MJPEG decoder + * Copyright (c) 2000, 2001 Fabrice Bellard + * Copyright (c) 2003 Alex Beregszaszi + * Copyright (c) 2003-2004 Michael Niedermayer * * This file is part of FFmpeg. * @@ -20,66 +21,48 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef AVCODEC_NVDEC_H -#define AVCODEC_NVDEC_H - -#include "compat/cuda/dynlink_loader.h" +/** + * @file + * MJPEG decoder. + */ -#include +#ifndef ASCEND_AVCODEC_MJPEGDEC_H +#define ASCEND_AVCODEC_MJPEGDEC_H -#include "libavutil/buffer.h" -#include "libavutil/frame.h" +#include "libavutil/log.h" +#include "libavutil/mem_internal.h" +#include "libavutil/pixdesc.h" +#include "libavutil/stereo3d.h" #include "avcodec.h" +#include "blockdsp.h" +#include "get_bits.h" +#include "hpeldsp.h" +#include "idctdsp.h" -#if defined(NVDECAPI_MAJOR_VERSION) && defined(NVDECAPI_MINOR_VERSION) -# define NVDECAPI_CHECK_VERSION(major, minor) \ - ((major) < NVDECAPI_MAJOR_VERSION || ((major) == NVDECAPI_MAJOR_VERSION && (minor) <= NVDECAPI_MINOR_VERSION)) -#else -/* version macros were added in SDK 8.1 ffnvcodec */ -# define NVDECAPI_CHECK_VERSION(major, minor) \ - ((major) < 8 || ((major) == 8 && (minor) <= 0)) -#endif - -typedef struct NVDECFrame { - unsigned int idx; - unsigned int ref_idx; - AVBufferRef *idx_ref; - AVBufferRef *ref_idx_ref; - AVBufferRef *decoder_ref; -} NVDECFrame; +#undef near /* This file uses struct member 'near' which in windows.h is defined as empty. */ -typedef struct NVDECContext { - CUVIDPICPARAMS pic_params; +typedef struct AscendMJpegDecodeContext { + AVClass *class; + AVCodecContext *avctx; + int buf_size; - AVBufferPool *decoder_pool; + AVPacket *pkt; + enum AVPixelFormat hwaccel_sw_pix_fmt; + enum AVPixelFormat hwaccel_pix_fmt; + void* hwaccel_picture_private; + int device_id; + uint32_t channel_id; + uint32_t vdec_width; + uint32_t vdec_height; + AVBufferRef* hw_device_ref; + AVBufferRef* hw_frame_ref; + AVHWFramesContext* hw_frames_ctx; + AscendContext* ascend_ctx; +} AscendMJpegDecodeContext; - AVBufferRef *decoder_ref; +int ff_mjpeg_ascend_decode_init(AVCodecContext *avctx); +int ff_mjpeg_ascend_decode_end(AVCodecContext *avctx); +int ff_mjpeg_ascend_receive_frame(AVCodecContext *avctx, AVFrame *frame); - uint8_t *bitstream; - int bitstream_len; - unsigned int bitstream_allocated; - uint8_t *bitstream_internal; - - unsigned *slice_offsets; - int nb_slices; - unsigned int slice_offsets_allocated; - - int supports_444; -} NVDECContext; - -int ff_nvdec_decode_init(AVCodecContext *avctx); -int ff_nvdec_decode_uninit(AVCodecContext *avctx); -int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame); -int ff_nvdec_start_frame_sep_ref(AVCodecContext *avctx, AVFrame *frame, int has_sep_ref); -int ff_nvdec_end_frame(AVCodecContext *avctx); -int ff_nvdec_simple_end_frame(AVCodecContext *avctx); -int ff_nvdec_simple_decode_slice(AVCodecContext *avctx, const uint8_t *buffer, - uint32_t size); -int ff_nvdec_frame_params(AVCodecContext *avctx, - AVBufferRef *hw_frames_ctx, - int dpb_size, - int supports_444); -int ff_nvdec_get_ref_idx(AVFrame *frame); - -#endif /* AVCODEC_NVDEC_H */ +#endif diff --git a/mxVision/Ascendffmpeg/libavcodec/mjpegdec.c b/mxVision/Ascendffmpeg/libavcodec/mjpegdec.c index c908d63ba..3b57be36e 100644 --- a/mxVision/Ascendffmpeg/libavcodec/mjpegdec.c +++ b/mxVision/Ascendffmpeg/libavcodec/mjpegdec.c @@ -2984,10 +2984,6 @@ AVCodec ff_mjpeg_decoder = { #if CONFIG_MJPEG_VAAPI_HWACCEL HWACCEL_VAAPI(mjpeg), #endif - -#if CONFIG_MJPEG_ASCEND_HWACCEL - HWACCEL_ASCEND(mjpeg), -#endif NULL }, }; -- Gitee From 4368920c3868723a4152c2ee0800646bd18c0a5b Mon Sep 17 00:00:00 2001 From: Malanchi Date: Tue, 10 Dec 2024 09:09:58 +0800 Subject: [PATCH 60/60] =?UTF-8?q?=E6=96=B0=E5=A2=9EmjpegAscend=E6=8F=92?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mxVision/Ascendffmpeg/libavcodec/Makefile | 2 - mxVision/Ascendffmpeg/libavcodec/allcodecs.c | 1 - .../libavcodec/ascend_mjpeg_dec.c | 891 ------------------ .../libavcodec/ascend_mjpeg_dec.h | 148 --- 4 files changed, 1042 deletions(-) delete mode 100644 mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c delete mode 100644 mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.h diff --git a/mxVision/Ascendffmpeg/libavcodec/Makefile b/mxVision/Ascendffmpeg/libavcodec/Makefile index 36933e5b4..e903ae212 100644 --- a/mxVision/Ascendffmpeg/libavcodec/Makefile +++ b/mxVision/Ascendffmpeg/libavcodec/Makefile @@ -28,7 +28,6 @@ HEADERS = ac3_parser.h \ ascend_dec.h \ ascend_enc.h \ ascend_jpeg_dec.h \ - ascend_mjpeg_dec.h \ ascend_dec_mjpeg.h \ OBJS = ac3_parser.o \ @@ -378,7 +377,6 @@ OBJS-$(CONFIG_H264_DECODER) += h264dec.o h264_cabac.o h264_cavlc.o \ OBJS-$(CONFIG_H264_ASCEND_DECODER) += ascend_dec.o OBJS-$(CONFIG_H264_ASCEND_ENCODER) += ascend_enc.o OBJS-$(CONFIG_JPEG_ASCEND_DECODER) += ascend_jpeg_dec.o -OBJS-$(CONFIG_MJPEG_ASCEND_DECODER) += ascend_mjpeg_dec.o OBJS-$(CONFIG_MJPEG_ASCEND_HWACCEL) += ascend_dec_mjpeg.o OBJS-$(CONFIG_H264_AMF_ENCODER) += amfenc_h264.o OBJS-$(CONFIG_H264_CUVID_DECODER) += cuviddec.o diff --git a/mxVision/Ascendffmpeg/libavcodec/allcodecs.c b/mxVision/Ascendffmpeg/libavcodec/allcodecs.c index 2a263c29c..3bbbcf852 100644 --- a/mxVision/Ascendffmpeg/libavcodec/allcodecs.c +++ b/mxVision/Ascendffmpeg/libavcodec/allcodecs.c @@ -793,7 +793,6 @@ extern AVCodec ff_h264_ascend_encoder; extern AVCodec ff_h265_ascend_encoder; extern AVCodec ff_jpeg_ascend_decoder; extern AVCodec ff_ascend_mjpeg_decoder; -extern AVCodec ff_mjpeg_ascend_decoder; extern AVCodec ff_h264_amf_encoder; extern AVCodec ff_h264_cuvid_decoder; extern AVCodec ff_h264_mf_encoder; diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c b/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c deleted file mode 100644 index 7507c1a08..000000000 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.c +++ /dev/null @@ -1,891 +0,0 @@ -/* - * Copyright(c) 2020. Huawei Technologies Co.,Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except int compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include "ascend_dec.h" - -static void *get_frame(void *arg) -{ - ASCENDContext_t *ctx = (ASCENDContext_t*)arg; - int ret = 0; - int eos_flag = 0; - ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); - return ((void*) (-1)); - } - - hi_video_frame_info frame; - hi_vdec_stream stream; - hi_vdec_supplement_info stSupplement; - - av_log(NULL, AV_LOG_INFO, "Thread start.\n"); - - while (ctx->thread_run_flag) { - ret = hi_mpi_vdec_get_frame(ctx->channel_id, &frame, &stSupplement, &stream, VDEC_GET_TIME_OUT); - if (ret != 0) { - if (ctx->decoder_flushing && ret == HI_ERR_VDEC_BUF_EMPTY) { - eos_flag = 1; - av_log(ctx, AV_LOG_DEBUG, "Decoder flushing or stream eos.\n"); - } else { - av_log(ctx, AV_LOG_DEBUG, "HiMpi get frame failed, ret is %d.\n", ret); - continue; - } - } - - size_t decResult = frame.v_frame.frame_flag; - if (eos_flag) { - // eos - FrameInfo_t frame_info; - memset(&frame_info, 0, sizeof(FrameInfo_t)); - frame_info.event_type = EVENT_EOS; - - ff_mutex_lock(&ctx->queue_mutex); - av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); - ff_mutex_unlock(&ctx->queue_mutex); - sem_post(&ctx->eos_sema); - av_log(ctx, AV_LOG_DEBUG, "Decode got eos.\n"); - break; - } - - hi_mpi_dvpp_free(stream.addr); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi free stream failed, ret is %d.\n", ret); - } - if (decResult != 0 && frame.v_frame.virt_addr[0] != NULL) { - hi_mpi_dvpp_free(frame.v_frame.virt_addr[0]); - } - - if (decResult != 0 || frame.v_frame.virt_addr[0] == NULL || stream.need_display == HI_FALSE) { - ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.", ret); - return ((void*) (-1)); - } - continue; - } - FrameInfo_t frame_info; - frame_info.ascend_ctx = ctx; - get_vdec_frame_info(&frame_info, frame); - - ff_mutex_lock(&ctx->queue_mutex); - av_fifo_generic_read(ctx->dts_queue, &frame_info.dts, sizeof(int64_t), NULL); - av_fifo_generic_write(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); - ff_mutex_unlock(&ctx->queue_mutex); - - ret = hi_mpi_vdec_release_frame(ctx->channel_id, &frame); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi release frame failed, ret is %d.\n", ret); - return ((void*) (-1)); - } - - ctx->total_out_frame_count++; - } - return NULL; -} - -static inline int decode_params_checking(AVCodecContext* avctx) -{ - switch (avctx->codec->id) { - case AV_CODEC_ID_MJPEG: - if (avctx->width < 128 || avctx->height < 128 || - avctx->width > 4096 || avctx->height > 4096) { - av_log(avctx, AV_LOG_ERROR, - "H264 decoder only support resolution: 128x128 ~ 4096x4096, now: %dx%d.\n", - avctx->width, avctx->height); - return -1; - } - break; - - default: - break; - } - - return 0; -} - -static av_cold int ff_himpi_decode_end(AVCodecContext *avctx) -{ - ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; - int ret = 0; - int semvalue = 0; - struct timespec ts; - if (ctx == NULL || avctx->priv_data == NULL) { - av_log(ctx, AV_LOG_ERROR, "HiMpi decode end error, AVCodecContext is NULL.\n"); - return AVERROR_BUG; - } - - ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "Set Context failed, ret is %d.\n", ret); - return ret; - } - - clock_gettime(CLOCK_REALTIME, &ts); - ts.tv_sec += 3; - if (sem_timedwait(&(ctx->eos_sema), &ts) == -1) { - semvalue = -1; - sem_getvalue(&ctx->eos_sema, &semvalue); - av_log(ctx, AV_LOG_ERROR, "Decode sem_timewait = -1, semvalue = %d.\n", semvalue); - } - - if (ctx->hi_mpi_init_flag) { - ret = hi_mpi_vdec_stop_recv_stream(ctx->channel_id); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi stop receive stream failed, ret is %d.\n", ret); - return ret; - } - - ret = hi_mpi_vdec_destroy_chn(ctx->channel_id); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi destroy channel failed, ret is %d.\n", ret); - return ret; - } - - ret = hi_mpi_sys_exit(); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "HiMpi sys exit failed, ret is %d.\n", ret); - return ret; - } - } - - ctx->hi_mpi_init_flag = 0; - ctx->decode_run_flag = 0; - - if (ctx->thread_run_flag) { - ctx->thread_run_flag = 0; - pthread_join(ctx->thread_id, NULL); - } - - sem_destroy(&ctx->eos_sema); - - if (ctx->frame_queue) { - av_fifo_freep(&ctx->frame_queue); - ctx->frame_queue = NULL; - } - - ff_mutex_destroy(&ctx->queue_mutex); - if (ctx->bsf) { - av_bsf_free(&ctx->bsf); - ctx->bsf = NULL; - } - - av_buffer_unref(&ctx->hw_frame_ref); - av_buffer_unref(&ctx->hw_device_ref); - - av_log(avctx, AV_LOG_INFO, "Decode hw send packet count is: %llu.\n", ctx->total_out_frame_count); - av_log(avctx, AV_LOG_INFO, "Decode hw out frame count is: %llu.\n", ctx->total_packet_count); - - return 0; -} - -static int malloc_and_send_frame(AVCodecContext *avctx, const AVPacket *avpkt) -{ - ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; - int ret = 0; - if (ctx->first_packet) { - if (avctx->extradata_size) { - uint8_t* streamBuffer = NULL; - ret = hi_mpi_dvpp_malloc(ctx->device_id, &streamBuffer, avctx->extradata_size); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi malloc first packet failed, ret is %d.\n", ret); - return ret; - } - ret = aclrtMemcpy(streamBuffer, avctx->extradata_size, avctx->extradata, avctx->extradata_size, - ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "Mem copy H2D first packet failed, ret is %d.\n", ret); - return ret; - } - - hi_vdec_stream stream; - stream.pts = avpkt->pts; - stream.addr = streamBuffer; - stream.len = avctx->extradata_size; - stream.end_of_frame = HI_TRUE; - stream.end_of_stream = HI_FALSE; - stream.need_display = HI_FALSE; - - hi_vdec_pic_info pic_info; - pic_info.vir_addr = 0; - pic_info.buffer_size = 0; - pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; - ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi vdec send first packet failed, ret is %d.\n", ret); - return ret; - } - } - ctx->first_packet = 0; - } - - uint8_t* streamBuffer = NULL; - ret = hi_mpi_dvpp_malloc(ctx->device_id, &streamBuffer, avpkt->size); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi malloc packet failed, ret is %d.\n", ret); - return ret; - } - - ret = aclrtMemcpy(streamBuffer, avpkt->size, avpkt->data, avpkt->size, - ACL_MEMCPY_HOST_TO_DEVICE); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "Mem copy H2D first packet failed, ret is %d.\n", ret); - return ret; - } - - // create stream info - hi_vdec_stream stream; - stream.pts = avpkt->pts; - stream.addr = streamBuffer; - stream.len = avpkt->size; - stream.end_of_frame = HI_TRUE; - stream.end_of_stream = HI_FALSE; - stream.need_display = HI_TRUE; - - ff_mutex_lock(&ctx->queue_mutex); - av_fifo_generic_write(ctx->dts_queue, &avpkt->dts, sizeof(int64_t), NULL); - ff_mutex_unlock(&ctx->queue_mutex); - - // create frame info - hi_vdec_pic_info pic_info; - pic_info.width = ctx->resize_width; // Output image width, supports resize, set 0 means no resize. - pic_info.height = ctx->resize_height; // Output image height, supports resize, set 0 means no resize. - pic_info.width_stride = FFALIGN(ctx->vdec_width, VDEC_WIDTH_ALIGN); - pic_info.height_stride = FFALIGN(ctx->vdec_height, VDEC_HEIGHT_ALIGN); - if (ctx->resize_str && ctx->resize_width != 0 && ctx->resize_height != 0) { - pic_info.width_stride = FFALIGN(ctx->resize_width, VDEC_WIDTH_ALIGN); - pic_info.height_stride = FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN); - } - uint32_t size = pic_info.width_stride * pic_info.height_stride * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; - - pic_info.buffer_size = size; - pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; - void *picBuffer = NULL; - - ret = hi_mpi_dvpp_malloc(ctx->device_id, &picBuffer, size); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi malloc failed, ret is %d.\n", ret); - return ret; - } - pic_info.vir_addr = (uint64_t)picBuffer; - - do { - ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, VDEC_TIME_OUT); - if ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL) { - usleep(VDEC_SLEEP_TIME); - } - } while ((unsigned int)ret == HI_ERR_VDEC_BUF_FULL); - - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi send stream failed, ret is %d.\n", ret); - return ret; - } - - ctx->frame_id++; - ctx->total_packet_count++; - return 0; -} - -static int hi_mpi_decode(AVCodecContext *avctx, const AVPacket *avpkt) -{ - ASCENDContext_t *ctx = (ASCENDContext_t*) avctx->priv_data; - int ret = 0; - AVPacket packet = { 0 }; -// AVPacket bsf_packet = { 0 }; - -// if (avpkt && avpkt->size && ctx->bsf) { -// ret = av_packet_ref(&packet, avpkt); -// if (ret < 0) { -// av_log(avctx, AV_LOG_ERROR, "av_packet_ref failed, ret(%d).\n", ret); -// return ret; -// } -// ret = av_bsf_send_packet(ctx->bsf, &packet); -// if (ret < 0) { -// av_log(avctx, AV_LOG_ERROR, "av_bsf_send_packet failed, ret(%d).\n", ret); -// av_packet_unref(&packet); -// return ret; -// } -// ret = av_bsf_receive_packet(ctx->bsf, &bsf_packet); -// if (ret < 0) { -// av_log(avctx, AV_LOG_ERROR, "av_bsf_receive_packet failed, ret(%d).\n", ret); -// return ret; -// } -// avpkt = &bsf_packet; -// } - av_packet_unref(&packet); - - if (avpkt && avpkt->size) { - ret = malloc_and_send_frame(avctx, avpkt); - if (ret != 0) { - av_packet_unref(avpkt); - return AVERROR(EINVAL); - } - } else { - if (!ctx->decoder_flushing) { - hi_vdec_stream stream; - stream.addr = NULL; - stream.len = 0; - stream.end_of_frame = HI_FALSE; - stream.end_of_stream = HI_TRUE; // Stream end flag to flushing all data. - stream.need_display = HI_TRUE; - - hi_vdec_pic_info pic_info; - pic_info.vir_addr = 0; - pic_info.buffer_size = 0; - pic_info.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; - ret = hi_mpi_vdec_send_stream(ctx->channel_id, &stream, &pic_info, -1); - if (ret != 0) { - av_packet_unref(avpkt); - av_log(avctx, AV_LOG_ERROR, "Send last stream failed, ret is %d", ret); - return ret; - } - ctx->decoder_flushing = 1; - } - } - av_packet_unref(avpkt); - return 0; -} - -static int himpi_get_frame(AVCodecContext *avctx, AVFrame *frame) -{ - ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; - int ret = 0; - if (!ctx->frame_queue) { - return AVERROR(EAGAIN); - } - - FrameInfo_t frame_info; - ff_mutex_lock(&ctx->queue_mutex); - if (av_fifo_size(ctx->frame_queue) != 0) { - av_fifo_generic_read(ctx->frame_queue, &frame_info, sizeof(FrameInfo_t), NULL); - } else { - ff_mutex_unlock(&ctx->queue_mutex); - return AVERROR(EAGAIN); - } - av_log(avctx, AV_LOG_ERROR, "himpi_get_frame 0\n"); - ff_mutex_unlock(&ctx->queue_mutex); - - if (frame_info.event_type == EVENT_EOS) { - return AVERROR_EOF; - } - av_log(avctx, AV_LOG_ERROR, "avctx->pix_fmt is %d\n", avctx->pix_fmt); - if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { - ret = av_hwframe_get_buffer(ctx->hw_frame_ref, frame, 0); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "av_hwframe_get_buffer failed, ret is %d.\n", ret); - return AVERROR(EINVAL); - } - ret = ff_decode_frame_props(avctx, frame); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "ff_decode_frame_props failed, ret is %d.\n", ret); - return AVERROR(EINVAL); - } - } else { - ret = ff_get_buffer(avctx, frame, 0); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "Decode ff_get_buffer failed, ret is %d.\n", ret); - return AVERROR(EINVAL); - } - } - av_log(avctx, AV_LOG_ERROR, "himpi_get_frame 1\n"); - frame->pkt_pos = -1; - frame->pkt_duration = 0; - frame->pkt_size = -1; - frame->pts = frame_info.pts; - frame->pkt_pts = frame->pts; - frame->pkt_dts = frame_info.dts; - frame->width = frame_info.width_stride; - frame->height = frame_info.height_stride; - - switch (frame_info.format) { - case HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420: - av_log(avctx, AV_LOG_ERROR, "himpi_get_frame 2\n"); - if (avctx->pix_fmt == AV_PIX_FMT_ASCEND) { - uint32_t offset = 0; - for (int i = 0; i < 2; i++) { - size_t dstBytes = frame->width * frame->height * (i ? 1.0 / 2 : 1); - ret = aclrtMemcpy(frame->data[i], dstBytes, frame_info.data + offset, dstBytes, - ACL_MEMCPY_DEVICE_TO_DEVICE); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "Mem copy D2D failed, ret is %d.\n", ret); - hi_mpi_dvpp_free(frame_info.data); - return ret; - } - offset += dstBytes; - } - } else { - av_log(avctx, AV_LOG_ERROR, "himpi_get_frame 3\n"); - uint32_t offset = 0; - for (int i = 0; i < 2; i++) { - size_t dstBytes = frame->width * frame->height * (i ? 1.0 / 2 : 1); - ret = aclrtMemcpy(frame->data[i], dstBytes, frame_info.data + offset, dstBytes, - ACL_MEMCPY_DEVICE_TO_HOST); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "Mem copy D2H failed, ret is %d.\n", ret); - hi_mpi_dvpp_free(frame_info.data); - return ret; - } - offset += dstBytes; - } - } - ret = hi_mpi_dvpp_free(frame_info.data); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi free data failed, ret is %d.\n", ret); - } - break; - - default: - hi_mpi_dvpp_free(frame_info.data); - av_log(avctx, AV_LOG_ERROR, "Unsupport pixfmt: %d.\n", (int)frame_info.format); - break; - } - - - return 0; -} - -static int ff_himpi_receive_frame(AVCodecContext *avctx, AVFrame *frame) -{ - ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; - AVPacket pkt = { 0 }; - int send_ret = -1; - int get_ret = -1; - int ret = 0; - - if (avctx == NULL || avctx->priv_data == NULL) { - av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); - return AVERROR_BUG; - } - if (!ctx->hi_mpi_init_flag || !ctx->thread_run_flag || !ctx->decode_run_flag) { - av_log(avctx, AV_LOG_ERROR, "ff_himpi_receive_frame error, AVCodecContext is NULL.\n"); - return AVERROR_BUG; - } - - if (ctx->eos_received) { - return AVERROR_EOF; - } - - ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); - if (ret != 0) { - av_log(ctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); - return ret; - } - - while (ctx->decode_run_flag) { - if (!ctx->decoder_flushing) { - send_ret = ff_decode_get_packet(avctx, &pkt); - av_log(ctx, AV_LOG_ERROR, "ff_decode_get_packet ret is %d.\n", send_ret); - av_log(ctx, AV_LOG_ERROR, "pkt->size is %d.\n", pkt.size); - if (send_ret < 0 && send_ret != AVERROR_EOF) { - return send_ret; - } - send_ret = hi_mpi_decode(avctx, &pkt); - av_log(ctx, AV_LOG_ERROR, "hi_mpi_decode ret is %d.\n", send_ret); - av_packet_unref(&pkt); - if (send_ret < 0 && send_ret != AVERROR_EOF) { - av_log(ctx, AV_LOG_ERROR, "Send packet failed, ret is %d.\n", send_ret); - return send_ret; - } - } - - get_ret = himpi_get_frame(avctx, frame); - av_log(ctx, AV_LOG_ERROR, "himpi_get_frame ret is %d.\n", get_ret); - if (get_ret != 0 && get_ret != AVERROR_EOF) { - if (get_ret != AVERROR(EAGAIN)) { - return get_ret; - } - if (ctx->decoder_flushing) { - av_usleep(2000); - } - } else { - if (get_ret == AVERROR_EOF) { - ctx->eos_received = 1; - } - return get_ret; - } - } - - av_log(avctx, AV_LOG_ERROR, "Decode stop, error.\n"); - return AVERROR_BUG; - -} - -static av_cold int ff_himpi_decode_init(AVCodecContext *avctx) -{ - ASCENDContext_t *ctx = (ASCENDContext_t*)avctx->priv_data; - AVASCENDDeviceContext *hw_device_ctx; - AVHWFramesContext *hw_frame_ctx; - const AVBitStreamFilter *bsf; - int ret = 0; - - if (avctx == NULL || avctx->priv_data == NULL) { - av_log(avctx, AV_LOG_ERROR, "HiMpi decoder init failed, AVCodecContext is NULL.\n"); - return AVERROR_BUG; - } - - if (ctx->hi_mpi_init_flag == 1) { - av_log(avctx, AV_LOG_ERROR, "Error, himpi decode double init. \n"); - return AVERROR_BUG; - } - - enum AVPixelFormat pix_fmts[3] = { AV_PIX_FMT_ASCEND, AV_PIX_FMT_NV12, AV_PIX_FMT_NONE }; - avctx->pix_fmt = ff_get_format(avctx, pix_fmts); - av_log(avctx, AV_LOG_ERROR, "Error, ff_get_format failed with format id: %d.\n", avctx->pix_fmt); - if (avctx->pix_fmt < 0) { - av_log(avctx, AV_LOG_ERROR, "Error, ff_get_format failed with format id: %d.\n", avctx->pix_fmt); - return AVERROR_BUG; - } - - ctx->avctx = avctx; - - if (ctx->resize_str) { - ret = av_parse_video_size(&ctx->resize_width, &ctx->resize_height, ctx->resize_str); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be {width}x{height}.\n", - ctx->resize_str); - return AVERROR_BUG; - } - - if (ctx->resize_width != FFALIGN(ctx->resize_width, VDEC_WIDTH_ALIGN) || - ctx->resize_height != FFALIGN(ctx->resize_height, VDEC_HEIGHT_ALIGN)) { - av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be stride by %d and %d.\n", - ctx->resize_str, VDEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); - return AVERROR_BUG; - } - - if (ctx->resize_width < 128 || ctx->resize_height < 128 || - ctx->resize_width > 4096 || ctx->resize_height > 4096) { - av_log(avctx, AV_LOG_ERROR, "Invalid resize param: %s, which should be in [128x128 ~ 4096x4096].\n", - ctx->resize_str, VDEC_WIDTH_ALIGN, VDEC_HEIGHT_ALIGN); - return AVERROR_BUG; - } - avctx->coded_width = ctx->resize_width; - avctx->coded_height = ctx->resize_height; - } - - if (!ctx->resize_str || (ctx->resize_height == avctx->height && ctx->resize_width == avctx->width)) { - ctx->vdec_width = FFALIGN(avctx->width, VDEC_WIDTH_ALIGN); - ctx->vdec_height = FFALIGN(avctx->height, VDEC_HEIGHT_ALIGN); - ctx->resize_width = ctx->resize_height = 0; - } else { - av_log(avctx, AV_LOG_INFO, "Vdec resize: %dx%d.\n", ctx->resize_width, ctx->resize_height); - } - - ctx->vdec_width = avctx->width; - ctx->vdec_height = avctx->height; - - if (decode_params_checking(avctx) != 0) { - return AVERROR(EINVAL); - } - av_log(avctx, AV_LOG_DEBUG, "Vdec width: %d.\n", ctx->vdec_width); - av_log(avctx, AV_LOG_DEBUG, "Vdec height: %d.\n", ctx->vdec_height); - - if (avctx->hw_frames_ctx) { - av_buffer_unref(&ctx->hw_frame_ref); - ctx->hw_frame_ref = av_buffer_ref(avctx->hw_frames_ctx); - if (!ctx->hw_frame_ref) { - ret = AVERROR(EINVAL); - goto error; - } - - hw_frame_ctx = (AVHWFramesContext*)ctx->hw_frame_ref->data; - if (!hw_frame_ctx->pool || - (ctx->vdec_width != hw_frame_ctx->width && ctx->resize_width != hw_frame_ctx->width)) { - if (hw_frame_ctx->pool) { - av_buffer_pool_uninit(&hw_frame_ctx->pool); - } - hw_frame_ctx->width = ctx->resize_width == 0 ? ctx->vdec_width : ctx->resize_width; - hw_frame_ctx->height = ctx->resize_height == 0 ? ctx->vdec_height : ctx->resize_height; - hw_frame_ctx->initial_pool_size = 2; - hw_frame_ctx->format = AV_PIX_FMT_ASCEND; - hw_frame_ctx->sw_format = avctx->sw_pix_fmt; - - ret = av_hwframe_ctx_init(ctx->hw_frame_ref); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "HWFrame contex init failed.\n"); - return AVERROR(ENAVAIL); - } - } - ctx->hw_device_ref = av_buffer_ref(hw_frame_ctx->device_ref); - if (!ctx->hw_device_ref) { - av_log(avctx, AV_LOG_ERROR, "Get hw_device_ref failed.\n"); - ret = AVERROR(EINVAL); - goto error; - } - } else { - if (avctx->hw_device_ctx) { - ctx->hw_device_ref = av_buffer_ref(avctx->hw_device_ctx); - if (!ctx->hw_device_ref) { - av_log(avctx, AV_LOG_ERROR, "ref hwdevice failed.\n"); - ret = AVERROR(EINVAL); - goto error; - } - } else { - char dev_idx[sizeof(int)]; - sprintf(dev_idx, "%d", ctx->device_id); - av_log(avctx, AV_LOG_INFO, "dev_idx: %s.\n", dev_idx); - ret = av_hwdevice_ctx_create(&ctx->hw_device_ref, AV_HWDEVICE_TYPE_ASCEND, dev_idx, NULL, 0); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "hwdevice contex create failed.\n"); - goto error; - } - } - ctx->hw_frame_ref = av_hwframe_ctx_alloc(ctx->hw_device_ref); - if (!ctx->hw_frame_ref) { - av_log(avctx, AV_LOG_ERROR, "av_hwframe_ctx_alloc failed, ret is %d.\n", ret); - ret = AVERROR(EINVAL); - goto error; - } - hw_frame_ctx = (AVHWFramesContext*)ctx->hw_frame_ref->data; - if (!hw_frame_ctx->pool) { - hw_frame_ctx->width = ctx->resize_width == 0 ? ctx->vdec_width : ctx->resize_width; - hw_frame_ctx->height = ctx->resize_height == 0 ? ctx->vdec_height : ctx->resize_height; - hw_frame_ctx->initial_pool_size = 2; - hw_frame_ctx->format = AV_PIX_FMT_ASCEND; - hw_frame_ctx->sw_format = avctx->sw_pix_fmt; - ret = av_hwframe_ctx_init(ctx->hw_frame_ref); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "hwframe ctx init error, ret is %d.\n", ret); - return AVERROR(EINVAL); - } - } - } - hw_device_ctx = ((AVHWDeviceContext*)ctx->hw_device_ref->data)->hwctx; - ctx->hw_device_ctx = hw_device_ctx; - ctx->hw_frames_ctx = hw_frame_ctx; - ctx->ascend_ctx = ctx->hw_device_ctx->ascend_ctx; - - ctx->device_id = ctx->ascend_ctx->device_id; - ctx->frame_id = 0; - ctx->eos_received = 0; - ctx->total_out_frame_count = 0; - ctx->total_packet_count = 0; - ctx->decoder_flushing = 0; - ctx->first_packet = 1; - - ff_mutex_init(&ctx->queue_mutex, NULL); - ctx->codec_type = HI_PT_JPEG; - -// switch (avctx->codec->id) -// { -// case AV_CODEC_ID_H264: -// ctx->codec_type = HI_PT_H264; -// break; -// case AV_CODEC_ID_H265: -// ctx->codec_type = HI_PT_H265; -// break; -// default: -// av_log(avctx, AV_LOG_ERROR, "Invalid codec type, %d.\n", avctx->codec->id); -// return AVERROR_BUG; -// } -// ctx->bsf = NULL; -// if (avctx->codec->id == AV_CODEC_ID_H264 || avctx->codec->id == AV_CODEC_ID_H265) { -// if (avctx->codec->id == AV_CODEC_ID_H264) -// bsf = av_bsf_get_by_name("h264_mp4toannexb"); -// else if (avctx->codec->id == AV_CODEC_ID_H265) -// bsf = av_bsf_get_by_name("hevc_mp4toannexb"); -// if (!bsf) { -// ret = AVERROR_BSF_NOT_FOUND; -// goto error; -// } -// ret = av_bsf_alloc(bsf, &ctx->bsf); -// if (ret < 0) -// goto error; -// -// ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx); -// if (ret < 0) { -// av_bsf_free(&ctx->bsf); -// goto error; -// } -// ret = av_bsf_init(ctx->bsf); -// if (ret < 0) { -// av_bsf_free(&ctx->bsf); -// goto error; -// } -// } else { -// av_log(avctx, AV_LOG_ERROR, "Invalid codec id, %d.\n", avctx->codec->id); -// return AVERROR_BUG; -// } - - ctx->frame_queue = av_fifo_alloc(1000 * sizeof(FrameInfo_t)); - if (!ctx->frame_queue) { - av_log(avctx, AV_LOG_ERROR, "Failed to alloc frame fifo queue.\n"); - goto error; - } - - ctx->dts_queue = av_fifo_alloc(1000 * sizeof(int64_t)); - if (!ctx->dts_queue) { - av_log(avctx, AV_LOG_ERROR, "Failed to alloc dts fifo queue.\n"); - goto error; - } - - ret = aclrtSetCurrentContext(ctx->ascend_ctx->context); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "Set context failed, ret is %d.\n", ret); - goto error; - } - - ret = hi_mpi_sys_init(); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi sys init failed, ret is %d.\n", ret); - goto error; - } - - ctx->chn_attr_.type = ctx->codec_type; - ctx->chn_attr_.mode = HI_VDEC_SEND_MODE_FRAME; - ctx->chn_attr_.pic_width = ctx->vdec_width; - ctx->chn_attr_.pic_height = ctx->vdec_height; - - // Stream buffer size, Recommended value is width * height * 3 / 2 - ctx->chn_attr_.stream_buf_size = ctx->vdec_width * ctx->vdec_height * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; - ctx->chn_attr_.frame_buf_cnt = REF_FRAME_NUM + DISPLAY_FRAME_NUM + 1; - - // Create buf attribute - ctx->buf_attr_.width = ctx->chn_attr_.pic_width; - ctx->buf_attr_.height = ctx->chn_attr_.pic_height; - ctx->buf_attr_.align = 0; - ctx->buf_attr_.bit_width = HI_DATA_BIT_WIDTH_8; - ctx->buf_attr_.pixel_format = HI_PIXEL_FORMAT_YUV_SEMIPLANAR_420; - ctx->buf_attr_.compress_mode = HI_COMPRESS_MODE_NONE; - - ctx->chn_attr_.frame_buf_size = hi_vdec_get_pic_buf_size(ctx->chn_attr_.type, &ctx->buf_attr_); - - // Configure video decoder channel attribute - ctx->chn_attr_.video_attr.ref_frame_num = REF_FRAME_NUM; - ctx->chn_attr_.video_attr.temporal_mvp_en = HI_TRUE; - ctx->chn_attr_.video_attr.tmv_buf_size = hi_vdec_get_tmv_buf_size(ctx->chn_attr_.type, - ctx->chn_attr_.pic_width, - ctx->chn_attr_.pic_height); - - av_log(avctx, AV_LOG_INFO, "Channel Id is: %d.\n", ctx->channel_id); - ret = hi_mpi_vdec_create_chn(ctx->channel_id, &ctx->chn_attr_); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi create vdec channel failed, ret is %d.\n", ret); - goto error; - } - - // reset channel param. - ret = hi_mpi_vdec_get_chn_param(ctx->channel_id, &ctx->chn_param_); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi vdec get channel param failed, ret is %d.\n", ret); - goto error; - } - - ctx->chn_param_.video_param.dec_mode = HI_VIDEO_DEC_MODE_IPB; - ctx->chn_param_.video_param.compress_mode = HI_COMPRESS_MODE_HFBC; - ctx->chn_param_.video_param.video_format = HI_VIDEO_FORMAT_TILE_64x16; - ctx->chn_param_.display_frame_num = DISPLAY_FRAME_NUM; - ctx->chn_param_.video_param.out_order = HI_VIDEO_OUT_ORDER_DISPLAY; - - ret = hi_mpi_vdec_set_chn_param(ctx->channel_id, &ctx->chn_param_); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi vdec set channel param failed, ret is %d.\n", ret); - goto error; - } - - ret = hi_mpi_vdec_start_recv_stream(ctx->channel_id); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "HiMpi vdec start receive stream failed, ret is %d.\n", ret); - goto error; - } - ctx->hi_mpi_init_flag = 1; - ctx->decode_run_flag = 1; - - // create callback thread - ctx->thread_run_flag = 1; - ret = pthread_create(&ctx->thread_id, NULL, get_frame, (void *)ctx); - if (ret != 0) { - av_log(avctx, AV_LOG_ERROR, "pthread_create callback thread failed, ret is %d.\n", ret); - goto error; - } - - avctx->pkt_timebase.num = 1; - avctx->pkt_timebase.den = 90000; - if (!avctx->pkt_timebase.num || !avctx->pkt_timebase.den) { - av_log(avctx, AV_LOG_ERROR, "Invalid pkt_timebase.\n"); - } - av_log(avctx, AV_LOG_ERROR, "Invalid pkt_timebase.\n"); - sem_init(&ctx->eos_sema, 0, 0); - return 0; - -error: - sem_post(&ctx->eos_sema); - ff_himpi_decode_end(avctx); - return ret; -} - -static void ff_himpi_flush(AVCodecContext *avctx) { - ff_himpi_decode_end(avctx); - ff_himpi_decode_init(avctx); -} - -#define OFFSET(x) offsetof(ASCENDContext_t, x) -#define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM -static const AVOption options[] = { - { "device_id", "Use to choose the ascend chip.", OFFSET(device_id), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 8, VD}, - { "channel_id", "Set channelId of decoder.", OFFSET(channel_id), AV_OPT_TYPE_INT, { .i64 = 0}, 0, 255, VD}, - { "resize", "Resize (width)x(height).", OFFSET(resize_str), AV_OPT_TYPE_STRING, { .str = NULL}, 0, 0, VD}, - { NULL } -}; - -static const AVCodecHWConfigInternal* ascend_hw_configs[] = { - &(const AVCodecHWConfigInternal) { - .public = { - .pix_fmt = AV_PIX_FMT_ASCEND, - .methods = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX | AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX | \ - AV_CODEC_HW_CONFIG_METHOD_INTERNAL, - .device_type = AV_HWDEVICE_TYPE_ASCEND - }, - .hwaccel = NULL, - }, - NULL -}; - -#define ASCEND_DEC_CODEC(x, X) \ - static const AVClass x##_ascend_class = { \ - .class_name = #x "_ascend_dec", \ - .item_name = av_default_item_name, \ - .option = options, \ - .version = LIBAVUTIL_VERSION_INT, \ - }; \ - AVCodec ff_##x##_ascend_decoder = { \ - .name = #x "_ascend", \ - .long_name = NULL_IF_CONFIG_SMALL("Ascend HiMpi " #X " decoder"), \ - .type = AVMEDIA_TYPE_VIDEO, \ - .id = AV_CODEC_ID_MJPEG, \ - .priv_data_size = sizeof(ASCENDContext_t), \ - .priv_class = &x##_ascend_class, \ - .init = ff_himpi_decode_init, \ - .close = ff_himpi_decode_end, \ - .receive_frame = ff_himpi_receive_frame, \ - .flush = ff_himpi_flush, \ - .capabilities = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP | \ - FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM | FF_CODEC_CAP_SETS_PKT_DTS, \ - .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_ASCEND, \ - AV_PIX_FMT_NV12, \ - AV_PIX_FMT_NONE }, \ - .hw_configs = ascend_hw_configs, \ - .wrapper_name = "ascendmjpegdec", \ - }; - -#if CONFIG_MJPEG_ASCEND_DECODER -ASCEND_DEC_CODEC(mjpeg, MJPEG) -#endif \ No newline at end of file diff --git a/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.h b/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.h deleted file mode 100644 index cc8578152..000000000 --- a/mxVision/Ascendffmpeg/libavcodec/ascend_mjpeg_dec.h +++ /dev/null @@ -1,148 +0,0 @@ -/* - * Copyright(c) 2020. Huawei Technologies Co.,Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except int compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef FFMPEG_ASCEND_ASCEND_DEC_H -#define FFMPEG_ASCEND_ASCEND_DEC_H - -#include "libavutil/parseutils.h" -#include "libavutil/buffer.h" -#include "libavutil/mathematics.h" -#include "libavutil/hwcontext.h" -#include "libavutil/hwcontext_ascend.h" -#include "libavutil/fifo.h" -#include "libavutil/log.h" -#include "libavutil/opt.h" -#include "libavutil/time.h" -#include "libavutil/common.h" -#include "libavutil/pixdesc.h" -#include "libavutil/thread.h" -#include "libavutil/version.h" -#include "config.h" -#include "avcodec.h" -#include "decode.h" -#include "hwaccels.h" -#include "hwconfig.h" -#include "internal.h" -#include "libavutil/avutil.h" - -#include "acl/dvpp/hi_dvpp.h" - -#define VDEC_WIDTH_ALIGN 16 -#define VDEC_HEIGHT_ALIGN 2 - -#define REF_FRAME_NUM 8 -#define DISPLAY_FRAME_NUM 2 -#define VDEC_TIME_OUT 1000 -#define VDEC_GET_TIME_OUT 100 -#define VDEC_SLEEP_TIME 1000 - -#define YUV_BGR_CONVERT_3 3 -#define YUV_BGR_CONVERT_2 2 - -typedef enum { - EVENT_NEW_FRAME = 0, - EVENT_EOS = 1, -} eventType_t; - -typedef struct FrameInfo { - void *ascend_ctx; - eventType_t event_type; - uint8_t *data; - uint32_t data_size; - hi_pixel_format format; - int64_t pts; - int64_t dts; - uint32_t width_stride; - uint32_t height_stride; -} FrameInfo_t; - -typedef struct ASCENDContext { - AVClass* av_class; - int device_id; - int channel_id; - - pthread_t thread_id; - volatile int thread_run_flag; - volatile int hi_mpi_init_flag; - - /* - struct { - int x; - int y; - int w; - int h; - } crop; - struct { - int width; - int height; - } resize; - */ - - char *output_pixfmt; - sem_t eos_sema; - - AVBufferRef *hw_device_ref; - AVBufferRef *hw_frame_ref; - AVBSFContext *bsf; - AVCodecContext *avctx; - AVFifoBuffer *frame_queue; - AVFifoBuffer *dts_queue; - AVASCENDDeviceContext *hw_device_ctx; - AVHWFramesContext *hw_frames_ctx; - AscendContext *ascend_ctx; - - hi_vdec_chn_attr chn_attr_; - hi_pic_buf_attr buf_attr_; - hi_vdec_chn_param chn_param_; - - AVMutex queue_mutex; - - int max_width; - int max_height; - int vdec_width; - int vdec_height; - int stride_align; - char* resize_str; - int resize_width; - int resize_height; - hi_payload_type codec_type; - - volatile int frame_id; - int first_packet; - volatile int first_seq; - volatile int eos_received; - volatile int decoder_flushing; - volatile int decode_run_flag; - unsigned long long total_packet_count; - unsigned long long total_out_frame_count; - - hi_vdec_stream stream; - hi_vdec_pic_info pic_info; -} ASCENDContext_t; - -static inline void get_vdec_frame_info(FrameInfo_t* frame_info, hi_video_frame_info frame) -{ - uint32_t width_stride = frame.v_frame.width_stride[0]; - uint32_t height_stride = frame.v_frame.height_stride[0]; - frame_info->width_stride = width_stride; - frame_info->height_stride = height_stride; - frame_info->data_size = width_stride * height_stride * YUV_BGR_CONVERT_3 / YUV_BGR_CONVERT_2; - frame_info->format = frame.v_frame.pixel_format; - frame_info->pts = frame.v_frame.pts; - frame_info->data = frame.v_frame.virt_addr[0]; -} - -#endif // FFMPEG_ASCEND_ASCEND_DEC_H \ No newline at end of file -- Gitee