Recherche avancée

Médias (91)

Autres articles (109)

  • Le profil des utilisateurs

    12 avril 2011, par

    Chaque utilisateur dispose d’une page de profil lui permettant de modifier ses informations personnelle. Dans le menu de haut de page par défaut, un élément de menu est automatiquement créé à l’initialisation de MediaSPIP, visible uniquement si le visiteur est identifié sur le site.
    L’utilisateur a accès à la modification de profil depuis sa page auteur, un lien dans la navigation "Modifier votre profil" est (...)

  • Les autorisations surchargées par les plugins

    27 avril 2010, par

    Mediaspip core
    autoriser_auteur_modifier() afin que les visiteurs soient capables de modifier leurs informations sur la page d’auteurs

  • Publier sur MédiaSpip

    13 juin 2013

    Puis-je poster des contenus à partir d’une tablette Ipad ?
    Oui, si votre Médiaspip installé est à la version 0.2 ou supérieure. Contacter au besoin l’administrateur de votre MédiaSpip pour le savoir

Sur d’autres sites (12787)

  • Ffmpeg fail on avformat_open_input() while try to encode YUV420p uncompressed video, input from custom IO instead of file. (C++ code)

    25 avril 2023, par devprog

    I am trying to encode YUV420p raw data that being streamed live from a private memory implementation. It didn't work. As a preliminary stage to private memory reading, and after some searches, I tried to read Custom IO input according ffmpeg example. This didn't work either. So, for my tests I created YUV file using

    


    ffmpeg -f lavfi -i testsrc=duration=10:size=1920x1080:rate=30 1920x1080p30_10s.mp4


    


    create a mp4 file. As a note, it turns out that it creates mp4 file with yuv444 color system. I changed it to yuv420p with

    


    ffmpeg -i 1920x1080p30_10s.mp4 -c:v libx264 -pix_fmt yuv420p 1920x1080p30_10s_420p.mp4


    


    then create a yuv file with

    


    ffmpeg -i 1920x1080p30_10s_420p.mp4 -c:v rawvideo -pixel_format yuv420p output_1920x1080_420p.yuv


    


    Test the output_1920x1080_420p.yuv with ffplay, looks good. This file will be my input to custom IO operation.

    


    Then wrote a sample code as follows (based on several links) :
