From patchwork Tue Jan 31 10:18:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 40189 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3ca3:b0:b9:1511:ac2c with SMTP id b35csp4097276pzj; Tue, 31 Jan 2023 02:19:06 -0800 (PST) X-Google-Smtp-Source: AK7set9ILyajU91n6utbnEWCOIZxmqXXa5XmhNiupDD9S8Re3s3bsum2dul3M7oUo8NqShwt6sR9 X-Received: by 2002:a05:6402:1c03:b0:4a2:6ef0:5c5d with SMTP id ck3-20020a0564021c0300b004a26ef05c5dmr941997edb.3.1675160346288; Tue, 31 Jan 2023 02:19:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675160346; cv=none; d=google.com; s=arc-20160816; b=el6CpoQiNhIoFrdBRTV2Go+UM1B2misJz/B5beJkVjT5CfrFsJYM0BH4bNYW+go2nO UpEYjXnxBx2ARlTk6hvCl7/Vw8ENtvKVFG5Kfww48fpEHsMI2xVdelFF1+QSWOt7td2/ CPyHZbqsPy1ferreFcVa2FfLmlUkGbrgIkBGVkBTEX/EP835QOUmQY528aUzoo5gj0w+ RoBkyE3uZJaciV9oemBh84pfrtZKYpM075C0p9fwjvaWemJWnqQQdf90vsMuIaX1+/WY NCLh7k6AHnxoC0uuu4K5dw5+fqHDX5EoaC+y6a9Eawl5JEz2sHf+47fVc8RooO833bwz P36A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:mime-version:dkim-signature:delivered-to; bh=f+nGDBB+acxK1bTEzicALKsCNTFPYUMbRB6J6FVzHbc=; b=wwJvVDS/ERjsbSeSNF2LGKdQPzvAV9+wuqFkjmKsyqg5/p5+BKWVe+fECTw8hB5X6K i4NMdOUDgxR8D48kc/1t6bq0/+orQs8U5wThBtvhgaOh7f79hQw/dV8xCth419FuHN43 TgGyl90ADuwc0ALR+UUHY6DgTWa0UJs+hewneNjH9BjpJQ2XJV+vse+oM1B4Xr1Mb2y6 P0dUsiJUmeAho7/c+5CLrjlXEidMYuZ9BTwwbujGIS/JPm3xb3lSM8h5ejGww1DfvLBy Hu0/SHlrDhDm0rPu34wfjKMwSC+1QcuExtKsd0ej/zO1/qrnaoxidUh9rm2KagTJw24I q/Cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Jcl5NJmG; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id bm14-20020a0564020b0e00b004a216fa2599si1832010edb.385.2023.01.31.02.19.05; Tue, 31 Jan 2023 02:19:06 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Jcl5NJmG; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7C91568BE0B; Tue, 31 Jan 2023 12:19:01 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-vs1-f45.google.com (mail-vs1-f45.google.com [209.85.217.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8FAF568B6A0 for ; Tue, 31 Jan 2023 12:18:55 +0200 (EET) Received: by mail-vs1-f45.google.com with SMTP id k6so15559294vsk.1 for ; Tue, 31 Jan 2023 02:18:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=UhFtsHfCHVGdX/kxwTW8TaPBm+NaSmbF0D4fRriIr9c=; b=Jcl5NJmGPOhVXn0H7cpEr708u7Rfi5bJiKbyzBQDAuPO5+Z4ldDWYqRrONp4rwOlmx oDDUpiQe53bQeznx25mu21A/oN4JhSp8tAFkCpZ+L0HZVZrwpme85bK1XS9kFHCoCgII bU5bpAZLYgL1O/oCUQFFFYVWD+Ind47qpGMvveeqgjrSvYgK8eZb4If0txxsnZsNKxa3 IIeMJmWmpOfRnz+qXnruh7cmnGBPbxFKuHCrtk64+bdCKlXxPtu/6/ZA26WNaMUHJ/L4 3K3/iUi/mxHYmDivKV8VNH1i0ReuVwq/4+kAc4Q4ghOdJ1otvw3GyLXm/65OznEZ1Y0P ZLkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=UhFtsHfCHVGdX/kxwTW8TaPBm+NaSmbF0D4fRriIr9c=; b=GsIfXL3m4nZXYg1+B4PHRrQOmtTeLcBXJH8Qlvxzm98+HICxFKm85H65sQa5GJMybx R4dOr3XPEnnN8wzu1tNUzN1id4J3XlZWQEJGZDNFQOKAI2wzaMZSrFpeBf+lwNj2EluO GLv6KrEJ/8iIW8fSi4uBgg1n+zZC/dy5sGBq9Vis4gMyH97WvnXitGYAXy6hC5M78/r3 3yHUYNd8BVDw4Imb9P5Z4LJV/sMMcwEgOJyfIL1MXzYksDg8KLnBG4taE9l0sZD3cvS0 RHgDqPFhGFu7i0NtWCRxk/f4wgXcnpTmeLYZKC8cqwBL36/lPC8GW64NvUrBAPQ6yrra 23sQ== X-Gm-Message-State: AO0yUKU06vaOVZ1m2w4LbJVt9V26yV6BeQ/6/28zTMPHrc+1FVs0mfLM pgMxP0l1mPMR/FigDsYwf1W6j25M96xFTxSSrO1rbiPI X-Received: by 2002:a67:b708:0:b0:3f2:b0b9:7612 with SMTP id h8-20020a67b708000000b003f2b0b97612mr1400241vsf.36.1675160333861; Tue, 31 Jan 2023 02:18:53 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a05:612c:2428:b0:32a:5eaa:92e4 with HTTP; Tue, 31 Jan 2023 02:18:52 -0800 (PST) From: Paul B Mahol Date: Tue, 31 Jan 2023 11:18:52 +0100 Message-ID: To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH] add media100 bsf and use it X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: KcBuwPtDWWre Patch attached. From c9ec824211cccb745b3a4ab014d6be726c8ef1b9 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Tue, 31 Jan 2023 10:18:17 +0100 Subject: [PATCH] avcodec: add media100_to_mjpegb bitstream filter and use it Signed-off-by: Paul B Mahol --- libavcodec/Makefile | 3 +- libavcodec/bitstream_filters.c | 1 + .../{media100.c => media100_to_mjpegb_bsf.c} | 112 +++++++----------- libavcodec/mjpegbdec.c | 15 +++ 4 files changed, 61 insertions(+), 70 deletions(-) rename libavcodec/{media100.c => media100_to_mjpegb_bsf.c} (65%) diff --git a/libavcodec/Makefile b/libavcodec/Makefile index df76d37598..4971832ff4 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -485,7 +485,7 @@ OBJS-$(CONFIG_MACE6_DECODER) += mace.o OBJS-$(CONFIG_MAGICYUV_DECODER) += magicyuv.o OBJS-$(CONFIG_MAGICYUV_ENCODER) += magicyuvenc.o OBJS-$(CONFIG_MDEC_DECODER) += mdec.o mpeg12.o mpeg12data.o -OBJS-$(CONFIG_MEDIA100_DECODER) += media100.o +OBJS-$(CONFIG_MEDIA100_DECODER) += mjpegbdec.o OBJS-$(CONFIG_METASOUND_DECODER) += metasound.o twinvq.o OBJS-$(CONFIG_MICRODVD_DECODER) += microdvddec.o ass.o OBJS-$(CONFIG_MIMIC_DECODER) += mimic.o @@ -1221,6 +1221,7 @@ OBJS-$(CONFIG_HEVC_METADATA_BSF) += h265_metadata_bsf.o h265_profile_le h2645data.o OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF) += hevc_mp4toannexb_bsf.o OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF) += imx_dump_header_bsf.o +OBJS-$(CONFIG_MEDIA100_TO_MJPEGB_BSF) += media100_to_mjpegb_bsf.o OBJS-$(CONFIG_MJPEG2JPEG_BSF) += mjpeg2jpeg_bsf.o OBJS-$(CONFIG_MJPEGA_DUMP_HEADER_BSF) += mjpega_dump_header_bsf.o OBJS-$(CONFIG_MPEG4_UNPACK_BFRAMES_BSF) += mpeg4_unpack_bframes_bsf.o diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c index a3bebefe5f..e8216819ca 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -43,6 +43,7 @@ extern const FFBitStreamFilter ff_hapqa_extract_bsf; extern const FFBitStreamFilter ff_hevc_metadata_bsf; extern const FFBitStreamFilter ff_hevc_mp4toannexb_bsf; extern const FFBitStreamFilter ff_imx_dump_header_bsf; +extern const FFBitStreamFilter ff_media100_to_mjpegb_bsf; extern const FFBitStreamFilter ff_mjpeg2jpeg_bsf; extern const FFBitStreamFilter ff_mjpega_dump_header_bsf; extern const FFBitStreamFilter ff_mp3_header_decompress_bsf; diff --git a/libavcodec/media100.c b/libavcodec/media100_to_mjpegb_bsf.c similarity index 65% rename from libavcodec/media100.c rename to libavcodec/media100_to_mjpegb_bsf.c index 37b98c9a57..dafb65a0ad 100644 --- a/libavcodec/media100.c +++ b/libavcodec/media100_to_mjpegb_bsf.c @@ -1,6 +1,6 @@ /* - * Media 100 decoder - * Copyright (c) 2022 Paul B Mahol + * Media 100 to MJPEGB bitstream filter + * Copyright (c) 2023 Paul B Mahol * * This file is part of FFmpeg. * @@ -21,55 +21,34 @@ /** * @file - * Media 100 decoder. + * Media 100 to MJPEGB bitstream filter. */ #include +#include "bsf.h" +#include "bsf_internal.h" #include "libavutil/intreadwrite.h" -#include "avcodec.h" #include "bytestream.h" -#include "codec_internal.h" typedef struct Media100Context { - AVCodecContext *avctx; // wrapper context for mjpegb AVPacket *pkt; } Media100Context; -static av_cold int media100_decode_init(AVCodecContext *avctx) +static av_cold int init(AVBSFContext *ctx) { - Media100Context *ctx = avctx->priv_data; - const AVCodec *codec; - int ret; - - codec = avcodec_find_decoder(AV_CODEC_ID_MJPEGB); - if (!codec) - return AVERROR_BUG; - ctx->avctx = avcodec_alloc_context3(codec); - if (!ctx->avctx) - return AVERROR(ENOMEM); - ctx->avctx->thread_count = 1; - ctx->avctx->flags = avctx->flags; - ctx->avctx->flags2 = avctx->flags2; - ctx->avctx->width = ctx->avctx->coded_width = avctx->width; - ctx->avctx->height = ctx->avctx->coded_height = avctx->height; - - ret = avcodec_open2(ctx->avctx, codec, NULL); - if (ret < 0) - return ret; + Media100Context *s = ctx->priv_data; - ctx->pkt = av_packet_alloc(); - if (!ctx->pkt) + s->pkt = av_packet_alloc(); + if (!s->pkt) return AVERROR(ENOMEM); return 0; } -static int media100_decode_frame(AVCodecContext *avctx, - AVFrame *frame, int *got_frame, - AVPacket *avpkt) +static int filter(AVBSFContext *ctx, AVPacket *avpkt) { - Media100Context *ctx = avctx->priv_data; + Media100Context *s = ctx->priv_data; unsigned second_field_offset = 0; unsigned next_field = 0; unsigned dht_offset[2]; @@ -83,18 +62,20 @@ static int media100_decode_frame(AVCodecContext *avctx, AVPacket *pkt; int ret; - if (avpkt->size + 1024 > ctx->pkt->size) { - ret = av_grow_packet(ctx->pkt, avpkt->size + 1024 - ctx->pkt->size); - if (ret < 0) - return ret; - } + ret = ff_bsf_get_packet_ref(ctx, avpkt); + if (ret < 0) + return ret; - ret = av_packet_make_writable(ctx->pkt); + ret = av_new_packet(s->pkt, avpkt->size + 1024); + if (ret < 0) + return ret; + + ret = av_packet_make_writable(s->pkt); if (ret < 0) return ret; bytestream2_init(&gb, avpkt->data, avpkt->size); - bytestream2_init_writer(&pb, ctx->pkt->data, ctx->pkt->size); + bytestream2_init_writer(&pb, s->pkt->data, s->pkt->size); second_field: bytestream2_put_be32(&pb, 0); @@ -107,8 +88,8 @@ second_field: sof_offset[field] = bytestream2_tell_p(&pb); bytestream2_put_be16(&pb, 17); bytestream2_put_byte(&pb, 8); - bytestream2_put_be16(&pb, avctx->height / 2); - bytestream2_put_be16(&pb, avctx->width); + bytestream2_put_be16(&pb, ctx->par_in->height / 2); + bytestream2_put_be16(&pb, ctx->par_in->width); bytestream2_put_byte(&pb, 3); bytestream2_put_byte(&pb, 1); bytestream2_put_byte(&pb, 0x21); @@ -164,7 +145,7 @@ second_field: goto second_field; } - pkt = ctx->pkt; + pkt = s->pkt; AV_WB32(pkt->data + 8, second_field_offset); AV_WB32(pkt->data + 12, second_field_offset); @@ -186,40 +167,33 @@ second_field: pkt->size = bytestream2_tell_p(&pb); - ret = avcodec_send_packet(ctx->avctx, pkt); - if (ret < 0) { - av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n"); - return ret; - } + av_packet_copy_props(pkt, avpkt); + av_packet_unref(avpkt); + av_packet_move_ref(avpkt, s->pkt); - ret = avcodec_receive_frame(ctx->avctx, frame); - if (ret < 0) - return ret; + return 0; +} - avctx->pix_fmt = ctx->avctx->pix_fmt; - *got_frame = 1; +static void flush(AVBSFContext *ctx) +{ + Media100Context *s = ctx->priv_data; - return avpkt->size; + av_packet_unref(s->pkt); } -static av_cold int media100_decode_end(AVCodecContext *avctx) +static av_cold void uninit(AVBSFContext *ctx) { - Media100Context *ctx = avctx->priv_data; + Media100Context *s = ctx->priv_data; - avcodec_free_context(&ctx->avctx); - av_packet_free(&ctx->pkt); - - return 0; + av_packet_free(&s->pkt); } -const FFCodec ff_media100_decoder = { - .p.name = "media100", - CODEC_LONG_NAME("Media 100"), - .p.type = AVMEDIA_TYPE_VIDEO, - .p.id = AV_CODEC_ID_MEDIA100, - .priv_data_size = sizeof(Media100Context), - .init = media100_decode_init, - .close = media100_decode_end, - FF_CODEC_DECODE_CB(media100_decode_frame), - .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, +const FFBitStreamFilter ff_media100_to_mjpegb_bsf = { + .p.name = "media100_to_mjpegb", + .p.codec_ids = (const enum AVCodecID []){ AV_CODEC_ID_MEDIA100, AV_CODEC_ID_NONE }, + .priv_data_size = sizeof(Media100Context), + .init = init, + .flush = flush, + .close = uninit, + .filter = filter, }; diff --git a/libavcodec/mjpegbdec.c b/libavcodec/mjpegbdec.c index 6d422e7a29..4db1d9a89d 100644 --- a/libavcodec/mjpegbdec.c +++ b/libavcodec/mjpegbdec.c @@ -168,3 +168,18 @@ const FFCodec ff_mjpegb_decoder = { .p.max_lowres = 3, .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, }; + +const FFCodec ff_media100_decoder = { + .p.name = "media100", + CODEC_LONG_NAME("Media 100"), + .p.type = AVMEDIA_TYPE_VIDEO, + .p.id = AV_CODEC_ID_MEDIA100, + .priv_data_size = sizeof(MJpegDecodeContext), + .init = ff_mjpeg_decode_init, + .close = ff_mjpeg_decode_end, + FF_CODEC_DECODE_CB(mjpegb_decode_frame), + .p.capabilities = AV_CODEC_CAP_DR1, + .p.max_lowres = 3, + .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, + .bsfs = "media100_to_mjpegb", +}; -- 2.39.1