From patchwork Sat May 1 02:23:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brad Hards X-Patchwork-Id: 27548 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a11:cc:0:0:0:0 with SMTP id m12csp64243pxt; Fri, 30 Apr 2021 19:23:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmoWsOuf2PylS1klSlidT8dXkzwJmOqF4N6G83qtpNCtlPhcnAhLf0Uodgk6iX1Ka7V0et X-Received: by 2002:a05:6402:27d3:: with SMTP id c19mr9474502ede.129.1619835822183; Fri, 30 Apr 2021 19:23:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619835822; cv=none; d=google.com; s=arc-20160816; b=FQvOqC5KMJtkRl792F0ZGuMOiT+gD91OmEoyAj5smAqvf4twSJsQxyCYSJ1MWjNsxq BJ4HZJEGP/FB/ID9R8GKGZDq51WHl+ms0WKX79ww91ReLx4z0JhMTu//+o/O11UBQfKw oQr8xcYxDZNpGI22UWgvWBQ2JPgMZSi1LAjZOWcGz5l2xbwajXTgXWId9L0Ny8q1cA8z civlM1rHAaSLF/47OWeCMNyPV61AeVf0g7P8RytNiUmDLhUWrzuK8VQNKFYwXL1LQKeS OS9TjuQcKbFLk6qjPL+Cc0DXhUV3ghkPm97+Zl/KMcWDQ2VS6+aNb/3/rR8fpno1fiLH Mi5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:delivered-to; bh=NxsuuojewFPdzkup8c7icD8StTdcsOCp3/digFqAf9I=; b=lGu6C+bsYmOTfm1sEedryOjlIFqNp2Ju9A/DhwRruWGkJ1p4sfV4SdFt7y5Kx9x8X6 TMPs7Q6pzhjwJ1AgUcu50CHMIMm6NiYmbKI4IJ4/tm9oBfTf5ZTHIU7Nroknak74/ZL+ wc8YMvPMpDWSl1oAfwSZPNbn/wsxNH43g/lzLPXvL234ld8znFqmtuBmWYu72Y4q3int 1+CS9E0AFJH9gPqBj6lHq1TQUZADdyTd5RQc/tiEqnveYi7KJ1PCCt9qif4wbuxppZbk DU99RhVcssKpBonT/cf+U7daIr2meT5Zgz7Q9QAo92CN9Acr6ocJ1bGY1tQgNuhVdRc0 l4dg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s27si3426721edw.377.2021.04.30.19.23.41; Fri, 30 Apr 2021 19:23:42 -0700 (PDT) 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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 274AB6808E6; Sat, 1 May 2021 05:23:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from cp1.nerdvana.net.au (cp1-new.nerdvana.net.au [113.212.99.70]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6B81C680911 for ; Sat, 1 May 2021 05:23:17 +0300 (EEST) Received: from ppp115-205.static.internode.on.net ([150.101.115.205]:41406 helo=audax.fritz.box) by cp1.nerdvana.net.au with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1lcfHm-0008W8-K1; Sat, 01 May 2021 12:23:14 +1000 From: Brad Hards To: ffmpeg-devel@ffmpeg.org Date: Sat, 1 May 2021 12:23:04 +1000 Message-Id: <20210501022304.301425-5-bradh@frogmouth.net> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210501022304.301425-1-bradh@frogmouth.net> References: <20210501022304.301425-1-bradh@frogmouth.net> MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - cp1.nerdvana.net.au X-AntiAbuse: Original Domain - ffmpeg.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - frogmouth.net X-Get-Message-Sender-Via: cp1.nerdvana.net.au: authenticated_id: bradh@frogmouth.net X-Authenticated-Sender: cp1.nerdvana.net.au: bradh@frogmouth.net X-Source: X-Source-Args: X-Source-Dir: Subject: [FFmpeg-devel] [PATCH v2 4/4] doc: add example for user data unregistered SEI encoding 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 Cc: Brad Hards Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: PdWnwDpkkFn5 Signed-off-by: Brad Hards --- configure | 2 + doc/examples/.gitignore | 1 + doc/examples/Makefile | 1 + doc/examples/Makefile.example | 1 + doc/examples/encode_unregistered.c | 205 +++++++++++++++++++++++++++++ 5 files changed, 210 insertions(+) create mode 100644 doc/examples/encode_unregistered.c diff --git a/configure b/configure index 6580859ef2..1fad5b29ca 100755 --- a/configure +++ b/configure @@ -1708,6 +1708,7 @@ EXAMPLE_LIST=" demuxing_decoding_example encode_audio_example encode_video_example + encode_unregistered_example extract_mvs_example filter_audio_example filtering_audio_example @@ -3708,6 +3709,7 @@ decode_video_example_deps="avcodec avutil" demuxing_decoding_example_deps="avcodec avformat avutil" encode_audio_example_deps="avcodec avutil" encode_video_example_deps="avcodec avutil" +encode_unregistered_example_deps="avcodec avutil" extract_mvs_example_deps="avcodec avformat avutil" filter_audio_example_deps="avfilter avutil" filtering_audio_example_deps="avfilter avcodec avformat avutil" diff --git a/doc/examples/.gitignore b/doc/examples/.gitignore index 44960e1de7..d3f7739e68 100644 --- a/doc/examples/.gitignore +++ b/doc/examples/.gitignore @@ -4,6 +4,7 @@ /decode_video /demuxing_decoding /encode_audio +/encode_unregistered /encode_video /extract_mvs /filter_audio diff --git a/doc/examples/Makefile b/doc/examples/Makefile index 81bfd34d5d..00ef609c7b 100644 --- a/doc/examples/Makefile +++ b/doc/examples/Makefile @@ -5,6 +5,7 @@ EXAMPLES-$(CONFIG_DECODE_VIDEO_EXAMPLE) += decode_video EXAMPLES-$(CONFIG_DEMUXING_DECODING_EXAMPLE) += demuxing_decoding EXAMPLES-$(CONFIG_ENCODE_AUDIO_EXAMPLE) += encode_audio EXAMPLES-$(CONFIG_ENCODE_VIDEO_EXAMPLE) += encode_video +EXAMPLES-$(CONFIG_ENCODE_UNREGISTERED_EXAMPLE) += encode_unregistered EXAMPLES-$(CONFIG_EXTRACT_MVS_EXAMPLE) += extract_mvs EXAMPLES-$(CONFIG_FILTER_AUDIO_EXAMPLE) += filter_audio EXAMPLES-$(CONFIG_FILTERING_AUDIO_EXAMPLE) += filtering_audio diff --git a/doc/examples/Makefile.example b/doc/examples/Makefile.example index a232d97f98..f26a6597f8 100644 --- a/doc/examples/Makefile.example +++ b/doc/examples/Makefile.example @@ -18,6 +18,7 @@ EXAMPLES= avio_list_dir \ demuxing_decoding \ encode_audio \ encode_video \ + encode_unregistered \ extract_mvs \ filtering_video \ filtering_audio \ diff --git a/doc/examples/encode_unregistered.c b/doc/examples/encode_unregistered.c new file mode 100644 index 0000000000..e9309460c0 --- /dev/null +++ b/doc/examples/encode_unregistered.c @@ -0,0 +1,205 @@ +/* + * Copyright (c) 2001 Fabrice Bellard + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @file + * video encoding with SEI (user unregistered) API example + * + * @example encode_unregistered.c + */ + +#include +#include +#include + +#include + +#include +#include + +static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt, + FILE *outfile) +{ + int ret; + + /* send the frame to the encoder */ + if (frame) + printf("Send frame %3" PRId64 "\n", frame->pts); + + ret = avcodec_send_frame(enc_ctx, frame); + if (ret < 0) + { + fprintf(stderr, "Error sending a frame for encoding\n"); + exit(1); + } + + while (ret >= 0) + { + ret = avcodec_receive_packet(enc_ctx, pkt); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + return; + else if (ret < 0) + { + fprintf(stderr, "Error during encoding\n"); + exit(1); + } + + printf("Write packet %3" PRId64 " (size=%5d)\n", pkt->pts, pkt->size); + fwrite(pkt->data, 1, pkt->size, outfile); + av_packet_unref(pkt); + } +} + +int main(int argc, char **argv) +{ + const char *filename, *codec_name; + const AVCodec *codec; + AVCodecContext *c = NULL; + int i, ret, x, y; + FILE *f; + AVFrame *frame; + AVPacket *pkt; + AVFrameSideData *side_data; + // This is from MISB ST 2101 + char sei_message[] = {0xa5, 0x50, 0x52, 0xaf, 0x52, 0x16, 0x5f, 0x45, + 0xa3, 0x18, 0x1c, 0xfc, 0x7a, 0xbb, 0xc2, 0x67, + 0x01, 0x70, 0xF5, 0x92, 0xF0, 0x23, 0x73, 0x36, + 0x4A, 0xF8, 0xAA, 0x91, 0x62, 0xC0, 0x0F, 0x2E, + 0xB2, 0xDA, 0x16, 0xB7, 0x43, 0x41, 0x00, 0x08, + 0x41, 0xA0, 0xBE, 0x36, 0x5B, 0x5A, 0xB9, 0x6A, + 0x36, 0x45}; + + if (argc <= 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(0); + } + filename = argv[1]; + codec_name = argv[2]; + + codec = avcodec_find_encoder_by_name(codec_name); + if (!codec) { + fprintf(stderr, "Codec '%s' not found\n", codec_name); + exit(1); + } + + c = avcodec_alloc_context3(codec); + if (!c) + { + fprintf(stderr, "Could not allocate video codec context\n"); + exit(1); + } + + pkt = av_packet_alloc(); + if (!pkt) + exit(1); + + c->bit_rate = 400000; + c->width = 352; + c->height = 288; + /* frames per second */ + c->time_base = (AVRational){1, 25}; + c->framerate = (AVRational){25, 1}; + c->gop_size = 10; + c->max_b_frames = 0; + c->pix_fmt = AV_PIX_FMT_YUV420P; + if (codec->id == AV_CODEC_ID_H264) + av_opt_set(c->priv_data, "preset", "medium", 0); + + /* open it */ + ret = avcodec_open2(c, codec, NULL); + if (ret < 0) + { + fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret)); + exit(1); + } + + f = fopen(filename, "wb"); + if (!f) + { + fprintf(stderr, "Could not open %s\n", filename); + exit(1); + } + + frame = av_frame_alloc(); + if (!frame) + { + fprintf(stderr, "Could not allocate video frame\n"); + exit(1); + } + frame->format = c->pix_fmt; + frame->width = c->width; + frame->height = c->height; + side_data = av_frame_new_side_data(frame, AV_FRAME_DATA_SEI_UNREGISTERED, sizeof(sei_message)); + if (!side_data) + { + fprintf(stderr, "Could not allocate the video frame side data\n"); + exit(1); + } + memcpy(side_data->data, sei_message, side_data->size); + + ret = av_frame_get_buffer(frame, 0); + if (ret < 0) + { + fprintf(stderr, "Could not allocate the video frame data\n"); + exit(1); + } + + /* encode 1 second of video */ + for (i = 0; i < 25; i++) + { + fflush(stdout); + + /* make sure the frame data is writable */ + ret = av_frame_make_writable(frame); + if (ret < 0) + exit(1); + + /* prepare a dummy image */ + /* Y */ + for (y = 0; y < c->height; y++) { + for (x = 0; x < c->width; x++) { + frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3; + } + } + + /* Cb and Cr */ + for (y = 0; y < c->height/2; y++) { + for (x = 0; x < c->width/2; x++) { + frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2; + frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5; + } + } + + frame->pts = i; + encode(c, frame, pkt, f); + } + + /* flush the encoder */ + encode(c, NULL, pkt, f); + fclose(f); + + avcodec_free_context(&c); + av_frame_free(&frame); + av_packet_free(&pkt); + + return 0; +}