avio_reading ffmpeg example.
Creating Custom FFmpeg IO-Context

    


    Code fail with the function

    


        if ((ret = avformat_open_input(&pAVFormatContext, "IO", NULL, &options_in) < 0) )
    {
        printf ("err=%d\n", ret);
        av_strerror(ret, errbuf, sizeof(errbuf));
        fprintf(stderr, "Unable to open err=%s\n", errbuf);
        return false;
    }


    


    Some notes :

    


      

    1. I added the code, the compile command & the output
    2. 


    3. I tried use with option parameters to avformat_open_input() & without, it failed.
    4. 


    5. I tried to use with seek callback to avio_alloc_context() & without, it failed.
    6. 


    7. I tries to use instead of YUV some mp4 file and looks as running (didn't fail) - BUT it is NOT what I need. I Need a YUV input
    8. 


    


    Can anyone help ? Is it even possible ?

    


    My following code compiles with :

    


    g++ ffmpeg_custom_io.cpp -lavformat -lavdevice -lavcodec -lavutil -lswscale -lswresample -lpthread -pthread -o ffmpeg_custom_io


    


    and whole code is as follows :

    


    using namespace std;&#xA;&#xA;#include &#xA;#include &#xA;#include &#xA;#include <iostream>&#xA;&#xA;extern "C" {&#xA;    #include <libavformat></libavformat>avformat.h>&#xA;    #include <libavcodec></libavcodec>avcodec.h>&#xA;    &#xA;    #include <libavutil></libavutil>opt.h>&#xA;    #include <libavutil></libavutil>imgutils.h>&#xA;    #include <libavutil></libavutil>file.h>&#xA;}&#xA;&#xA;&#xA;struct buffer_data {&#xA;    uint8_t *   bd_ptr;&#xA;    size_t      bd_size; ///&lt; size left in the buffer&#xA;};&#xA;&#xA;int readFileBuffer(void *opaque, uint8_t *buf, int buf_size)&#xA;{&#xA;&#xA;    printf("Start reading custom IO\n");&#xA;    struct buffer_data *bd = (struct buffer_data *)opaque;&#xA;    buf_size = FFMIN(buf_size, bd->bd_size);&#xA;&#xA;    printf("min buf size:%d\n", buf_size);&#xA;&#xA;    if (!buf_size) {&#xA;        printf("return -1, size = %d\n", buf_size);&#xA;        return -1;&#xA;    }&#xA;    printf("ptr:%p size:%zu\n", bd->bd_ptr, bd->bd_size);&#xA;&#xA;    /* copy internal buffer data to buf */&#xA;    memcpy(buf, bd->bd_ptr, buf_size);&#xA;    bd->bd_ptr  &#x2B;= buf_size;&#xA;    bd->bd_size -= buf_size;&#xA;    printf("End reading custom IO\n");&#xA;    return buf_size;&#xA;&#xA;}&#xA;&#xA;int64_t seekFunction(void* opaque, int64_t offset, int whence)&#xA;{&#xA;    std::cout &lt;&lt; "SEEK " &lt;&lt; std::endl;&#xA;    if (whence == AVSEEK_SIZE)&#xA;        return -1; // I don&#x27;t know "size of my handle in bytes"&#xA;}&#xA;&#xA;static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,&#xA;                   FILE *outfile)&#xA;{&#xA;    int ret;&#xA;&#xA;    /* send the frame to the encoder */&#xA;    if (frame)&#xA;        printf("Send frame %3" PRId64 "\n", frame->pts);&#xA;&#xA;    ret = avcodec_send_frame(enc_ctx, frame);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Error sending a frame for encoding\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    while (ret >= 0) {&#xA;        ret = avcodec_receive_packet(enc_ctx, pkt);&#xA;        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;            return;&#xA;        else if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error during encoding\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        printf("Write packet %3" PRId64 " (size=%5d)\n", pkt->pts, pkt->size);&#xA;        fwrite(pkt->data, 1, pkt->size, outfile);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;}&#xA;&#xA;int main(int argc, char **argv)&#xA;{&#xA;&#xA;    av_log_set_level(AV_LOG_MAX_OFFSET);&#xA;&#xA;    AVFormatContext* pAVFormatContext;&#xA;    uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;&#xA;    size_t buffer_size, avio_ctx_buffer_size = 32768;&#xA;&#xA;    int ret = 0;&#xA;    char errbuf[100];&#xA;&#xA;    // Alloc format context&#xA;    //--------------------&#xA;    if ( !(pAVFormatContext = avformat_alloc_context()) )&#xA;    {&#xA;        ret = AVERROR(ENOMEM);&#xA;        std::cout &lt;&lt; "error=" &lt;&lt; ret &lt;&lt; " Fail to allocate avformat_alloc_context()\n";&#xA;        return false;&#xA;    }&#xA;&#xA;    // Copy from ffmpeg example - bd buffer&#xA;    //--------------------&#xA;    struct buffer_data bd = { 0, 0 };&#xA;&#xA;    // slurp file content into buffer&#xA;    ret = av_file_map("output_1920x1080_420p.yuv", &amp;buffer, &amp;buffer_size, 0, NULL);&#xA;    if (ret &lt; 0)&#xA;    {&#xA;        std::cout &lt;&lt; "error=" &lt;&lt; ret &lt;&lt; " Fail to allocate avformat_alloc_context()\n";&#xA;        return false;&#xA;    }&#xA;&#xA;    // fill opaque structure used by the AVIOContext read callback&#xA;    bd.bd_ptr  = buffer;&#xA;    bd.bd_size = buffer_size;&#xA;&#xA;    // Prepare AVIO context&#xA;    //--------------------&#xA;    avio_ctx_buffer = static_cast(av_malloc(avio_ctx_buffer_size &#x2B; AV_INPUT_BUFFER_PADDING_SIZE));&#xA;&#xA;    if (!avio_ctx_buffer) {&#xA;        ret = AVERROR(ENOMEM);&#xA;        std::cout &lt;&lt; "error=" &lt;&lt; ret &lt;&lt; " Fail to allocate av_malloc()\n";&#xA;        return false;&#xA;    }&#xA;&#xA;    AVIOContext* pAVIOContext = avio_alloc_context(avio_ctx_buffer,&#xA;                                                   avio_ctx_buffer_size,&#xA;                                                   0,&#xA;                                                   &amp;bd,&#xA;                                                   readFileBuffer,&#xA;                                                   NULL,&#xA;                                                   seekFunction);&#xA;&#xA;    if (!pAVIOContext) {&#xA;        ret = AVERROR(ENOMEM);&#xA;        std::cout &lt;&lt; "error=" &lt;&lt; ret &lt;&lt; " Fail to allocate avio_alloc_context()\n";&#xA;        return false;&#xA;    }&#xA;&#xA;    pAVFormatContext->pb = pAVIOContext;&#xA;    pAVFormatContext->flags = AVFMT_FLAG_CUSTOM_IO; // - NOT SURE ABOUT THIS FLAG&#xA;&#xA;    // Handle ffmpeg input&#xA;    //--------------------&#xA;    AVDictionary* options_in = NULL;&#xA;    //av_dict_set(&amp;options_in, "framerate", "30", 0);&#xA;    av_dict_set(&amp;options_in, "video_size", "1920x1080", 0);&#xA;    av_dict_set(&amp;options_in, "pixel_format", "yuv420p", 0);&#xA;    av_dict_set(&amp;options_in, "vcodec", "rawvideo", 0);&#xA;&#xA;    if ((ret = avformat_open_input(&amp;pAVFormatContext, "IO", NULL, &amp;options_in) &lt; 0) )&#xA;    {&#xA;        printf ("err=%d\n", ret);&#xA;        av_strerror(ret, errbuf, sizeof(errbuf));&#xA;        fprintf(stderr, "Unable to open err=%s\n", errbuf);&#xA;        return false;&#xA;    }&#xA;&#xA;    //Raw video doesn&#x27;t contain any stream information.&#xA;    //if ((ret = avformat_find_stream_info(pAVFormatContext, 0)) &lt; 0) {&#xA;    //    fprintf(stderr, "Failed to retrieve input stream information");&#xA;    //}&#xA;&#xA;    AVCodec* pAVCodec = avcodec_find_decoder(AV_CODEC_ID_RAWVIDEO); //Get pointer to rawvideo codec.&#xA;&#xA;    AVCodecContext* pAVCodecContext = avcodec_alloc_context3(pAVCodec); //Allocate codec context.&#xA;&#xA;    //Fill the codec context based on the values from the codec parameters.&#xA;    AVStream *vid_stream = pAVFormatContext->streams[0];&#xA;    avcodec_parameters_to_context(pAVCodecContext, vid_stream->codecpar);&#xA;&#xA;    avcodec_open2(pAVCodecContext, pAVCodec, NULL); //Open the codec&#xA;&#xA;    //Allocate memory for packet and frame&#xA;    AVPacket* pAVPacketIn = av_packet_alloc();&#xA;    AVFrame* pAVFrameOut = av_frame_alloc();&#xA;&#xA;    /* find the mpeg1video encoder */&#xA;    AVCodec* pAVCodecOut = avcodec_find_encoder_by_name("libx264");&#xA;    if (!pAVCodecOut) {&#xA;        fprintf(stderr, "Codec &#x27;%s&#x27; not found\n", "libx264");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    AVCodecContext* pAVCodecContextOut = avcodec_alloc_context3(pAVCodecOut);&#xA;    if (!pAVCodecContextOut) {&#xA;        fprintf(stderr, "Could not allocate video codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    AVPacket* pAVPacketOut = av_packet_alloc();&#xA;    if (!pAVPacketOut)&#xA;        exit(1);&#xA;&#xA;    /* put sample parameters */&#xA;    pAVCodecContextOut->bit_rate = 2000000;&#xA;    /* resolution must be a multiple of two */&#xA;    pAVCodecContextOut->width = 1280;&#xA;    pAVCodecContextOut->height = 720;&#xA;    /* frames per second */&#xA;    pAVCodecContextOut->time_base = (AVRational){1, 30};&#xA;    pAVCodecContextOut->framerate = (AVRational){30, 1};&#xA;&#xA;    /* emit one intra frame every ten frames&#xA;     * check frame pict_type before passing frame&#xA;     * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I&#xA;     * then gop_size is ignored and the output of encoder&#xA;     * will always be I frame irrespective to gop_size&#xA;     */&#xA;    pAVCodecContextOut->gop_size = 10;&#xA;    pAVCodecContextOut->max_b_frames = 0;&#xA;    pAVCodecContextOut->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;&#xA;    if (pAVCodecOut->id == AV_CODEC_ID_H264)&#xA;        av_opt_set(pAVCodecContextOut->priv_data, "preset", "slow", 0);&#xA;&#xA;    /* open it */&#xA;    ret = avcodec_open2(pAVCodecContextOut, pAVCodecOut, NULL);&#xA;    if (ret &lt; 0) {&#xA;        //fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    FILE* filename_out = fopen("out_vid_h264.mp4", "wb");&#xA;    if (!filename_out) {&#xA;        fprintf(stderr, "Could not open %s\n", "out_vid_h264.mp4");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    pAVFrameOut->format = pAVCodecContextOut->pix_fmt;&#xA;    pAVFrameOut->width  = pAVCodecContextOut->width;&#xA;    pAVFrameOut->height = pAVCodecContextOut->height;&#xA;&#xA;&#xA;    uint8_t endcode_suf[] = { 0, 0, 1, 0xb7 };&#xA;    int i = 0;&#xA;    //Read video frames and pass through the decoder.&#xA;    //Note: Since the video is rawvideo, we don&#x27;t really have to pass it through the decoder.&#xA;    while (av_read_frame(pAVFormatContext, pAVPacketIn) >= 0)&#xA;    {&#xA;        //The video is not encoded - passing through the decoder is simply copy the data.&#xA;        avcodec_send_packet(pAVCodecContext, pAVPacketIn);    //Supply raw packet data as input to the "decoder".&#xA;        avcodec_receive_frame(pAVCodecContext, pAVFrameOut);   //Return decoded output data from the "decoder".&#xA;&#xA;        fflush(stdout);&#xA;&#xA;        pAVFrameOut->pts = i&#x2B;&#x2B;;&#xA;&#xA;        /* encode the image */&#xA;        encode(pAVCodecContextOut, pAVFrameOut, pAVPacketOut, filename_out);&#xA;    }&#xA;&#xA;    /* flush the encoder */&#xA;    encode(pAVCodecContextOut, NULL, pAVPacketOut, filename_out);&#xA;&#xA;    /* Add sequence end code to have a real MPEG file.&#xA;       It makes only sense because this tiny examples writes packets&#xA;       directly. This is called "elementary stream" and only works for some&#xA;       codecs. To create a valid file, you usually need to write packets&#xA;       into a proper file format or protocol; see mux.c.&#xA;     */&#xA;    if (pAVCodecOut->id == AV_CODEC_ID_MPEG1VIDEO || pAVCodecOut->id == AV_CODEC_ID_MPEG2VIDEO)&#xA;        fwrite(endcode_suf, 1, sizeof(endcode_suf), filename_out);&#xA;    fclose(filename_out);&#xA;&#xA;&#xA;    return 0;&#xA;}&#xA;</iostream>

    &#xA;

    The output

    &#xA;

    ./ffmpeg_custom_io &#xA;Start reading custom IO&#xA;min buf size:32768&#xA;ptr:0x7f236a200000 size:933120000&#xA;End reading custom IO&#xA;Probing adp score:25 size:2048&#xA;Probing adp score:25 size:4096&#xA;Probing adp score:25 size:8192&#xA;Probing adp score:25 size:16384&#xA;Start reading custom IO&#xA;min buf size:32768&#xA;ptr:0x7f236a208000 size:933087232&#xA;End reading custom IO&#xA;Start reading custom IO&#xA;min buf size:65536&#xA;ptr:0x7f236a210000 size:933054464&#xA;End reading custom IO&#xA;Start reading custom IO&#xA;min buf size:131072&#xA;ptr:0x7f236a220000 size:932988928&#xA;End reading custom IO&#xA;Start reading custom IO&#xA;min buf size:262144&#xA;ptr:0x7f236a240000 size:932857856&#xA;End reading custom IO&#xA;Start reading custom IO&#xA;min buf size:524288&#xA;ptr:0x7f236a280000 size:932595712&#xA;End reading custom IO&#xA;err=1&#xA;Unable to open err=Error number 1 occurred&#xA;&#xA;

    &#xA;

    I tried all as noted above expecting avformat_open_input() will succeed, but it failed.

    &#xA;

  • Encoding YUV420p uncompressed video that comes from custom IO instead of file. (C++ code)

    24 avril 2023, par devprog

    I am trying to encode YUV420p raw data that being streamed live from a private memory implementation. It didn't work. As a preliminary stage to private memory reading, and after some searches, I tried to read Custom IO input according ffmpeg example. This didn't work either. So, for my tests I created YUV file using

    &#xA;

    ffmpeg -f lavfi -i testsrc=duration=10:size=1920x1080:rate=30 1920x1080p30_10s.mp4&#xA;

    &#xA;

    create a mp4 file. As a note, it turns out that it creates mp4 file with yuv444 color system. I changed it to yuv420p with

    &#xA;

    ffmpeg -i 1920x1080p30_10s.mp4 -c:v libx264 -pix_fmt yuv420p 1920x1080p30_10s_420p.mp4&#xA;

    &#xA;

    then create a yuv file with

    &#xA;

    ffmpeg -i 1920x1080p30_10s_420p.mp4 -c:v rawvideo -pixel_format yuv420p output_1920x1080_420p.yuv&#xA;

    &#xA;

    Test the output_1920x1080_420p.yuv with ffplay, looks good. This file will be my input to custom IO operation.

    &#xA;

    Then wrote a sample code as follows (based on several links) :&#xA;avio_reading ffmpeg example.&#xA;Creating Custom FFmpeg IO-Context

    &#xA;

    Code fail with the function

    &#xA;

        if ((ret = avformat_open_input(&amp;pAVFormatContext, "IO", NULL, &amp;options_in) &lt; 0) )&#xA;    {&#xA;        printf ("err=%d\n", ret);&#xA;        av_strerror(ret, errbuf, sizeof(errbuf));&#xA;        fprintf(stderr, "Unable to open err=%s\n", errbuf);&#xA;        return false;&#xA;    }&#xA;

    &#xA;

    Some notes :

    &#xA;

      &#xA;
    1. I added the code, the compile command & the output
    2. &#xA;

    3. I tried use with option parameters to avformat_open_input() & without, it failed.
    4. &#xA;

    5. I tried to use with seek callback to avio_alloc_context() & without, it failed.
    6. &#xA;

    7. I tries to use instead of YUV some mp4 file and looks as running (didn't fail) - BUT it is NOT what I need. I Need a YUV input
    8. &#xA;

    &#xA;

    Can anyone help ? Is it even possible ?

    &#xA;

    My following code compiles with :

    &#xA;

    g&#x2B;&#x2B; ffmpeg_custom_io.cpp -lavformat -lavdevice -lavcodec -lavutil -lswscale -lswresample -lpthread -pthread -o ffmpeg_custom_io&#xA;

    &#xA;

    and whole code is as follows :

    &#xA;

    using namespace std;&#xA;&#xA;#include &#xA;#include &#xA;#include &#xA;#include <iostream>&#xA;&#xA;extern "C" {&#xA;    #include <libavformat></libavformat>avformat.h>&#xA;    #include <libavcodec></libavcodec>avcodec.h>&#xA;    &#xA;    #include <libavutil></libavutil>opt.h>&#xA;    #include <libavutil></libavutil>imgutils.h>&#xA;    #include <libavutil></libavutil>file.h>&#xA;}&#xA;&#xA;&#xA;struct buffer_data {&#xA;    uint8_t *   bd_ptr;&#xA;    size_t      bd_size; ///&lt; size left in the buffer&#xA;};&#xA;&#xA;int readFileBuffer(void *opaque, uint8_t *buf, int buf_size)&#xA;{&#xA;&#xA;    printf("Start reading custom IO\n");&#xA;    struct buffer_data *bd = (struct buffer_data *)opaque;&#xA;    buf_size = FFMIN(buf_size, bd->bd_size);&#xA;&#xA;    printf("min buf size:%d\n", buf_size);&#xA;&#xA;    if (!buf_size) {&#xA;        printf("return -1, size = %d\n", buf_size);&#xA;        return -1;&#xA;    }&#xA;    printf("ptr:%p size:%zu\n", bd->bd_ptr, bd->bd_size);&#xA;&#xA;    /* copy internal buffer data to buf */&#xA;    memcpy(buf, bd->bd_ptr, buf_size);&#xA;    bd->bd_ptr  &#x2B;= buf_size;&#xA;    bd->bd_size -= buf_size;&#xA;    printf("End reading custom IO\n");&#xA;    return buf_size;&#xA;&#xA;}&#xA;&#xA;int64_t seekFunction(void* opaque, int64_t offset, int whence)&#xA;{&#xA;    std::cout &lt;&lt; "SEEK " &lt;&lt; std::endl;&#xA;    if (whence == AVSEEK_SIZE)&#xA;        return -1; // I don&#x27;t know "size of my handle in bytes"&#xA;}&#xA;&#xA;static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,&#xA;                   FILE *outfile)&#xA;{&#xA;    int ret;&#xA;&#xA;    /* send the frame to the encoder */&#xA;    if (frame)&#xA;        printf("Send frame %3" PRId64 "\n", frame->pts);&#xA;&#xA;    ret = avcodec_send_frame(enc_ctx, frame);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Error sending a frame for encoding\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    while (ret >= 0) {&#xA;        ret = avcodec_receive_packet(enc_ctx, pkt);&#xA;        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)&#xA;            return;&#xA;        else if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error during encoding\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        printf("Write packet %3" PRId64 " (size=%5d)\n", pkt->pts, pkt->size);&#xA;        fwrite(pkt->data, 1, pkt->size, outfile);&#xA;        av_packet_unref(pkt);&#xA;    }&#xA;}&#xA;&#xA;int main(int argc, char **argv)&#xA;{&#xA;&#xA;    av_log_set_level(AV_LOG_MAX_OFFSET);&#xA;&#xA;    AVFormatContext* pAVFormatContext;&#xA;    uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;&#xA;    size_t buffer_size, avio_ctx_buffer_size = 32768;&#xA;&#xA;    int ret = 0;&#xA;    char errbuf[100];&#xA;&#xA;    // Alloc format context&#xA;    //--------------------&#xA;    if ( !(pAVFormatContext = avformat_alloc_context()) )&#xA;    {&#xA;        ret = AVERROR(ENOMEM);&#xA;        std::cout &lt;&lt; "error=" &lt;&lt; ret &lt;&lt; " Fail to allocate avformat_alloc_context()\n";&#xA;        return false;&#xA;    }&#xA;&#xA;    // Copy from ffmpeg example - bd buffer&#xA;    //--------------------&#xA;    struct buffer_data bd = { 0, 0 };&#xA;&#xA;    // slurp file content into buffer&#xA;    ret = av_file_map("output_1920x1080_420p.yuv", &amp;buffer, &amp;buffer_size, 0, NULL);&#xA;    if (ret &lt; 0)&#xA;    {&#xA;        std::cout &lt;&lt; "error=" &lt;&lt; ret &lt;&lt; " Fail to allocate avformat_alloc_context()\n";&#xA;        return false;&#xA;    }&#xA;&#xA;    // fill opaque structure used by the AVIOContext read callback&#xA;    bd.bd_ptr  = buffer;&#xA;    bd.bd_size = buffer_size;&#xA;&#xA;    // Prepare AVIO context&#xA;    //--------------------&#xA;    avio_ctx_buffer = static_cast(av_malloc(avio_ctx_buffer_size &#x2B; AV_INPUT_BUFFER_PADDING_SIZE));&#xA;&#xA;    if (!avio_ctx_buffer) {&#xA;        ret = AVERROR(ENOMEM);&#xA;        std::cout &lt;&lt; "error=" &lt;&lt; ret &lt;&lt; " Fail to allocate av_malloc()\n";&#xA;        return false;&#xA;    }&#xA;&#xA;    AVIOContext* pAVIOContext = avio_alloc_context(avio_ctx_buffer,&#xA;                                                   avio_ctx_buffer_size,&#xA;                                                   0,&#xA;                                                   &amp;bd,&#xA;                                                   readFileBuffer,&#xA;                                                   NULL,&#xA;                                                   seekFunction);&#xA;&#xA;    if (!pAVIOContext) {&#xA;        ret = AVERROR(ENOMEM);&#xA;        std::cout &lt;&lt; "error=" &lt;&lt; ret &lt;&lt; " Fail to allocate avio_alloc_context()\n";&#xA;        return false;&#xA;    }&#xA;&#xA;    pAVFormatContext->pb = pAVIOContext;&#xA;    pAVFormatContext->flags = AVFMT_FLAG_CUSTOM_IO; // - NOT SURE ABOUT THIS FLAG&#xA;&#xA;    // Handle ffmpeg input&#xA;    //--------------------&#xA;    AVDictionary* options_in = NULL;&#xA;    //av_dict_set(&amp;options_in, "framerate", "30", 0);&#xA;    av_dict_set(&amp;options_in, "video_size", "1920x1080", 0);&#xA;    av_dict_set(&amp;options_in, "pixel_format", "yuv420p", 0);&#xA;    av_dict_set(&amp;options_in, "vcodec", "rawvideo", 0);&#xA;&#xA;    if ((ret = avformat_open_input(&amp;pAVFormatContext, "IO", NULL, &amp;options_in) &lt; 0) )&#xA;    {&#xA;        printf ("err=%d\n", ret);&#xA;        av_strerror(ret, errbuf, sizeof(errbuf));&#xA;        fprintf(stderr, "Unable to open err=%s\n", errbuf);&#xA;        return false;&#xA;    }&#xA;&#xA;    //Raw video doesn&#x27;t contain any stream information.&#xA;    //if ((ret = avformat_find_stream_info(pAVFormatContext, 0)) &lt; 0) {&#xA;    //    fprintf(stderr, "Failed to retrieve input stream information");&#xA;    //}&#xA;&#xA;    AVCodec* pAVCodec = avcodec_find_decoder(AV_CODEC_ID_RAWVIDEO); //Get pointer to rawvideo codec.&#xA;&#xA;    AVCodecContext* pAVCodecContext = avcodec_alloc_context3(pAVCodec); //Allocate codec context.&#xA;&#xA;    //Fill the codec context based on the values from the codec parameters.&#xA;    AVStream *vid_stream = pAVFormatContext->streams[0];&#xA;    avcodec_parameters_to_context(pAVCodecContext, vid_stream->codecpar);&#xA;&#xA;    avcodec_open2(pAVCodecContext, pAVCodec, NULL); //Open the codec&#xA;&#xA;    //Allocate memory for packet and frame&#xA;    AVPacket* pAVPacketIn = av_packet_alloc();&#xA;    AVFrame* pAVFrameOut = av_frame_alloc();&#xA;&#xA;    /* find the mpeg1video encoder */&#xA;    AVCodec* pAVCodecOut = avcodec_find_encoder_by_name("libx264");&#xA;    if (!pAVCodecOut) {&#xA;        fprintf(stderr, "Codec &#x27;%s&#x27; not found\n", "libx264");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    AVCodecContext* pAVCodecContextOut = avcodec_alloc_context3(pAVCodecOut);&#xA;    if (!pAVCodecContextOut) {&#xA;        fprintf(stderr, "Could not allocate video codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    AVPacket* pAVPacketOut = av_packet_alloc();&#xA;    if (!pAVPacketOut)&#xA;        exit(1);&#xA;&#xA;    /* put sample parameters */&#xA;    pAVCodecContextOut->bit_rate = 2000000;&#xA;    /* resolution must be a multiple of two */&#xA;    pAVCodecContextOut->width = 1280;&#xA;    pAVCodecContextOut->height = 720;&#xA;    /* frames per second */&#xA;    pAVCodecContextOut->time_base = (AVRational){1, 30};&#xA;    pAVCodecContextOut->framerate = (AVRational){30, 1};&#xA;&#xA;    /* emit one intra frame every ten frames&#xA;     * check frame pict_type before passing frame&#xA;     * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I&#xA;     * then gop_size is ignored and the output of encoder&#xA;     * will always be I frame irrespective to gop_size&#xA;     */&#xA;    pAVCodecContextOut->gop_size = 10;&#xA;    pAVCodecContextOut->max_b_frames = 0;&#xA;    pAVCodecContextOut->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;&#xA;    if (pAVCodecOut->id == AV_CODEC_ID_H264)&#xA;        av_opt_set(pAVCodecContextOut->priv_data, "preset", "slow", 0);&#xA;&#xA;    /* open it */&#xA;    ret = avcodec_open2(pAVCodecContextOut, pAVCodecOut, NULL);&#xA;    if (ret &lt; 0) {&#xA;        //fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    FILE* filename_out = fopen("out_vid_h264.mp4", "wb");&#xA;    if (!filename_out) {&#xA;        fprintf(stderr, "Could not open %s\n", "out_vid_h264.mp4");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    pAVFrameOut->format = pAVCodecContextOut->pix_fmt;&#xA;    pAVFrameOut->width  = pAVCodecContextOut->width;&#xA;    pAVFrameOut->height = pAVCodecContextOut->height;&#xA;&#xA;&#xA;    uint8_t endcode_suf[] = { 0, 0, 1, 0xb7 };&#xA;    int i = 0;&#xA;    //Read video frames and pass through the decoder.&#xA;    //Note: Since the video is rawvideo, we don&#x27;t really have to pass it through the decoder.&#xA;    while (av_read_frame(pAVFormatContext, pAVPacketIn) >= 0)&#xA;    {&#xA;        //The video is not encoded - passing through the decoder is simply copy the data.&#xA;        avcodec_send_packet(pAVCodecContext, pAVPacketIn);    //Supply raw packet data as input to the "decoder".&#xA;        avcodec_receive_frame(pAVCodecContext, pAVFrameOut);   //Return decoded output data from the "decoder".&#xA;&#xA;        fflush(stdout);&#xA;&#xA;        pAVFrameOut->pts = i&#x2B;&#x2B;;&#xA;&#xA;        /* encode the image */&#xA;        encode(pAVCodecContextOut, pAVFrameOut, pAVPacketOut, filename_out);&#xA;    }&#xA;&#xA;    /* flush the encoder */&#xA;    encode(pAVCodecContextOut, NULL, pAVPacketOut, filename_out);&#xA;&#xA;    /* Add sequence end code to have a real MPEG file.&#xA;       It makes only sense because this tiny examples writes packets&#xA;       directly. This is called "elementary stream" and only works for some&#xA;       codecs. To create a valid file, you usually need to write packets&#xA;       into a proper file format or protocol; see mux.c.&#xA;     */&#xA;    if (pAVCodecOut->id == AV_CODEC_ID_MPEG1VIDEO || pAVCodecOut->id == AV_CODEC_ID_MPEG2VIDEO)&#xA;        fwrite(endcode_suf, 1, sizeof(endcode_suf), filename_out);&#xA;    fclose(filename_out);&#xA;&#xA;&#xA;    return 0;&#xA;}&#xA;</iostream>

    &#xA;

    The output

    &#xA;

    ./ffmpeg_custom_io &#xA;Start reading custom IO&#xA;min buf size:32768&#xA;ptr:0x7f236a200000 size:933120000&#xA;End reading custom IO&#xA;Probing adp score:25 size:2048&#xA;Probing adp score:25 size:4096&#xA;Probing adp score:25 size:8192&#xA;Probing adp score:25 size:16384&#xA;Start reading custom IO&#xA;min buf size:32768&#xA;ptr:0x7f236a208000 size:933087232&#xA;End reading custom IO&#xA;Start reading custom IO&#xA;min buf size:65536&#xA;ptr:0x7f236a210000 size:933054464&#xA;End reading custom IO&#xA;Start reading custom IO&#xA;min buf size:131072&#xA;ptr:0x7f236a220000 size:932988928&#xA;End reading custom IO&#xA;Start reading custom IO&#xA;min buf size:262144&#xA;ptr:0x7f236a240000 size:932857856&#xA;End reading custom IO&#xA;Start reading custom IO&#xA;min buf size:524288&#xA;ptr:0x7f236a280000 size:932595712&#xA;End reading custom IO&#xA;err=1&#xA;Unable to open err=Error number 1 occurred&#xA;&#xA;

    &#xA;

    I tried all as noted above expecting avformat_open_input() will succeed, but it failed.

    &#xA;

  • ffmpeg (libav, libavfilter, etx) - modify frame with image or text using C\C++ API

    7 juillet 2023, par Alex

    After reading a huge bunch of docs and tutorials I still cant find a way to add some image or text to each frame of video. Something like logo on the frame corner, or text watermark.

    &#xA;

    Iam know how to do such things with ffmpeg from cli, but for this case, C\C++ code is required.

    &#xA;

    Looks like, ffmpeg's libav allow me to do some things with frame on decode stage, using AVFrame structure of current frame and add some modifications to it with libavfilter. But how exactly this can be done ?

    &#xA;