Recherche avancée

Médias (91)

Autres articles (45)

  • Participer à sa traduction

    10 avril 2011

    Vous pouvez nous aider à améliorer les locutions utilisées dans le logiciel ou à traduire celui-ci dans n’importe qu’elle nouvelle langue permettant sa diffusion à de nouvelles communautés linguistiques.
    Pour ce faire, on utilise l’interface de traduction de SPIP où l’ensemble des modules de langue de MediaSPIP sont à disposition. ll vous suffit de vous inscrire sur la liste de discussion des traducteurs pour demander plus d’informations.
    Actuellement MediaSPIP n’est disponible qu’en français et (...)

  • Support audio et vidéo HTML5

    10 avril 2011

    MediaSPIP utilise les balises HTML5 video et audio pour la lecture de documents multimedia en profitant des dernières innovations du W3C supportées par les navigateurs modernes.
    Pour les navigateurs plus anciens, le lecteur flash Flowplayer est utilisé.
    Le lecteur HTML5 utilisé a été spécifiquement créé pour MediaSPIP : il est complètement modifiable graphiquement pour correspondre à un thème choisi.
    Ces technologies permettent de distribuer vidéo et son à la fois sur des ordinateurs conventionnels (...)

  • D’autres logiciels intéressants

    12 avril 2011, par

    On ne revendique pas d’être les seuls à faire ce que l’on fait ... et on ne revendique surtout pas d’être les meilleurs non plus ... Ce que l’on fait, on essaie juste de le faire bien, et de mieux en mieux...
    La liste suivante correspond à des logiciels qui tendent peu ou prou à faire comme MediaSPIP ou que MediaSPIP tente peu ou prou à faire pareil, peu importe ...
    On ne les connais pas, on ne les a pas essayé, mais vous pouvez peut être y jeter un coup d’oeil.
    Videopress
    Site Internet : (...)

Sur d’autres sites (6468)

  • 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.

    


    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 :

    


    


    unspecified pixel format

    


    


    ffprobe version N-93020-g3224d6691c Copyright (c) 2007-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181201
  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
  libavutil      56. 26.100 / 56. 26.100
  libavcodec     58. 44.100 / 58. 44.100
  libavformat    58. 26.100 / 58. 26.100
  libavdevice    58.  6.101 / 58.  6.101
  libavfilter     7. 48.100 /  7. 48.100
  libswscale      5.  4.100 /  5.  4.100
  libswresample   3.  4.100 /  3.  4.100
  libpostproc    55.  4.100 / 55.  4.100
[h264 @ 02a46240] non-existing PPS 0 referenced
[h264 @ 02a46240] decode_slice_header error
[h264 @ 02a46240] no frame!
...
[h264 @ 02a46240] non-existing PPS 0 referenced
[h264 @ 02a46240] decode_slice_header error
[h264 @ 02a46240] no frame!
[mov,mp4,m4a,3gp,3g2,mj2 @ 02a35380] decoding for stream 0 failed
[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
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\Fabrice\Desktop\video\Test.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.26.100
  Duration: 00:00:09.00, start: 0.000000, bitrate: 323 kb/s
    Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 352x288, 320 kb/s, 25.11 fps, 25 tbr, 12800 tbn, 25600 tbc (default)
    Metadata:
      handler_name    : VideoHandler


    


    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.

    


    #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;

  • ffmpeg avcodec_encode_video2 hangs when using Quick Sync h264_qsv encoder

    9 juin 2020, par Mike Simpson

    When I use the mpeg4 or h264 encoders, I am able to successfully encode images to make a valid AVI file using the API for ffmpeg 3.1.0. However, when I use the Quick Sync encoder (h264_qsv), avcodec_encode_video2 will hang some of the time. I found that when using images that are 1920x1080, it was rare that avcodec_encode_video2 would hang. When using 256x256 images, it was very likely that the function would hang.

    &#xA;&#xA;

    I have created the test code below that demonstrates the hang of avcodec_encode_video2. The code will create a 1000 frame, 256x256 AVI with a bit rate of 400000. The frames are simply allocated, so the output video should just be green frames.

    &#xA;&#xA;

    The problem was observed using Windows 7 and Windows 10, using the 32-bit or 64-bit test application.

    &#xA;&#xA;

    If anyone has any idea on how I can avoid the avcodec_encode_video2 hang I would be very grateful ! Thanks in advance for any assistance.

    &#xA;&#xA;

    extern "C"&#xA;{&#xA;#ifndef __STDC_CONSTANT_MACROS&#xA;#define __STDC_CONSTANT_MACROS&#xA;#endif&#xA;#include "avcodec.h"&#xA;#include "avformat.h"&#xA;#include "swscale.h"&#xA;#include "avutil.h"&#xA;#include "imgutils.h"&#xA;#include "opt.h"&#xA;#include &#xA;}&#xA;&#xA;#include <iostream>&#xA;&#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 = 256;&#xA;// Number of frames to export&#xA;int m_frameCount = 1000;&#xA;// Output file name&#xA;const char* m_fileName = "c:/test/test.avi";&#xA;// Output file type&#xA;const char* m_fileType = "AVI";&#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;    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;    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;</iostream>

    &#xA;