Recherche avancée

Médias (91)

Autres articles (37)

  • Use, discuss, criticize

    13 avril 2011, par

    Talk to people directly involved in MediaSPIP’s development, or to people around you who could use MediaSPIP to share, enhance or develop their creative projects.
    The bigger the community, the more MediaSPIP’s potential will be explored and the faster the software will evolve.
    A discussion list is available for all exchanges between users.

  • 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

  • ANNEXE : Les plugins utilisés spécifiquement pour la ferme

    5 mars 2010, par

    Le site central/maître de la ferme a besoin d’utiliser plusieurs plugins supplémentaires vis à vis des canaux pour son bon fonctionnement. le plugin Gestion de la mutualisation ; le plugin inscription3 pour gérer les inscriptions et les demandes de création d’instance de mutualisation dès l’inscription des utilisateurs ; le plugin verifier qui fournit une API de vérification des champs (utilisé par inscription3) ; le plugin champs extras v2 nécessité par inscription3 (...)

Sur d’autres sites (4055)

  • FFMPEG libx265 encoding leaves memory unfreed after avcodec_free_context

    28 août 2020, par ahugeat

    I am working on H265 encoding software and, in my unit tests, I have some weird memory leaks. To found them, I have modified the encode_video.c example from FFMPEG documentation. I have changed the resolution to correspond at a 4K video, I have adapted the bitrate and I have added a pause before context allocation and another one before the final return :

    


    #include &#xA;#include &#xA;#include &#xA;#include &#xA;&#xA;#include <libavcodec></libavcodec>avcodec.h>&#xA;&#xA;#include <libavutil></libavutil>opt.h>&#xA;#include <libavutil></libavutil>imgutils.h>&#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;    const char *filename, *codec_name;&#xA;    const AVCodec *codec;&#xA;    AVCodecContext *c= NULL;&#xA;    int i, ret, x, y;&#xA;    FILE *f;&#xA;    AVFrame *frame;&#xA;    AVPacket *pkt;&#xA;    uint8_t endcode[] = { 0, 0, 1, 0xb7 };&#xA;&#xA;    if (argc &lt;= 2) {&#xA;        fprintf(stderr, "Usage: %s <output file="file"> <codec>\n", argv[0]);&#xA;        exit(0);&#xA;    }&#xA;    filename = argv[1];&#xA;    codec_name = argv[2];&#xA;&#xA;    sleep(10);&#xA;&#xA;    /* find the mpeg1video encoder */&#xA;    codec = avcodec_find_encoder_by_name(codec_name);&#xA;    if (!codec) {&#xA;        fprintf(stderr, "Codec &#x27;%s&#x27; not found\n", codec_name);&#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;    pkt = av_packet_alloc();&#xA;    if (!pkt)&#xA;        exit(1);&#xA;&#xA;    /* put sample parameters */&#xA;    c->bit_rate = 1000000;&#xA;    /* resolution must be a multiple of two */&#xA;    c->width = 3840;&#xA;    c->height = 2160;&#xA;    /* frames per second */&#xA;    c->time_base = (AVRational){1, 25};&#xA;    c->framerate = (AVRational){25, 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;    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;    ret = avcodec_open2(c, codec, NULL);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));&#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;    ret = av_frame_get_buffer(frame, 0);&#xA;    if (ret &lt; 0) {&#xA;        fprintf(stderr, "Could not allocate the video frame data\n");&#xA;        exit(1);&#xA;    }&#xA;&#xA;    /* encode 1 second of video */&#xA;    for (i = 0; i &lt; 25; i&#x2B;&#x2B;) {&#xA;        fflush(stdout);&#xA;&#xA;        /* make sure the frame data is writable */&#xA;        ret = av_frame_make_writable(frame);&#xA;        if (ret &lt; 0)&#xA;            exit(1);&#xA;&#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;        encode(c, frame, pkt, f);&#xA;    }&#xA;&#xA;    /* flush the encoder */&#xA;    encode(c, NULL, pkt, f);&#xA;&#xA;    /* add sequence end code to have a real MPEG file */&#xA;    if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO)&#xA;        fwrite(endcode, 1, sizeof(endcode), f);&#xA;    fclose(f);&#xA;&#xA;    avcodec_free_context(&amp;c);&#xA;    av_frame_free(&amp;frame);&#xA;    av_packet_free(&amp;pkt);&#xA;&#xA;    sleep(10);&#xA;&#xA;    return 0;&#xA;}&#xA;</codec></output>

    &#xA;

    I was expecting that the RAM memory usage at the first pause is the same as the second pause but there is about 55 Mo of difference. If I increase the number of encoded frames, this difference up to 390 Mo. I have tested this code under Linux Mint LMDE 4 (roughly same as Debian 10).

    &#xA;

    I guess this memory "leak" it isn't a real memory leak but that it's some internal values used by libx265 to be maybe reused for another encoding. But has there a way to free this memory through FFMPEG API ?

    &#xA;

  • Wma decoding with ffmpeg

    21 janvier 2012, par Izak

    I am new to ffmpeg and I tried using api-example.c to decode wma files. However when I run the program, it gave me an error saying

    "frame_len overflow". Does anyone know how to fix this error ?

    Here is my code :

    extern "C" {
    #include
    #include "../libavcodec/avcodec.h"
    #include
    }

    #include <iostream>
    #include
    #include
    #include
    #define INBUF_SIZE 4096
    #define AUDIO_INBUF_SIZE 20480
    #define AUDIO_REFILL_THRESH 4096

    int main(int argc, char *argv[]) {
       avcodec_init();
       avcodec_register_all();
       //avdevice_register_all();
       av_register_all();

       AVCodec *codec;
       AVCodecContext *c= NULL;

       AVCodec *ocodec;
       AVCodecContext *oc= NULL;

       int out_size, len,out_size2;
       FILE *f, *outfile;
       uint8_t *outbuf;
       uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];

       AVPacket avpkt;
       char* outfilename="test.wma";
       char* filename="Beethoven&#39;s.wma";
       AVFormatContext *pFormatCtx;

       WAVEFORMATEX* wfx=new WAVEFORMATEX;

       int ret;
       ret=av_open_input_file(&amp;pFormatCtx, filename, NULL, 0, NULL);

       if(ret!=0)
       {
           std::cout&lt;&lt;"cannot open file!"&lt;/ Find the first video stream
       audioStream=-1;
       for(int i=0; inb_streams; i++)
           if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO)
           {
               audioStream=i;
               break;
           }
       if(audioStream==-1)
       {
           std::cout&lt;&lt;"cannot find audio!"&lt;/ Get a pointer to the codec context for the audio stream
       pCodecCtx=pFormatCtx->streams[audioStream]->codec;
       av_init_packet(&amp;avpkt);

       printf("Audio decoding\n");

       /* find the suitable audio decoder */
       codec = avcodec_find_decoder(pCodecCtx->codec_id);

       if (!codec) {
           fprintf(stderr, "codec not found\n");
           exit(1);
       }

       if(codec->capabilities &amp; CODEC_CAP_TRUNCATED)
           pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;

       //open the codec (for decoding)
       int test = avcodec_open(pCodecCtx, codec);
       if (test &lt; 0) {
           fprintf(stderr, "could not open codec\n");
           exit(1);
       }

       //find mp3 encoder
       ocodec = avcodec_find_encoder(CODEC_ID_MP3);
       if (!ocodec) {
           fprintf(stderr, "codec not found\n");
           exit(1);
       }

       //allocate context
       oc= avcodec_alloc_context();
       /* put sample parameters */
       oc->bit_rate = 64000;
       oc->sample_rate = 44100;
       oc->channels = 1;
       /* open it */
       if (avcodec_open(oc, ocodec) &lt; 0) {
           fprintf(stderr, "could not open encoding codec\n");
           exit(1);
       }

       //buffer
       outbuf = (uint8_t*)malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

       //open inputfile
       f = fopen(filename, "rb");
       if (!f) {
           fprintf(stderr, "could not open %s\n", filename);
           exit(1);
       }

       //open outputfile
       outfile = fopen(outfilename, "wb");

       if (!outfile) {
           av_free(c);
           exit(1);
       }

       /* decode until eof */
       avpkt.data = inbuf;
       avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);

       //while there is still data
       while (avpkt.size > 0) {
           std::cout&lt;&lt;"decoding..."&lt;/decode
           len = avcodec_decode_audio3(pCodecCtx, (short *)outbuf, &amp;out_size, &amp;avpkt);

           if (len &lt; 0) {
               fprintf(stderr, "Error while decoding\n");
               exit(1);
           }

           if (out_size > 0) {
               /* if a frame has been decoded, output it */
               std::cout&lt;&lt;"1 frame decoded!"&lt;/subtract data from whatever decode function returns

           avpkt.size -= len;

           avpkt.data += len;

           if (avpkt.size &lt; AUDIO_REFILL_THRESH) {

               /* Refill the input buffer, to avoid trying to decode

               * incomplete frames. Instead of this, one could also use

               * a parser, or use a proper container format through

               * libavformat. */

               memmove(inbuf, avpkt.data, avpkt.size);

               avpkt.data = inbuf;

               len = fread(avpkt.data + avpkt.size, 1,

                           AUDIO_INBUF_SIZE - avpkt.size, f);

               if (len > 0)

                   avpkt.size += len;

           }



       }

       fclose(outfile);

       fclose(f);

       free(outbuf);

       avcodec_close(c);

       av_free(c);



    }
    </iostream>

    I have been stuck on this for quite a long time. Please help me.
    anyone know whats wrong with my code ?

    Thanks,

    Izak

  • Does anyone use go-av to parse mp4 audio and then use oto/portaudio to output it ?

    27 juillet 2021, par seven

    it is use go-av to get audio

    &#xA;

    func audio() (&lt;-chan []byte, error) {&#xA;    buffer := make(chan []byte, 1024)&#xA;    go func() {&#xA;     ......&#xA;      for inCtx.AvReadFrame(pkt) >= 0 {&#xA;            if pkt.StreamIndex() == audioStreamIndex {&#xA;                l := pCodecCtx.AvcodecDecodeAudio4((*avcodec.Frame)(unsafe.Pointer(utilFrame)), &amp;gotName, pkt)&#xA;                //fmt.Println("AvcodecDecodeAudio4:", l)&#xA;                if l &lt; 0 {&#xA;                    fmt.Println("codec decode audio4 error")&#xA;                    os.Exit(1)&#xA;                }&#xA;                if gotName > 0 {&#xA;&#xA;                    fram := getFramBytes(utilFrame)&#xA;                    fmt.Println("buf add:", index)&#xA;                    buffer &lt;- fram&#xA;&#xA;                }&#xA;            }&#xA;            pkt.AvFreePacket()&#xA;        }&#xA;        go func() {&#xA;            for {&#xA;                if len(buffer) &lt;= 0 {&#xA;                    fmt.Println("close buf")&#xA;                    close(buffer)&#xA;                    break&#xA;                }&#xA;            }&#xA;        }()&#xA;&#xA;        (*avcodec.Context)(unsafe.Pointer(pCodecCtxOrig)).AvcodecClose()&#xA;    }()&#xA;    return buffer, nil&#xA;}&#xA;func getFramBytes(f *avutil.Frame) []byte {&#xA;    data := avutil.Data(f)&#xA;    var bf = make([]byte, len(data))&#xA;    for i := 0; i &lt; len(data); i&#x2B;&#x2B; {&#xA;&#xA;        if data[i] != nil {&#xA;            bf = append(bf, *data[i])&#xA;        }&#xA;    }&#xA;    return bf&#xA;}&#xA;&#xA;

    &#xA;

    and it is output it

    &#xA;

    func main() {&#xA;&#xA;    portaudio.Initialize()&#xA;    defer portaudio.Terminate()&#xA;    out := make([]int32, 8192)&#xA;    stream, err := portaudio.OpenDefaultStream(0, 1, 44100, len(out), &amp;out)&#xA;    defer stream.Close()&#xA;    if err != nil {&#xA;        fmt.Println(err)&#xA;        return&#xA;    }&#xA;    err = stream.Start()&#xA;    if err != nil {&#xA;        fmt.Println(err)&#xA;        return&#xA;    }&#xA;    defer stream.Stop()&#xA;    buf, err := audio()&#xA;    if err != nil {&#xA;        fmt.Println(err)&#xA;        return&#xA;    }&#xA;    //index := 0&#xA;    //c, err := oto.NewContext(44100, 2, 2, 8192)&#xA;    //if err != nil {&#xA;    //  return&#xA;    //}&#xA;    //defer c.Close()&#xA;    //&#xA;    //p := c.NewPlayer()&#xA;    //defer p.Close()&#xA;    for {&#xA;        select {&#xA;        case frame, ok := &lt;-buf:&#xA;            if !ok {&#xA;                os.Exit(0)&#xA;            }&#xA;            //index &#x2B;= 1&#xA;            //fmt.Println("$$:", index)&#xA;            //if _, err := io.Copy(p, bytes.NewReader(frame)); err != nil {&#xA;            //  fmt.Println(err)&#xA;            //  return&#xA;            //}&#xA;            err := binary.Read(bytes.NewReader(frame), binary.BigEndian, out)&#xA;            if err != nil {&#xA;                fmt.Println("binary.Read:", err)&#xA;                os.Exit(0)&#xA;            }&#xA;            err = stream.Write()&#xA;            if err != nil {&#xA;                fmt.Println("stream.Write:", err)&#xA;                os.Exit(0)&#xA;            }&#xA;        }&#xA;    }&#xA;&#xA;}&#xA;&#xA;

    &#xA;

    ** the result is**

    &#xA;

    binary.Read : unexpected EOF

    &#xA;

    if use oto it has no effect

    &#xA;

    Has anyone used this method, or is there any other way to use go-av to play audio and video ?

    &#xA;

    Is there a problem in use ? I feel that there is a problem with the data conversion from the audio decoding.

    &#xA;

    Maybe there is a problem with getFramBytes

    &#xA;