Recherche avancée

Médias (91)

Autres articles (98)

  • MediaSPIP 0.1 Beta version

    25 avril 2011, par

    MediaSPIP 0.1 beta is the first version of MediaSPIP proclaimed as "usable".
    The zip file provided here only contains the sources of MediaSPIP in its standalone version.
    To get a working installation, you must manually install all-software dependencies on the server.
    If you want to use this archive for an installation in "farm mode", you will also need to proceed to other manual (...)

  • Multilang : améliorer l’interface pour les blocs multilingues

    18 février 2011, par

    Multilang est un plugin supplémentaire qui n’est pas activé par défaut lors de l’initialisation de MediaSPIP.
    Après son activation, une préconfiguration est mise en place automatiquement par MediaSPIP init permettant à la nouvelle fonctionnalité d’être automatiquement opérationnelle. Il n’est donc pas obligatoire de passer par une étape de configuration pour cela.

  • Amélioration de la version de base

    13 septembre 2013

    Jolie sélection multiple
    Le plugin Chosen permet d’améliorer l’ergonomie des champs de sélection multiple. Voir les deux images suivantes pour comparer.
    Il suffit pour cela d’activer le plugin Chosen (Configuration générale du site > Gestion des plugins), puis de configurer le plugin (Les squelettes > Chosen) en activant l’utilisation de Chosen dans le site public et en spécifiant les éléments de formulaires à améliorer, par exemple select[multiple] pour les listes à sélection multiple (...)

