[FFmpeg-devel] [PATCH 5/5] doc: add example for user data unregistered SEI encoding

Brad Hards bradh at frogmouth.net
Fri Apr 30 14:34:32 EEST 2021


Signed-off-by: Brad Hards <bradh at frogmouth.net>
---
 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libavcodec/avcodec.h>
+
+#include <libavutil/opt.h>
+#include <libavutil/imgutils.h>
+
+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 <output file> <codec name>\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;
+}
-- 
2.27.0



More information about the ffmpeg-devel mailing list