Sur d’autres sites (11990)

  • how to extract audio from video using ffmpeg c++

    29 septembre 2020, par daenerys

    I am trying to write c++ code on how to extract audio from mp4 format file. I have compiled the examples in ffmpeg library and tried to run the demuxing_decoding.c file. The problem is that on running the code, it starts to decode way more than the actual file size and decodes wrong codec format (the decoded files cant be run).

    



    here is the demuxing_decoding.c that I am trying to run :

    



    #include &#xA;&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libavutil></libavutil>channel_layout.h>&#xA;#include <libavutil></libavutil>common.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;#include <libavutil></libavutil>mathematics.h>&#xA;#include <libavutil></libavutil>samplefmt.h>&#xA;&#xA;#define INBUF_SIZE 4096&#xA;#define AUDIO_INBUF_SIZE 20480&#xA;#define AUDIO_REFILL_THRESH 4096&#xA;&#xA;/* check that a given sample format is supported by the encoder */&#xA;static int check_sample_fmt(AVCodec *codec, enum AVSampleFormat sample_fmt)&#xA;{&#xA;    const enum AVSampleFormat *p = codec->sample_fmts;&#xA;&#xA;    while (*p != AV_SAMPLE_FMT_NONE) {&#xA;        if (*p == sample_fmt)&#xA;            return 1;&#xA;        p&#x2B;&#x2B;;&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;/* just pick the highest supported samplerate */&#xA;static int select_sample_rate(AVCodec *codec)&#xA;{&#xA;    const int *p;&#xA;    int best_samplerate = 0;&#xA;&#xA;    if (!codec->supported_samplerates)&#xA;        return 44100;&#xA;&#xA;    p = codec->supported_samplerates;&#xA;    while (*p) {&#xA;        best_samplerate = FFMAX(*p, best_samplerate);&#xA;        p&#x2B;&#x2B;;&#xA;    }&#xA;    return best_samplerate;&#xA;}&#xA;&#xA;/* select layout with the highest channel count */&#xA;static int select_channel_layout(AVCodec *codec)&#xA;{&#xA;    const uint64_t *p;&#xA;    uint64_t best_ch_layout = 0;&#xA;    int best_nb_channels   = 0;&#xA;&#xA;    if (!codec->channel_layouts)&#xA;        return AV_CH_LAYOUT_STEREO;&#xA;&#xA;    p = codec->channel_layouts;&#xA;    while (*p) {&#xA;        int nb_channels = av_get_channel_layout_nb_channels(*p);&#xA;&#xA;        if (nb_channels > best_nb_channels) {&#xA;            best_ch_layout    = *p;&#xA;            best_nb_channels = nb_channels;&#xA;        }&#xA;        p&#x2B;&#x2B;;&#xA;    }&#xA;    return best_ch_layout;&#xA;}&#xA;&#xA;/*&#xA; * Audio encoding example&#xA; */&#xA;static void audio_encode_example(const char *filename)&#xA;{&#xA;    AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    AVFrame *frame;&#xA;    AVPacket pkt;&#xA;    int i, j, k, ret, got_output;&#xA;    int buffer_size;&#xA;    FILE *f;&#xA;    uint16_t *samples;&#xA;    float t, tincr;&#xA;&#xA;    printf("Encode audio file %s\n", filename);&#xA;&#xA;    /* find the MP2 encoder */&#xA;    codec = avcodec_find_encoder(AV_CODEC_ID_MP2);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    c = avcodec_alloc_context3(codec);&#xA;    if (!c) {&#xA;        fprintf(stderr, "Could not allocate audio codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* put sample parameters */&#xA;    c->bit_rate = 64000;&#xA;&#xA;    /* check that the encoder supports s16 pcm input */&#xA;    c->sample_fmt = AV_SAMPLE_FMT_S16;&#xA;    if (!check_sample_fmt(codec, c->sample_fmt)) {&#xA;        fprintf(stderr, "Encoder does not support sample format %s",&#xA;                av_get_sample_fmt_name(c->sample_fmt));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* select other audio parameters supported by the encoder */&#xA;    c->sample_rate    = select_sample_rate(codec);&#xA;    c->channel_layout = select_channel_layout(codec);&#xA;    c->channels       = av_get_channel_layout_nb_channels(c->channel_layout);&#xA;&#xA;    /* open it */&#xA;    if (avcodec_open2(c, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    f = fopen(filename, "wb");&#xA;    if (!f) {&#xA;        fprintf(stderr, "Could not open %s\n", filename);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* frame containing input raw audio */&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        fprintf(stderr, "Could not allocate audio frame\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame->nb_samples     = c->frame_size;&#xA;    frame->format         = c->sample_fmt;&#xA;    frame->channel_layout = c->channel_layout;&#xA;&#xA;    /* the codec gives us the frame size, in samples,&#xA;     * we calculate the size of the samples buffer in bytes */&#xA;    buffer_size = av_samples_get_buffer_size(NULL, c->channels, c->frame_size,&#xA;                                             c->sample_fmt, 0);&#xA;    if (buffer_size &lt; 0) {&#xA;        fprintf(stderr, "Could not get sample buffer size\n");&#xA;        exit(1);&#xA;    }&#xA;    samples = av_malloc(buffer_size);&#xA;    if (!samples) {&#xA;        fprintf(stderr, "Could not allocate %d bytes for samples buffer\n",&#xA;                buffer_size);&#xA;        exit(1);&#xA;    }&#xA;    /* setup the data pointers in the AVFrame */&#xA;    ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,&#xA;                                   (const uint8_t*)samples, buffer_size, 0);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not setup audio frame\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* encode a single tone sound */&#xA;    t = 0;&#xA;    tincr = 2 * M_PI * 440.0 / c->sample_rate;&#xA;    for (i = 0; i &lt; 200; i&#x2B;&#x2B;) {&#xA;        av_init_packet(&amp;pkt);&#xA;        pkt.data = NULL; // packet data will be allocated by the encoder&#xA;        pkt.size = 0;&#xA;&#xA;        for (j = 0; j &lt; c->frame_size; j&#x2B;&#x2B;) {&#xA;            samples[2*j] = (int)(sin(t) * 10000);&#xA;&#xA;            for (k = 1; k &lt; c->channels; k&#x2B;&#x2B;)&#xA;                samples[2*j &#x2B; k] = samples[2*j];&#xA;            t &#x2B;= tincr;&#xA;        }&#xA;        /* encode the samples */&#xA;        ret = avcodec_encode_audio2(c, &amp;pkt, frame, &amp;got_output);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error encoding audio frame\n");&#xA;            exit(1);&#xA;        }&#xA;        if (got_output) {&#xA;            fwrite(pkt.data, 1, pkt.size, f);&#xA;            av_free_packet(&amp;pkt);&#xA;        }&#xA;    }&#xA;&#xA;    /* get the delayed frames */&#xA;    for (got_output = 1; got_output; i&#x2B;&#x2B;) {&#xA;        ret = avcodec_encode_audio2(c, &amp;pkt, NULL, &amp;got_output);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error encoding frame\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        if (got_output) {&#xA;            fwrite(pkt.data, 1, pkt.size, f);&#xA;            av_free_packet(&amp;pkt);&#xA;        }&#xA;    }&#xA;    fclose(f);&#xA;&#xA;    av_freep(&amp;samples);&#xA;    av_frame_free(&amp;frame);&#xA;    avcodec_close(c);&#xA;    av_free(c);&#xA;}&#xA;&#xA;/*&#xA; * Audio decoding.&#xA; */&#xA;static void audio_decode_example(const char *outfilename, const char *filename)&#xA;{&#xA;    AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    int len;&#xA;    FILE *f, *outfile;&#xA;    uint8_t inbuf[AUDIO_INBUF_SIZE &#x2B; AV_INPUT_BUFFER_PADDING_SIZE];&#xA;    AVPacket avpkt;&#xA;    AVFrame *decoded_frame = NULL;&#xA;&#xA;    av_init_packet(&amp;avpkt);&#xA;&#xA;    printf("Decode audio file %s to %s\n", filename, outfilename);&#xA;&#xA;    /* find the mpeg audio decoder */&#xA;    codec = avcodec_find_decoder(AV_CODEC_ID_MP2);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    c = avcodec_alloc_context3(codec);&#xA;    if (!c) {&#xA;        fprintf(stderr, "Could not allocate audio codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* open it */&#xA;    if (avcodec_open2(c, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    f = fopen(filename, "rb");&#xA;    if (!f) {&#xA;        fprintf(stderr, "Could not open %s\n", filename);&#xA;        exit(1);&#xA;    }&#xA;    outfile = fopen(outfilename, "wb");&#xA;    if (!outfile) {&#xA;        av_free(c);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* decode until eof */&#xA;    avpkt.data = inbuf;&#xA;    avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);&#xA;&#xA;    while (avpkt.size > 0) {&#xA;        int i, ch;&#xA;        int got_frame = 0;&#xA;&#xA;        if (!decoded_frame) {&#xA;            if (!(decoded_frame = av_frame_alloc())) {&#xA;                fprintf(stderr, "Could not allocate audio frame\n");&#xA;                exit(1);&#xA;            }&#xA;        }&#xA;&#xA;        len = avcodec_decode_audio4(c, decoded_frame, &amp;got_frame, &amp;avpkt);&#xA;        if (len &lt; 0) {&#xA;            fprintf(stderr, "Error while decoding\n");&#xA;            exit(1);&#xA;        }&#xA;        if (got_frame) {&#xA;            /* if a frame has been decoded, output it */&#xA;            int data_size = av_get_bytes_per_sample(c->sample_fmt);&#xA;            if (data_size &lt; 0) {&#xA;                /* This should not occur, checking just for paranoia */&#xA;                fprintf(stderr, "Failed to calculate data size\n");&#xA;                exit(1);&#xA;            }&#xA;            for (i=0; inb_samples; i&#x2B;&#x2B;)&#xA;                for (ch=0; chchannels; ch&#x2B;&#x2B;)&#xA;                    fwrite(decoded_frame->data[ch] &#x2B; data_size*i, 1, data_size, outfile);&#xA;        }&#xA;        avpkt.size -= len;&#xA;        avpkt.data &#x2B;= len;&#xA;        avpkt.dts =&#xA;        avpkt.pts = AV_NOPTS_VALUE;&#xA;        if (avpkt.size &lt; AUDIO_REFILL_THRESH) {&#xA;            /* Refill the input buffer, to avoid trying to decode&#xA;             * incomplete frames. Instead of this, one could also use&#xA;             * a parser, or use a proper container format through&#xA;             * libavformat. */&#xA;            memmove(inbuf, avpkt.data, avpkt.size);&#xA;            avpkt.data = inbuf;&#xA;            len = fread(avpkt.data &#x2B; avpkt.size, 1,&#xA;                        AUDIO_INBUF_SIZE - avpkt.size, f);&#xA;            if (len > 0)&#xA;                avpkt.size &#x2B;= len;&#xA;        }&#xA;    }&#xA;&#xA;    fclose(outfile);&#xA;    fclose(f);&#xA;&#xA;    avcodec_close(c);&#xA;    av_free(c);&#xA;    av_frame_free(&amp;decoded_frame);&#xA;}&#xA;&#xA;/*&#xA; * Video encoding example&#xA; */&#xA;static void video_encode_example(const char *filename, int codec_id)&#xA;{&#xA;    AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    int i, ret, x, y, got_output;&#xA;    FILE *f;&#xA;    AVFrame *frame;&#xA;    AVPacket pkt;&#xA;    uint8_t endcode[] = { 0, 0, 1, 0xb7 };&#xA;&#xA;    printf("Encode video file %s\n", filename);&#xA;&#xA;    /* find the mpeg1 video encoder */&#xA;    codec = avcodec_find_encoder(codec_id);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    c = avcodec_alloc_context3(codec);&#xA;    if (!c) {&#xA;        fprintf(stderr, "Could not allocate video codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* put sample parameters */&#xA;    c->bit_rate = 400000;&#xA;    /* resolution must be a multiple of two */&#xA;    c->width = 352;&#xA;    c->height = 288;&#xA;    /* frames per second */&#xA;    c->time_base = (AVRational){1,25};&#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;    c->gop_size = 10;&#xA;    c->max_b_frames = 1;&#xA;    c->pix_fmt = AV_PIX_FMT_YUV420P;&#xA;&#xA;    if (codec_id == AV_CODEC_ID_H264)&#xA;        av_opt_set(c->priv_data, "preset", "slow", 0);&#xA;&#xA;    /* open it */&#xA;    if (avcodec_open2(c, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    f = fopen(filename, "wb");&#xA;    if (!f) {&#xA;        fprintf(stderr, "Could not open %s\n", filename);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        fprintf(stderr, "Could not allocate video frame\n");&#xA;        exit(1);&#xA;    }&#xA;    frame->format = c->pix_fmt;&#xA;    frame->width  = c->width;&#xA;    frame->height = c->height;&#xA;&#xA;    /* the image can be allocated by any means and av_image_alloc() is&#xA;     * just the most convenient way if av_malloc() is to be used */&#xA;    ret = av_image_alloc(frame->data, frame->linesize, c->width, c->height,&#xA;                         c->pix_fmt, 32);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate raw picture buffer\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* encode 1 second of video */&#xA;    for (i = 0; i &lt; 25; i&#x2B;&#x2B;) {&#xA;        av_init_packet(&amp;pkt);&#xA;        pkt.data = NULL;    // packet data will be allocated by the encoder&#xA;        pkt.size = 0;&#xA;&#xA;        fflush(stdout);&#xA;        /* prepare a dummy image */&#xA;        /* Y */&#xA;        for (y = 0; y &lt; c->height; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; c->width; x&#x2B;&#x2B;) {&#xA;                frame->data[0][y * frame->linesize[0] &#x2B; x] = x &#x2B; y &#x2B; i * 3;&#xA;            }&#xA;        }&#xA;&#xA;        /* Cb and Cr */&#xA;        for (y = 0; y &lt; c->height/2; y&#x2B;&#x2B;) {&#xA;            for (x = 0; x &lt; c->width/2; x&#x2B;&#x2B;) {&#xA;                frame->data[1][y * frame->linesize[1] &#x2B; x] = 128 &#x2B; y &#x2B; i * 2;&#xA;                frame->data[2][y * frame->linesize[2] &#x2B; x] = 64 &#x2B; x &#x2B; i * 5;&#xA;            }&#xA;        }&#xA;&#xA;        frame->pts = i;&#xA;&#xA;        /* encode the image */&#xA;        ret = avcodec_encode_video2(c, &amp;pkt, frame, &amp;got_output);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error encoding frame\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        if (got_output) {&#xA;            printf("Write frame %3d (size=%5d)\n", i, pkt.size);&#xA;            fwrite(pkt.data, 1, pkt.size, f);&#xA;            av_free_packet(&amp;pkt);&#xA;        }&#xA;    }&#xA;&#xA;    /* get the delayed frames */&#xA;    for (got_output = 1; got_output; i&#x2B;&#x2B;) {&#xA;        fflush(stdout);&#xA;&#xA;        ret = avcodec_encode_video2(c, &amp;pkt, NULL, &amp;got_output);&#xA;        if (ret &lt; 0) {&#xA;            fprintf(stderr, "Error encoding frame\n");&#xA;            exit(1);&#xA;        }&#xA;&#xA;        if (got_output) {&#xA;            printf("Write frame %3d (size=%5d)\n", i, pkt.size);&#xA;            fwrite(pkt.data, 1, pkt.size, f);&#xA;            av_free_packet(&amp;pkt);&#xA;        }&#xA;    }&#xA;&#xA;    /* add sequence end code to have a real mpeg file */&#xA;    fwrite(endcode, 1, sizeof(endcode), f);&#xA;    fclose(f);&#xA;&#xA;    avcodec_close(c);&#xA;    av_free(c);&#xA;    av_freep(&amp;frame->data[0]);&#xA;    av_frame_free(&amp;frame);&#xA;    printf("\n");&#xA;}&#xA;&#xA;/*&#xA; * Video decoding example&#xA; */&#xA;&#xA;static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,&#xA;                     char *filename)&#xA;{&#xA;    FILE *f;&#xA;    int i;&#xA;&#xA;    f = fopen(filename,"w");&#xA;    fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);&#xA;    for (i = 0; i &lt; ysize; i&#x2B;&#x2B;)&#xA;        fwrite(buf &#x2B; i * wrap, 1, xsize, f);&#xA;    fclose(f);&#xA;}&#xA;&#xA;static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,&#xA;                              AVFrame *frame, int *frame_count, AVPacket *pkt, int last)&#xA;{&#xA;    int len, got_frame;&#xA;    char buf[1024];&#xA;&#xA;    len = avcodec_decode_video2(avctx, frame, &amp;got_frame, pkt);&#xA;    if (len &lt; 0) {&#xA;        fprintf(stderr, "Error while decoding frame %d\n", *frame_count);&#xA;        return len;&#xA;    }&#xA;    if (got_frame) {&#xA;        printf("Saving %sframe %3d\n", last ? "last " : "", *frame_count);&#xA;        fflush(stdout);&#xA;&#xA;        /* the picture is allocated by the decoder, no need to free it */&#xA;        snprintf(buf, sizeof(buf), outfilename, *frame_count);&#xA;        pgm_save(frame->data[0], frame->linesize[0],&#xA;                 frame->width, frame->height, buf);&#xA;        (*frame_count)&#x2B;&#x2B;;&#xA;    }&#xA;    if (pkt->data) {&#xA;        pkt->size -= len;&#xA;        pkt->data &#x2B;= len;&#xA;    }&#xA;    return 0;&#xA;}&#xA;&#xA;static void video_decode_example(const char *outfilename, const char *filename)&#xA;{&#xA;    AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    int frame_count;&#xA;    FILE *f;&#xA;    AVFrame *frame;&#xA;    uint8_t inbuf[INBUF_SIZE &#x2B; AV_INPUT_BUFFER_PADDING_SIZE];&#xA;    AVPacket avpkt;&#xA;&#xA;    av_init_packet(&amp;avpkt);&#xA;&#xA;    /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */&#xA;    memset(inbuf &#x2B; INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);&#xA;&#xA;    printf("Decode video file %s to %s\n", filename, outfilename);&#xA;&#xA;    /* find the mpeg1 video decoder */&#xA;    codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec not found\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    c = avcodec_alloc_context3(codec);&#xA;    if (!c) {&#xA;        fprintf(stderr, "Could not allocate video codec context\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    if (codec->capabilities &amp; AV_CODEC_CAP_TRUNCATED)&#xA;        c->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames&#xA;&#xA;    /* For some codecs, such as msmpeg4 and mpeg4, width and height&#xA;       MUST be initialized there because this information is not&#xA;       available in the bitstream. */&#xA;&#xA;    /* open it */&#xA;    if (avcodec_open2(c, codec, NULL) &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    f = fopen(filename, "rb");&#xA;    if (!f) {&#xA;        fprintf(stderr, "Could not open %s\n", filename);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame = av_frame_alloc();&#xA;    if (!frame) {&#xA;        fprintf(stderr, "Could not allocate video frame\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame_count = 0;&#xA;    for (;;) {&#xA;        avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);&#xA;        if (avpkt.size == 0)&#xA;            break;&#xA;&#xA;        /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)&#xA;           and this is the only method to use them because you cannot&#xA;           know the compressed data size before analysing it.&#xA;&#xA;           BUT some other codecs (msmpeg4, mpeg4) are inherently frame&#xA;           based, so you must call them with all the data for one&#xA;           frame exactly. You must also initialize &#x27;width&#x27; and&#xA;           &#x27;height&#x27; before initializing them. */&#xA;&#xA;        /* NOTE2: some codecs allow the raw parameters (frame size,&#xA;           sample rate) to be changed at any frame. We handle this, so&#xA;           you should also take care of it */&#xA;&#xA;        /* here, we use a stream based decoder (mpeg1video), so we&#xA;           feed decoder and see if it could decode a frame */&#xA;        avpkt.data = inbuf;&#xA;        while (avpkt.size > 0)&#xA;            if (decode_write_frame(outfilename, c, frame, &amp;frame_count, &amp;avpkt, 0) &lt; 0)&#xA;                exit(1);&#xA;    }&#xA;&#xA;    /* some codecs, such as MPEG, transmit the I and P frame with a&#xA;       latency of one frame. You must do the following to have a&#xA;       chance to get the last frame of the video */&#xA;    avpkt.data = NULL;&#xA;    avpkt.size = 0;&#xA;    decode_write_frame(outfilename, c, frame, &amp;frame_count, &amp;avpkt, 1);&#xA;&#xA;    fclose(f);&#xA;&#xA;    avcodec_close(c);&#xA;    av_free(c);&#xA;    av_frame_free(&amp;frame);&#xA;    printf("\n");&#xA;}&#xA;&#xA;int main(int argc, char **argv)&#xA;{&#xA;    const char *output_type;&#xA;&#xA;    /* register all the codecs */&#xA;    avcodec_register_all();&#xA;&#xA;    if (argc &lt; 2) {&#xA;        printf("usage: %s output_type\n"&#xA;               "API example program to decode/encode a media stream with libavcodec.\n"&#xA;               "This program generates a synthetic stream and encodes it to a file\n"&#xA;               "named test.h264, test.mp2 or test.mpg depending on output_type.\n"&#xA;               "The encoded stream is then decoded and written to a raw data output.\n"&#xA;               "output_type must be chosen between &#x27;h264&#x27;, &#x27;mp2&#x27;, &#x27;mpg&#x27;.\n",&#xA;               argv[0]);&#xA;        return 1;&#xA;    }&#xA;    output_type = argv[1];&#xA;&#xA;    if (!strcmp(output_type, "h264")) {&#xA;        video_encode_example("test.h264", AV_CODEC_ID_H264);&#xA;    } else if (!strcmp(output_type, "mp2")) {&#xA;        audio_encode_example("test.mp2");&#xA;        audio_decode_example("test.pcm", "test.mp2");&#xA;    } else if (!strcmp(output_type, "mpg")) {&#xA;        video_encode_example("test.mpg", AV_CODEC_ID_MPEG1VIDEO);&#xA;        video_decode_example("test%02d.pgm", "test.mpg");&#xA;    } else {&#xA;        fprintf(stderr, "Invalid output type &#x27;%s&#x27;, choose between &#x27;h264&#x27;, &#x27;mp2&#x27;, or &#x27;mpg&#x27;\n",&#xA;                output_type);&#xA;        return 1;&#xA;    }&#xA;&#xA;    return 0;&#xA;}&#xA;

    &#xA;&#xA;

    What could be wrong ?&#xA;The command I used is :

    &#xA;&#xA;

    ./demuxing_decoding /home/cortana/Burn.mp4 /home/cortana/Desktop/Burn.mp4 /home/cortana/Desktop/Burn.aac&#xA;

    &#xA;

  • FFMPEG API Mp4 H264 Encoding/Muxing - unspecified pixel format

    28 juillet 2020, par Fabrice

    I'm working on a c++ project using ffmpeg. I have to generate an mp4 file with h264 encoding.

    &#xA;

    My problem is that the file generate but when reading the file with VLC I've got no image, and analyzing it with ffprobe give me (log below) the error :

    &#xA;

    &#xA;

    unspecified pixel format

    &#xA;

    &#xA;

    ffprobe version N-93020-g3224d6691c Copyright (c) 2007-2019 the FFmpeg developers&#xA;  built with gcc 8.2.1 (GCC) 20181201&#xA;  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt&#xA;  libavutil      56. 26.100 / 56. 26.100&#xA;  libavcodec     58. 44.100 / 58. 44.100&#xA;  libavformat    58. 26.100 / 58. 26.100&#xA;  libavdevice    58.  6.101 / 58.  6.101&#xA;  libavfilter     7. 48.100 /  7. 48.100&#xA;  libswscale      5.  4.100 /  5.  4.100&#xA;  libswresample   3.  4.100 /  3.  4.100&#xA;  libpostproc    55.  4.100 / 55.  4.100&#xA;[h264 @ 02a46240] non-existing PPS 0 referenced&#xA;[h264 @ 02a46240] decode_slice_header error&#xA;[h264 @ 02a46240] no frame!&#xA;...&#xA;[h264 @ 02a46240] non-existing PPS 0 referenced&#xA;[h264 @ 02a46240] decode_slice_header error&#xA;[h264 @ 02a46240] no frame!&#xA;[mov,mp4,m4a,3gp,3g2,mj2 @ 02a35380] decoding for stream 0 failed&#xA;[mov,mp4,m4a,3gp,3g2,mj2 @ 02a35380] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 352x288, 320 kb/s): unspecified pixel format&#xA;Consider increasing the value for the &#x27;analyzeduration&#x27; and &#x27;probesize&#x27; options&#xA;Input #0, mov,mp4,m4a,3gp,3g2,mj2, from &#x27;C:\Users\Fabrice\Desktop\video\Test.mp4&#x27;:&#xA;  Metadata:&#xA;    major_brand     : isom&#xA;    minor_version   : 512&#xA;    compatible_brands: isomiso2avc1mp41&#xA;    encoder         : Lavf58.26.100&#xA;  Duration: 00:00:09.00, start: 0.000000, bitrate: 323 kb/s&#xA;    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 352x288, 320 kb/s, 25.11 fps, 25 tbr, 12800 tbn, 25600 tbc (default)&#xA;    Metadata:&#xA;      handler_name    : VideoHandler&#xA;

    &#xA;

    Here is the code I use to genererate my mp4 File, it's based on sample from ffmpeg (see : FFMPEG Muxing sample). I have tried to adapt it without using deprecated function. It works using webm/vp8 encoding, but not mp4/h264.

    &#xA;

    #include &#xA;#include &#xA;#include &#xA;extern "C"&#xA;{&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include <libswscale></libswscale>swscale.h>&#xA;#include <libavformat></libavformat>avformat.h>&#xA;#include <libavutil></libavutil>error.h> &#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#xA;}&#xA;&#xA;#pragma comment(lib, "avcodec.lib")&#xA;#pragma comment(lib, "swscale.lib")&#xA;#pragma comment(lib, "avformat.lib")&#xA;#pragma comment(lib, "avutil.lib")&#xA;&#xA;/* 10 seconds stream duration */&#xA;#define STREAM_DURATION   10.0&#xA;#define STREAM_FRAME_RATE 25 /* 25 images/s */&#xA;#define STREAM_NB_FRAMES  ((int)(STREAM_DURATION * STREAM_FRAME_RATE))&#xA;#define STREAM_PIX_FMT    AV_PIX_FMT_YUV420P /* default pix_fmt */&#xA;&#xA;//#pragma warning(disable : 4996) // TODO: remove&#xA;&#xA;static int sws_flags = SWS_BICUBIC;&#xA;&#xA;/* Add an output stream. */&#xA;static AVStream *add_stream(AVFormatContext *formatContext, AVCodec **codec, enum AVCodecID codecId, AVCodecContext **codecCtx)&#xA;{&#xA;    AVStream *stream;&#xA;&#xA;    // Get the encoder codec&#xA;    *codec = avcodec_find_encoder(codecId);&#xA;    if (!(*codec)) {&#xA;        fprintf(stderr, "Could not find encoder for &#x27;%s&#x27;\n",&#xA;            avcodec_get_name(codecId));&#xA;        exit(1);&#xA;    }&#xA;&#xA;    // Get the stream for codec&#xA;    stream = avformat_new_stream(formatContext, *codec);&#xA;    if (!stream) {&#xA;        fprintf(stderr, "Could not allocate stream\n");&#xA;        exit(1);&#xA;    }&#xA;    stream->id = formatContext->nb_streams - 1;&#xA;&#xA;    (*codecCtx) = avcodec_alloc_context3(*codec);&#xA;&#xA;    switch ((*codec)->type) {&#xA;    case AVMEDIA_TYPE_VIDEO:&#xA;        stream->codecpar->codec_id = codecId;&#xA;        stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;&#xA;        stream->codecpar->bit_rate = 400000;&#xA;        stream->codecpar->width = 352;&#xA;        stream->codecpar->height = 288;&#xA;        stream->codecpar->format = STREAM_PIX_FMT;&#xA;        stream->time_base = { 1, STREAM_FRAME_RATE };&#xA;&#xA;        avcodec_parameters_to_context((*codecCtx), stream->codecpar);&#xA;        (*codecCtx)->gop_size = 12; /* emit one intra frame every twelve frames at most */&#xA;        (*codecCtx)->max_b_frames = 2;&#xA;        (*codecCtx)->time_base = { 1, STREAM_FRAME_RATE };&#xA;        if ((*codecCtx)->codec_id == AV_CODEC_ID_MPEG1VIDEO) {&#xA;            /* Needed to avoid using macroblocks in which some coeffs overflow.&#xA;             * This does not happen with normal video, it just happens here as&#xA;             * the motion of the chroma plane does not match the luma plane. */&#xA;            (*codecCtx)->mb_decision = 2;&#xA;        }&#xA;        break;&#xA;&#xA;    default:&#xA;        break;&#xA;    }&#xA;    &#xA;    //if (stream->codecpar->codec_id == AV_CODEC_ID_H264) {&#xA;    //  av_opt_set(codecCtx, "preset", "ultrafast", 0);&#xA;    //}&#xA;    //(*codecCtx)->flags |= AV_CODEC_FLAG_LOW_DELAY;&#xA;&#xA;    /* Some formats want stream headers to be separate. */&#xA;    if (formatContext->oformat->flags &amp; AVFMT_GLOBALHEADER)&#xA;        (*codecCtx)->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;&#xA;&#xA;&#xA;    int ret = avcodec_parameters_from_context(stream->codecpar, (*codecCtx));&#xA;    if (ret &lt; 0) {&#xA;        char error[255];&#xA;        av_strerror(ret, error, 255);&#xA;        fprintf(stderr, "avcodec_parameters_from_context returned (%d) - %s", ret, error);&#xA;        return false;&#xA;    }&#xA;&#xA;    return stream;&#xA;}&#xA;&#xA;/**************************************************************/&#xA;/* video output */&#xA;&#xA;static AVFrame *frame_video;&#xA;static int frame_count;&#xA;&#xA;static void open_video(AVCodec *codec, AVStream *stream, AVCodecContext *codecCtx)&#xA;{&#xA;    int ret;&#xA;&#xA;    /* open the codec */&#xA;    ret = avcodec_open2(codecCtx, codec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        char error[255];&#xA;        av_strerror(ret, error, 255);&#xA;        fprintf(stderr, "Could not open video codec: %s\n", error);&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* allocate and init a re-usable frame */&#xA;    frame_video = av_frame_alloc();&#xA;    if (!frame_video) {&#xA;        fprintf(stderr, "Could not allocate video frame\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    frame_video->format = codecCtx->pix_fmt;&#xA;    frame_video->width = codecCtx->width;&#xA;    frame_video->height = codecCtx->height;&#xA;&#xA;    ret = av_frame_get_buffer(frame_video, 32);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate the video frame data\n");&#xA;        exit(1);&#xA;    }&#xA;}&#xA;&#xA;/* Prepare a dummy image. */&#xA;static void fill_yuv_image(AVFrame *pict, int frame_index, int width, int height)&#xA;{&#xA;    int x, y, i;&#xA;&#xA;    i = frame_index;&#xA;&#xA;    /* Y */&#xA;    for (y = 0; y &lt; height; y&#x2B;&#x2B;)&#xA;        for (x = 0; x &lt; width; x&#x2B;&#x2B;)&#xA;            pict->data[0][y * pict->linesize[0] &#x2B; x] = x &#x2B; y &#x2B; i * 3;&#xA;&#xA;    /* Cb and Cr */&#xA;    for (y = 0; y &lt; height / 2; y&#x2B;&#x2B;) {&#xA;        for (x = 0; x &lt; width / 2; x&#x2B;&#x2B;) {&#xA;            pict->data[1][y * pict->linesize[1] &#x2B; x] = 128 &#x2B; y &#x2B; i * 2;&#xA;            pict->data[2][y * pict->linesize[2] &#x2B; x] = 64 &#x2B; x &#x2B; i * 5;&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;int timestamp = 0;&#xA;static void write_video_frame(AVFormatContext *formatContext, AVStream *stream, AVCodecContext *codecCtx)&#xA;{&#xA;    int ret;&#xA;    static struct SwsContext *sws_ctx;&#xA;&#xA;    if (frame_count >= STREAM_NB_FRAMES) {&#xA;        /* No more frames to compress. The codec has a latency of a few&#xA;         * frames if using B-frames, so we get the last frames by&#xA;         * passing the same picture again. */&#xA;    }&#xA;    else {&#xA;        if (codecCtx->pix_fmt != AV_PIX_FMT_YUV420P) {&#xA;            /* as we only generate a YUV420P picture, we must convert it&#xA;             * to the codec pixel format if needed */&#xA;            if (!sws_ctx) {&#xA;                sws_ctx = sws_getContext(codecCtx->width, codecCtx->height, AV_PIX_FMT_YUV420P,&#xA;                    codecCtx->width, codecCtx->height, codecCtx->pix_fmt,&#xA;                    sws_flags, NULL, NULL, NULL);&#xA;                if (!sws_ctx) {&#xA;                    fprintf(stderr, "Could not initialize the conversion context\n");&#xA;                    exit(1);&#xA;                }&#xA;            }&#xA;            fill_yuv_image(frame_video, frame_count, codecCtx->width, codecCtx->height);&#xA;            sws_scale(sws_ctx, (const uint8_t * const *)frame_video->data, frame_video->linesize,&#xA;                0, codecCtx->height, frame_video->data, frame_video->linesize);&#xA;        }&#xA;        else {&#xA;            fill_yuv_image(frame_video, frame_count, codecCtx->width, codecCtx->height);&#xA;        }&#xA;    }&#xA;&#xA;    frame_video->format = AV_PIX_FMT_YUV420P;&#xA;    frame_video->width = codecCtx->width;&#xA;    frame_video->height = codecCtx->height;&#xA;&#xA;    if (formatContext->oformat->flags &amp; 0x0020) {&#xA;        /* Raw video case - directly store the picture in the packet */&#xA;        AVPacket pkt;&#xA;        av_init_packet(&amp;pkt);&#xA;&#xA;        pkt.flags |= AV_PKT_FLAG_KEY;&#xA;        pkt.stream_index = stream->index;&#xA;        pkt.data = frame_video->data[0];&#xA;        pkt.size = sizeof(AVPicture);&#xA;&#xA;        ret = av_interleaved_write_frame(formatContext, &amp;pkt);&#xA;    }&#xA;    else {&#xA;        AVPacket pkt = { 0 };&#xA;        av_init_packet(&amp;pkt);&#xA;&#xA;        /* encode the image */&#xA;        fprintf(stderr, "\nFrame type : %c\n", av_get_picture_type_char(frame_video->pict_type));&#xA;        fprintf(stderr, "Frame pts: %lld, \n", frame_video->pts);&#xA;        fprintf(stderr, "Codec timebase: %d/%d\n", codecCtx->time_base.num, codecCtx->time_base.den);&#xA;        fprintf(stderr, "Stream timebase: %d/%d\n", stream->time_base.num, stream->time_base.den);&#xA;        fprintf(stderr, "Resacale: %lld, \n\n", av_rescale_q(1, codecCtx->time_base, stream->time_base));&#xA;        ret = avcodec_send_frame(codecCtx, frame_video);&#xA;        if (ret &lt; 0) {&#xA;            char error[255];&#xA;            av_strerror(ret, error, 255);&#xA;            fprintf(stderr, "Error encoding video frame: %s\n", error);&#xA;            exit(1);&#xA;        }&#xA;        /* If size is zero, it means the image was buffered. */&#xA;        ret = avcodec_receive_packet(codecCtx, &amp;pkt);&#xA;        if (!ret &amp;&amp; pkt.size) {&#xA;            pkt.stream_index = stream->index;&#xA;            fprintf(stderr, "Packet flags : %d\n", pkt.flags);&#xA;            fprintf(stderr, "Packet pts: %lld\n", pkt.pts);&#xA;            fprintf(stderr, "Packet dts: %lld\n", pkt.dts);&#xA;            fprintf(stderr, "Packet duration: %lld\n", pkt.duration);&#xA;            fprintf(stderr, "Packet pos: %lld\n\n", pkt.pos);&#xA;            &#xA;            /* Write the compressed frame to the media file. */&#xA;            ret = av_interleaved_write_frame(formatContext, &amp;pkt);&#xA;        }&#xA;        else {&#xA;            ret = 0;&#xA;        }&#xA;    }&#xA;    if (ret != 0) {&#xA;        char error[255];&#xA;        av_strerror(ret, error, 255);&#xA;        fprintf(stderr, "Error while writing video frame: %s\n", error);&#xA;        exit(1);&#xA;    }&#xA;    frame_count&#x2B;&#x2B;;&#xA;}&#xA;&#xA;static void close_video(AVFormatContext *oc, AVStream *st)&#xA;{&#xA;    av_free(frame_video->data[0]);&#xA;    av_free(frame_video);&#xA;}&#xA;&#xA;/**************************************************************/&#xA;/* media file output */&#xA;&#xA;int main(int argc, char **argv)&#xA;{&#xA;    // The outputed media&#xA;    char filename[100];&#xA;    const char *mediaFormat = "mp4"; AVCodecID mediaVideoCodec = AV_CODEC_ID_H264;&#xA;    //const char *mediaFormat="webm"; AVCodecID mediaVideoCodec = AV_CODEC_ID_VP8;&#xA;    AVOutputFormat *formatOut;&#xA;    AVFormatContext *formatCtx;&#xA;&#xA;    // The video stream&#xA;    AVStream *stream_video;&#xA;    AVCodec *codec_video = nullptr;&#xA;    AVCodecContext *codecCtx_video = nullptr;&#xA;    double time_video = 0;&#xA;&#xA;    // Return code&#xA;    int ret;&#xA;&#xA;    strcpy_s(filename, "C:\\Test.");&#xA;    strcat_s(filename, mediaFormat);&#xA;&#xA;    // allocate the output media context&#xA;    avformat_alloc_output_context2(&amp;formatCtx, NULL, NULL, filename);&#xA;    if (!formatCtx) {&#xA;        return 1;&#xA;    }&#xA;    formatOut = formatCtx->oformat;&#xA;&#xA;    // Add the video stream using H264 codec&#xA;    stream_video = NULL;&#xA;    stream_video = add_stream(formatCtx, &amp;codec_video, mediaVideoCodec, &amp;codecCtx_video);&#xA;&#xA;    // Open video codec and allocate the necessary encode buffers&#xA;    if (stream_video)&#xA;        open_video(codec_video, stream_video, codecCtx_video);&#xA;&#xA;    av_dump_format(formatCtx, 0, filename, 1);&#xA;&#xA;    // Open the output media file, if needed&#xA;    if (!(formatOut->flags &amp; AVFMT_NOFILE)) {&#xA;        ret = avio_open(&amp;formatCtx->pb, filename, AVIO_FLAG_WRITE);&#xA;        if (ret &lt; 0) {&#xA;            char error[255];&#xA;            av_strerror(ret, error, 255);&#xA;            fprintf(stderr, "Could not open &#x27;%s&#x27;: %s\n", filename, error);&#xA;            return 1;&#xA;        }&#xA;    }&#xA;&#xA;    // Write media header&#xA;    ret = avformat_write_header(formatCtx, NULL);&#xA;    if (ret &lt; 0) {&#xA;        char error[255];&#xA;        av_strerror(ret, error, 255);&#xA;        fprintf(stderr, "Error occurred when opening output file: %s\n", error);&#xA;        return 1;&#xA;    }&#xA;&#xA;    if (frame_video)&#xA;        frame_video->pts = 0;&#xA;    for (;;) {&#xA;        // Compute video time from last added video frame&#xA;        time_video = ((double)frame_video->pts) * av_q2d(stream_video->time_base);&#xA;&#xA;        // Stop media if enough time&#xA;        if (!stream_video || time_video >= STREAM_DURATION)&#xA;            break;&#xA;&#xA;        // Add a video frame&#xA;        write_video_frame(formatCtx, stream_video, codecCtx_video);&#xA;&#xA;        // Increase frame pts according to time base&#xA;        frame_video->pts &#x2B;= av_rescale_q(1, codecCtx_video->time_base, stream_video->time_base);&#xA;    }&#xA;&#xA;    // Write media trailer&#xA;    av_write_trailer(formatCtx);&#xA;&#xA;    /* Close each codec. */&#xA;    if (stream_video)&#xA;        close_video(formatCtx, stream_video);&#xA;&#xA;    if (!(formatOut->flags &amp; AVFMT_NOFILE))&#xA;        /* Close the output file. */&#xA;        avio_close(formatCtx->pb);&#xA;&#xA;    /* free the stream */&#xA;    avformat_free_context(formatCtx);&#xA;&#xA;    return 0;&#xA;}&#xA;

    &#xA;

    What am I missing ? Which part give me this error ?

    &#xA;

  • avcodec_encode_video2 the memory usage is very high

    9 juin 2020, par 陆骁剑

    code :&#xA; ```&#xA; #include "pch.h"&#xA; #include

    &#xA;&#xA;

    extern "C"&#xA;{&#xA;#ifndef __STDC_CONSTANT_MACROS&#xA;#define __STDC_CONSTANT_MACROS&#xA;#endif&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;#include "libavformat/avformat.h"&#xA;#include "libswscale/swscale.h"&#xA;#include "libavutil/avutil.h"&#xA;#include "libavutil/imgutils.h"&#xA;#include "libavutil/opt.h"&#xA;#include <libavutil></libavutil>rational.h>&#xA;//#include "libavutil/avassert.h"&#xA;&#xA;&#xA;//#include "libavutil/attributes.h"&#xA;//#include "libavutil/avassert.h"&#xA;//#include "libavutil/frame.h"&#xA;//#include "libavutil/imgutils.h"&#xA;//#include "internal1.h"&#xA;//#include "libavutil/samplefmt.h"&#xA;}&#xA;#include &#xA;//#include "avcodec.h"&#xA;//#include "frame_thread_encoder.h"&#xA;//#include "internal.h"&#xA;&#xA;// Globals&#xA;AVCodec* m_pCodec = NULL;&#xA;AVStream *m_pStream = NULL;&#xA;AVOutputFormat* m_pFormat = NULL;&#xA;AVFormatContext* m_pFormatContext = NULL;&#xA;AVCodecContext* m_pCodecContext = NULL;&#xA;AVFrame* m_pFrame = NULL;&#xA;int m_frameIndex;&#xA;&#xA;// Output format&#xA;AVPixelFormat m_pixType = AV_PIX_FMT_NV12;&#xA;// Use for mpeg4&#xA;//AVPixelFormat m_pixType = AV_PIX_FMT_YUV420P;&#xA;&#xA;// Output frame rate&#xA;int m_frameRate = 30;&#xA;// Output image dimensions&#xA;int m_imageWidth = 256;&#xA;int m_imageHeight = 1537;&#xA;// Number of frames to export&#xA;int m_frameCount = 20000;&#xA;// Output file name&#xA;const char* m_fileName = "test.h264";&#xA;// Output file type&#xA;const char* m_fileType = "H264";&#xA;// Codec name used to encode&#xA;const char* m_encoderName = "h264_qsv";&#xA;// use for mpeg4&#xA;//const char* m_encoderName = "mpeg4";&#xA;// Target bit rate&#xA;int m_targetBitRate = 400000;&#xA;&#xA;void addVideoStream()&#xA;{&#xA;    m_pStream = avformat_new_stream(m_pFormatContext, m_pCodec);&#xA;    m_pStream->id = m_pFormatContext->nb_streams - 1;&#xA;    m_pStream->time_base = m_pCodecContext->time_base;&#xA;    m_pStream->codec->pix_fmt = m_pixType;&#xA;    m_pStream->codec->flags = m_pCodecContext->flags;&#xA;    m_pStream->codec->width = m_pCodecContext->width;&#xA;    m_pStream->codec->height = m_pCodecContext->height;&#xA;    m_pStream->codec->time_base = m_pCodecContext->time_base;&#xA;    m_pStream->codec->bit_rate = m_pCodecContext->bit_rate;&#xA;}&#xA;&#xA;AVFrame* allocatePicture(enum AVPixelFormat pix_fmt, int width, int height)&#xA;{&#xA;    AVFrame *frame;&#xA;&#xA;    frame = av_frame_alloc();&#xA;&#xA;    if (!frame)&#xA;    {&#xA;        return NULL;&#xA;    }&#xA;&#xA;    frame->format = pix_fmt;&#xA;    frame->width = width;&#xA;    frame->height = height;&#xA;&#xA;    int checkImage = av_image_alloc(frame->data, frame->linesize, width, height, pix_fmt, 32);&#xA;&#xA;    if (checkImage &lt; 0)&#xA;    {&#xA;        return NULL;&#xA;    }&#xA;&#xA;    return frame;&#xA;}&#xA;&#xA;bool initialize()&#xA;{&#xA;    AVRational frameRate;&#xA;    frameRate.den = m_frameRate;&#xA;    frameRate.num = 1;&#xA;&#xA;    av_register_all();&#xA;&#xA;    m_pCodec = avcodec_find_encoder_by_name(m_encoderName);&#xA;&#xA;    if (!m_pCodec)&#xA;    {&#xA;        return false;&#xA;    }&#xA;&#xA;    m_pCodecContext = avcodec_alloc_context3(m_pCodec);&#xA;    m_pCodecContext->width = m_imageWidth;&#xA;    m_pCodecContext->height = m_imageHeight;&#xA;    m_pCodecContext->time_base = frameRate;&#xA;    m_pCodecContext->gop_size = 0;&#xA;    m_pCodecContext->pix_fmt = m_pixType;&#xA;    m_pCodecContext->codec_id = m_pCodec->id;&#xA;    m_pCodecContext->bit_rate = m_targetBitRate;&#xA;&#xA;    av_opt_set(m_pCodecContext->priv_data, "&#x2B;CBR", "", 0);&#xA;&#xA;    return true;&#xA;}&#xA;&#xA;bool startExport()&#xA;{&#xA;    m_frameIndex = 0;&#xA;    char fakeFileName[512];&#xA;    int checkAllocContext = avformat_alloc_output_context2(&amp;m_pFormatContext, NULL, m_fileType, fakeFileName);&#xA;&#xA;    if (checkAllocContext &lt; 0)&#xA;    {&#xA;        return false;&#xA;    }&#xA;&#xA;    if (!m_pFormatContext)&#xA;    {&#xA;        return false;&#xA;    }&#xA;&#xA;    m_pFormat = m_pFormatContext->oformat;&#xA;&#xA;    if (m_pFormat->video_codec != AV_CODEC_ID_NONE)&#xA;    {&#xA;        addVideoStream();&#xA;&#xA;        int checkOpen = avcodec_open2(m_pCodecContext, m_pCodec, NULL);&#xA;&#xA;        if (checkOpen &lt; 0)&#xA;        {&#xA;            return false;&#xA;        }&#xA;&#xA;        m_pFrame = allocatePicture(m_pCodecContext->pix_fmt, m_pCodecContext->width, m_pCodecContext->height);&#xA;        if (!m_pFrame)&#xA;        {&#xA;            return false;&#xA;        }&#xA;        m_pFrame->pts = 0;&#xA;    }&#xA;&#xA;    int checkOpen = avio_open(&amp;m_pFormatContext->pb, m_fileName, AVIO_FLAG_WRITE);&#xA;    if (checkOpen &lt; 0)&#xA;    {&#xA;        return false;&#xA;    }&#xA;&#xA;    av_dict_set(&amp;(m_pFormatContext->metadata), "title", "QS Test", 0);&#xA;&#xA;    int checkHeader = avformat_write_header(m_pFormatContext, NULL);&#xA;    if (checkHeader &lt; 0)&#xA;    {&#xA;        return false;&#xA;    }&#xA;&#xA;    return true;&#xA;}&#xA;&#xA;int processFrame(AVPacket&amp; avPacket)&#xA;{&#xA;    avPacket.stream_index = 0;&#xA;    avPacket.pts = av_rescale_q(m_pFrame->pts, m_pStream->codec->time_base, m_pStream->time_base);&#xA;    avPacket.dts = av_rescale_q(m_pFrame->pts, m_pStream->codec->time_base, m_pStream->time_base);&#xA;    m_pFrame->pts&#x2B;&#x2B;;&#xA;&#xA;    int retVal = av_interleaved_write_frame(m_pFormatContext, &amp;avPacket);&#xA;    return retVal;&#xA;}&#xA;&#xA;bool exportFrame()&#xA;{&#xA;    int success = 1;&#xA;    int result = 0;&#xA;&#xA;    AVPacket avPacket;&#xA;&#xA;    av_init_packet(&amp;avPacket);&#xA;    avPacket.data = NULL;&#xA;    avPacket.size = 0;&#xA;&#xA;    AVPacket* avPacket1 = new AVPacket();&#xA;&#xA;    av_init_packet(avPacket1);&#xA;    avPacket1->data = NULL;&#xA;    avPacket1->size = 0;&#xA;&#xA;    fflush(stdout);&#xA;&#xA;    std::cout &lt;&lt; "Before avcodec_encode_video2 for frame: " &lt;&lt; m_frameIndex &lt;&lt; std::endl;&#xA;    success = avcodec_encode_video2(m_pCodecContext, &amp;avPacket, m_pFrame, &amp;result);&#xA;    std::cout &lt;&lt; "After avcodec_encode_video2 for frame: " &lt;&lt; m_frameIndex &lt;&lt; std::endl;&#xA;&#xA;    if (result)&#xA;    {&#xA;        success = processFrame(avPacket);&#xA;    }&#xA;&#xA;    av_packet_unref(&amp;avPacket);&#xA;&#xA;    av_free_packet(&amp;avPacket);&#xA;    //av_frame_free(&amp;m_pFrame);&#xA;&#xA;    m_frameIndex&#x2B;&#x2B;;&#xA;    return (success == 0);&#xA;}&#xA;&#xA;void endExport()&#xA;{&#xA;    int result = 0;&#xA;    int success = 0;&#xA;&#xA;    if (m_pFrame)&#xA;    {&#xA;        while (success == 0)&#xA;        {&#xA;            AVPacket avPacket;&#xA;            av_init_packet(&amp;avPacket);&#xA;            avPacket.data = NULL;&#xA;            avPacket.size = 0;&#xA;&#xA;            fflush(stdout);&#xA;            success = avcodec_encode_video2(m_pCodecContext, &amp;avPacket, NULL, &amp;result);&#xA;&#xA;            if (result)&#xA;            {&#xA;                success = processFrame(avPacket);&#xA;            }&#xA;            av_packet_unref(&amp;avPacket);&#xA;&#xA;            if (!result)&#xA;            {&#xA;                break;&#xA;            }&#xA;        }&#xA;    }&#xA;&#xA;    if (m_pFormatContext)&#xA;    {&#xA;        av_write_trailer(m_pFormatContext);&#xA;&#xA;        if (m_pFrame)&#xA;        {&#xA;            av_frame_free(&amp;m_pFrame);&#xA;        }&#xA;&#xA;        avio_closep(&amp;m_pFormatContext->pb);&#xA;        avformat_free_context(m_pFormatContext);&#xA;        m_pFormatContext = NULL;&#xA;    }&#xA;}&#xA;&#xA;void cleanup()&#xA;{&#xA;    if (m_pFrame || m_pCodecContext)&#xA;    {&#xA;        if (m_pFrame)&#xA;        {&#xA;            av_frame_free(&amp;m_pFrame);&#xA;        }&#xA;&#xA;        if (m_pCodecContext)&#xA;        {&#xA;            avcodec_close(m_pCodecContext);&#xA;            av_free(m_pCodecContext);&#xA;        }&#xA;    }&#xA;}&#xA;&#xA;int main()&#xA;{&#xA;    bool success = true;&#xA;    if (initialize())&#xA;    {&#xA;        if (startExport())&#xA;        {&#xA;            for (int loop = 0; loop &lt; m_frameCount; loop&#x2B;&#x2B;)&#xA;            {&#xA;                if (!exportFrame())&#xA;                {&#xA;                    std::cout &lt;&lt; "Failed to export frame\n";&#xA;                    success = false;&#xA;                    break;&#xA;                }&#xA;            }&#xA;            endExport();&#xA;        }&#xA;        else&#xA;        {&#xA;            std::cout &lt;&lt; "Failed to start export\n";&#xA;            success = false;&#xA;        }&#xA;&#xA;        cleanup();&#xA;    }&#xA;    else&#xA;    {&#xA;        std::cout &lt;&lt; "Failed to initialize export\n";&#xA;        success = false;&#xA;    }&#xA;&#xA;    if (success)&#xA;    {&#xA;        std::cout &lt;&lt; "Successfully exported file\n";&#xA;    }&#xA;    return 1;&#xA;}&#xA;&#xA;&#xA;    ```&#xA;

    &#xA;&#xA;

    When I set the m_imageHeight to 1536, the memory usage is very high. But when set to 1535 or 1537 or other values, the memory usage is normal, can you tell me why ?&#xA;I have navigated to avcodec_encode_video2&#xA;enter link description here&#xA;I am using the code from the link&#xA;I have updated to the latest Intel® Graphics Driver

    &#xA;