Recherche avancée

Médias (9)

Mot : - Tags -/soundtrack

Autres articles (95)

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

  • Gestion générale des documents

    13 mai 2011, par

    MédiaSPIP ne modifie jamais le document original mis en ligne.
    Pour chaque document mis en ligne il effectue deux opérations successives : la création d’une version supplémentaire qui peut être facilement consultée en ligne tout en laissant l’original téléchargeable dans le cas où le document original ne peut être lu dans un navigateur Internet ; la récupération des métadonnées du document original pour illustrer textuellement le fichier ;
    Les tableaux ci-dessous expliquent ce que peut faire MédiaSPIP (...)

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

Sur d’autres sites (9613)

  • swscale/swscale_unscaled : avoid nv12 <-> nv21 bug

    4 mars, par Niklas Haas
    swscale/swscale_unscaled : avoid nv12 <-> nv21 bug
    

    This is not handled by the planar copy wrapper, so exclude it.

    • [DH] libswscale/swscale_unscaled.c
  • Encoding/Decoding H264 using libav in C++ [closed]

    20 mai, par gbock93

    I want to build an application to

    &#xA;

      &#xA;
    • capture frames in YUYV 4:2:2 format
    • &#xA;

    • encode them to H264
    • &#xA;

    • send over network
    • &#xA;

    • decode the received data
    • &#xA;

    • display the video stream
    • &#xA;

    &#xA;

    To do so I wrote 2 classes, H264Encoder and H264Decoder.

    &#xA;

    I post only the .cpp contents, the .h are trivial :

    &#xA;

    H264Encoder.cpp

    &#xA;

    #include &#xA;&#xA;#include <stdexcept>&#xA;#include <iostream>&#xA;&#xA;H264Encoder::H264Encoder(unsigned int width_, unsigned int height_, unsigned int fps_):&#xA;    m_width(width_),&#xA;    m_height(height_),&#xA;    m_fps(fps_),&#xA;    m_frame_index(0),&#xA;    m_context(nullptr),&#xA;    m_frame(nullptr),&#xA;    m_packet(nullptr),&#xA;    m_sws_ctx(nullptr)&#xA;{&#xA;    // Find the video codec&#xA;    AVCodec* codec;&#xA;    codec = avcodec_find_encoder(AV_CODEC_ID_H264);&#xA;    if (!codec)&#xA;        throw std::runtime_error("[Encoder]: Error: Codec not found");&#xA;&#xA;    // Allocate codec&#xA;    m_context = avcodec_alloc_context3(codec);&#xA;    if (!m_context)&#xA;        throw std::runtime_error("[Encoder]: Error: Could not allocate codec context");&#xA;&#xA;    // Configure codec&#xA;    av_opt_set(m_context->priv_data, "preset", "ultrafast", 0);&#xA;    av_opt_set(m_context->priv_data, "tune", "zerolatency", 0);&#xA;    av_opt_set(m_context->priv_data, "crf",           "35", 0); // Range: [0; 51], sane range: [18; 26], lower -> higher compression&#xA;&#xA;    m_context->width        = (int)width_;&#xA;    m_context->height       = (int)height_;&#xA;    m_context->time_base    = {1, (int)fps_};&#xA;    m_context->framerate    = {(int)fps_, 1};&#xA;    m_context->codec_id     = AV_CODEC_ID_H264;&#xA;    m_context->pix_fmt      = AV_PIX_FMT_YUV420P; // H265|4 codec take as input only AV_PIX_FMT_YUV420P&#xA;    m_context->bit_rate     = 400000;&#xA;    m_context->gop_size     = 10;&#xA;    m_context->max_b_frames = 1;&#xA;&#xA;    // Open codec&#xA;    if (avcodec_open2(m_context, codec, nullptr) &lt; 0)&#xA;        throw std::runtime_error("[Encoder]: Error: Could not open codec");&#xA;&#xA;    // Allocate frame and its buffer&#xA;    m_frame = av_frame_alloc();&#xA;    if (!m_frame) &#xA;        throw std::runtime_error("[Encoder]: Error: Could not allocate frame");&#xA;&#xA;    m_frame->format = m_context->pix_fmt;&#xA;    m_frame->width  = m_context->width;&#xA;    m_frame->height = m_context->height;&#xA;&#xA;    if (av_frame_get_buffer(m_frame, 0) &lt; 0)&#xA;        throw std::runtime_error("[Encoder]: Error: Cannot allocate frame buffer");&#xA;    &#xA;    // Allocate packet&#xA;    m_packet = av_packet_alloc();&#xA;    if (!m_packet) &#xA;        throw std::runtime_error("[Encoder]: Error: Could not allocate packet");&#xA;&#xA;    // Convert from YUYV422 to YUV420P&#xA;    m_sws_ctx = sws_getContext(&#xA;        width_, height_, AV_PIX_FMT_YUYV422,&#xA;        width_, height_, AV_PIX_FMT_YUV420P,&#xA;        SWS_BILINEAR, nullptr, nullptr, nullptr&#xA;    );&#xA;    if (!m_sws_ctx) &#xA;        throw std::runtime_error("[Encoder]: Error: Could not allocate sws context");&#xA;&#xA;    //&#xA;    printf("[Encoder]: H264Encoder ready.\n");&#xA;}&#xA;&#xA;H264Encoder::~H264Encoder()&#xA;{&#xA;    sws_freeContext(m_sws_ctx);&#xA;    av_packet_free(&amp;m_packet);&#xA;    av_frame_free(&amp;m_frame);&#xA;    avcodec_free_context(&amp;m_context);&#xA;&#xA;    printf("[Encoder]: H264Encoder destroyed.\n");&#xA;}&#xA;&#xA;std::vector H264Encoder::encode(const cv::Mat&amp; img_)&#xA;{&#xA;    /*&#xA;    - YUYV422 is a packed format. It has 3 components (av_pix_fmt_desc_get((AVPixelFormat)AV_PIX_FMT_YUYV422)->nb_components == 3) but&#xA;        data is stored in a single plane (av_pix_fmt_count_planes((AVPixelFormat)AV_PIX_FMT_YUYV422) == 1).&#xA;    - YUV420P is a planar format. It has 3 components (av_pix_fmt_desc_get((AVPixelFormat)AV_PIX_FMT_YUV420P)->nb_components == 3) and&#xA;        each component is stored in a separate plane (av_pix_fmt_count_planes((AVPixelFormat)AV_PIX_FMT_YUV420P) == 3) with its&#xA;        own stride.&#xA;    */&#xA;    std::cout &lt;&lt; "[Encoder]" &lt;&lt; std::endl;&#xA;    std::cout &lt;&lt; "[Encoder]: Encoding img " &lt;&lt; img_.cols &lt;&lt; "x" &lt;&lt; img_.rows &lt;&lt; " | element size " &lt;&lt; img_.elemSize() &lt;&lt; std::endl;&#xA;    assert(img_.elemSize() == 2);&#xA;&#xA;    uint8_t* input_data[1] = {(uint8_t*)img_.data};&#xA;    int input_linesize[1] = {2 * (int)m_width};&#xA;    &#xA;    if (av_frame_make_writable(m_frame) &lt; 0)&#xA;        throw std::runtime_error("[Encoder]: Error: Cannot make frame data writable");&#xA;&#xA;    // Convert from YUV422 image to YUV420 frame. Apply scaling if necessary&#xA;    sws_scale(&#xA;        m_sws_ctx,&#xA;        input_data, input_linesize, 0, m_height,&#xA;        m_frame->data, m_frame->linesize&#xA;    );&#xA;    m_frame->pts = m_frame_index;&#xA;&#xA;    int n_planes = av_pix_fmt_count_planes((AVPixelFormat)m_frame->format);&#xA;    std::cout &lt;&lt; "[Encoder]: Sending Frame " &lt;&lt; m_frame_index &lt;&lt; " with dimensions " &lt;&lt; m_frame->width &lt;&lt; "x" &lt;&lt; m_frame->height &lt;&lt; "x" &lt;&lt; n_planes &lt;&lt; std::endl;&#xA;    for (int i=0; iframerate.num) &#x2B; 1;&#xA;            break;&#xA;        case AVERROR(EAGAIN):&#xA;            throw std::runtime_error("[Encoder]: avcodec_send_frame: EAGAIN");&#xA;        case AVERROR_EOF:&#xA;            throw std::runtime_error("[Encoder]: avcodec_send_frame: EOF");&#xA;        case AVERROR(EINVAL):&#xA;            throw std::runtime_error("[Encoder]: avcodec_send_frame: EINVAL");&#xA;        case AVERROR(ENOMEM):&#xA;            throw std::runtime_error("[Encoder]: avcodec_send_frame: ENOMEM");&#xA;        default:&#xA;            throw std::runtime_error("[Encoder]: avcodec_send_frame: UNKNOWN");&#xA;    }&#xA;&#xA;    // Receive packet from codec&#xA;    std::vector result;&#xA;    while(ret >= 0)&#xA;    {&#xA;        ret = avcodec_receive_packet(m_context, m_packet);&#xA;&#xA;        switch (ret)&#xA;        {&#xA;        case 0:&#xA;            std::cout &lt;&lt; "[Encoder]: Received packet from codec of size " &lt;&lt; m_packet->size &lt;&lt; " bytes " &lt;&lt; std::endl;&#xA;            result.insert(result.end(), m_packet->data, m_packet->data &#x2B; m_packet->size);&#xA;            av_packet_unref(m_packet);&#xA;            break;&#xA;&#xA;        case AVERROR(EAGAIN):&#xA;            std::cout &lt;&lt; "[Encoder]: avcodec_receive_packet: EAGAIN" &lt;&lt; std::endl;&#xA;            break;&#xA;        case AVERROR_EOF:&#xA;            std::cout &lt;&lt; "[Encoder]: avcodec_receive_packet: EOF" &lt;&lt; std::endl;&#xA;            break;&#xA;        case AVERROR(EINVAL):&#xA;            throw std::runtime_error("[Encoder]: avcodec_receive_packet: EINVAL");&#xA;        default:&#xA;            throw std::runtime_error("[Encoder]: avcodec_receive_packet: UNKNOWN");&#xA;        }&#xA;    }&#xA;&#xA;    std::cout &lt;&lt; "[Encoder]: Encoding complete" &lt;&lt; std::endl;&#xA;    return result;&#xA;}&#xA;</iostream></stdexcept>

    &#xA;

    H264Decoder.cpp

    &#xA;

    #include &#xA;&#xA;#include <iostream>&#xA;#include <stdexcept>&#xA;&#xA;H264Decoder::H264Decoder():&#xA;    m_context(nullptr),&#xA;    m_frame(nullptr),&#xA;    m_packet(nullptr)&#xA;{&#xA;    // Find the video codec&#xA;    AVCodec* codec;&#xA;    codec = avcodec_find_decoder(AV_CODEC_ID_H264);&#xA;    if (!codec)&#xA;        throw std::runtime_error("[Decoder]: Error: Codec not found");&#xA;&#xA;    // Allocate codec&#xA;    m_context = avcodec_alloc_context3(codec);&#xA;    if (!m_context)&#xA;        throw std::runtime_error("[Decoder]: Error: Could not allocate codec context");&#xA;&#xA;    // Open codec&#xA;    if (avcodec_open2(m_context, codec, nullptr) &lt; 0)&#xA;        throw std::runtime_error("[Decoder]: Error: Could not open codec");&#xA;&#xA;    // Allocate frame&#xA;    m_frame = av_frame_alloc();&#xA;    if (!m_frame)&#xA;        throw std::runtime_error("[Decoder]: Error: Could not allocate frame");&#xA;&#xA;    // Allocate packet&#xA;    m_packet = av_packet_alloc();&#xA;    if (!m_packet) &#xA;        throw std::runtime_error("[Decoder]: Error: Could not allocate packet");&#xA;&#xA;    //&#xA;    printf("[Decoder]: H264Decoder ready.\n");&#xA;}&#xA;&#xA;H264Decoder::~H264Decoder()&#xA;{&#xA;    av_packet_free(&amp;m_packet);&#xA;    av_frame_free(&amp;m_frame);&#xA;    avcodec_free_context(&amp;m_context);&#xA;&#xA;    printf("[Decoder]: H264Decoder destroyed.\n");&#xA;}&#xA;&#xA;bool H264Decoder::decode(uint8_t* data_, size_t size_, cv::Mat&amp; img_)&#xA;{&#xA;    std::cout &lt;&lt; "[Decoder]" &lt;&lt; std::endl;&#xA;    std::cout &lt;&lt; "[Decoder]: decoding " &lt;&lt; size_ &lt;&lt; " bytes of data" &lt;&lt; std::endl;&#xA;&#xA;    // Fill packet&#xA;    m_packet->data = data_;&#xA;    m_packet->size = size_;&#xA;&#xA;    if (size_ == 0)&#xA;        return false;&#xA;&#xA;    // Send packet to codec&#xA;    int send_result = avcodec_send_packet(m_context, m_packet);&#xA;&#xA;    switch (send_result)&#xA;    {&#xA;        case 0:&#xA;            std::cout &lt;&lt; "[Decoder]: Sent packet to codec" &lt;&lt; std::endl;&#xA;            break;&#xA;        case AVERROR(EAGAIN):&#xA;            throw std::runtime_error("[Decoder]: avcodec_send_packet: EAGAIN");&#xA;        case AVERROR_EOF:&#xA;            throw std::runtime_error("[Decoder]: avcodec_send_packet: EOF");&#xA;        case AVERROR(EINVAL):&#xA;            throw std::runtime_error("[Decoder]: avcodec_send_packet: EINVAL");&#xA;        case AVERROR(ENOMEM):&#xA;            throw std::runtime_error("[Decoder]: avcodec_send_packet: ENOMEM");&#xA;        default:&#xA;            throw std::runtime_error("[Decoder]: avcodec_send_packet: UNKNOWN");&#xA;    }&#xA;&#xA;    // Receive frame from codec&#xA;    int n_planes;&#xA;    uint8_t* output_data[1];&#xA;    int output_line_size[1];&#xA;&#xA;    int receive_result = avcodec_receive_frame(m_context, m_frame);&#xA;&#xA;    switch (receive_result)&#xA;    {&#xA;        case 0:&#xA;            n_planes = av_pix_fmt_count_planes((AVPixelFormat)m_frame->format);&#xA;            std::cout &lt;&lt; "[Decoder]: Received Frame with dimensions " &lt;&lt; m_frame->width &lt;&lt; "x" &lt;&lt; m_frame->height &lt;&lt; "x" &lt;&lt; n_planes &lt;&lt; std::endl;&#xA;            for (int i=0; i/&#xA;    std::cout &lt;&lt; "[Decoder]: Decoding complete" &lt;&lt; std::endl;&#xA;    return true;&#xA;}&#xA;</stdexcept></iostream>

    &#xA;

    To test the two classes I put together a main.cpp to grab a frame, encode/decode and display the decoded frame (no network transmission in place) :

    &#xA;

    main.cpp

    &#xA;

    while(...)&#xA;{&#xA;    // get frame from custom camera class. Format is YUYV 4:2:2&#xA;    camera.getFrame(camera_frame);&#xA;    // Construct a cv::Mat to represent the grabbed frame&#xA;    cv::Mat camera_frame_yuyv = cv::Mat(camera_frame.height, camera_frame.width, CV_8UC2, camera_frame.data.data());&#xA;    // Encode image&#xA;    std::vector encoded_data = encoder.encode(camera_frame_yuyv);&#xA;    if (!encoded_data.empty())&#xA;    {&#xA;        // Decode image&#xA;        cv::Mat decoded_frame;&#xA;        if (decoder.decode(encoded_data.data(), encoded_data.size(), decoded_frame))&#xA;        {&#xA;            // Display image&#xA;            cv::imshow("Camera", decoded_frame);&#xA;            cv::waitKey(1);&#xA;        }&#xA;    }&#xA;}&#xA;

    &#xA;

    Compiling and executing the code I get random results between subsequent executions :

    &#xA;

      &#xA;
    • Sometimes the whole loop runs without problems and I see the decoded image.
    • &#xA;

    • Sometimes the program crashes at the sws_scale(...) call in the decoder with "Assertion desc failed at src/libswscale/swscale_internal.h:757".
    • &#xA;

    • Sometimes the loop runs but I see a black image and the message Slice parameters 0, 720 are invalid is displayed when executing the sws_scale(...) call in the decoder.
    • &#xA;

    &#xA;

    Why is the behaviour so random ? What am I doing wrong with the libav API ?

    &#xA;

    Some resources I found useful :

    &#xA;

    &#xA;

  • Encoding AAC audio with libav

    14 mars 2018, par Michael IV

    I am trying to encode AAC using FFMPEG C libs. The closest thing I found on SO is this question,but it deals with mp4 container which has aac stream. That’s not what I am trying to do. I am encoding single audio file. Now, FFMPEG example for audio encoding doesn’t show aac and it is not clear if it is enough also for the aac codec. Here is how I do it :

    Setup :

       AVCodec*              mInputAudioCodec = NULL;  
       AVCodecContext*       mInputAudioCodecContext = NULL;
       AVPacket*             mAudioPacket = NULL;
       AVFrame*              mAudioInputFrame = NULL;

       mInputAudioCodec =  avcodec_find_encoder(AV_CODEC_ID_AAC);
       if (!mInputAudioCodec)
       {
           return false;
       }

       mInputAudioCodecContext = avcodec_alloc_context3(mInputAudioCodec);
       if (!mInputAudioCodecContext)
       {
           return false;
       }

       mInputAudioCodecContext->bit_rate = 192000;// 64000;
       mInputAudioCodecContext->sample_fmt = AV_SAMPLE_FMT_FLTP; AV_SAMPLE_FMT_S16;
       // check that a given sample format is supported by the encoder
       const enum AVSampleFormat *p = mInputAudioCodec->sample_fmts;
       bool formatSupported = false;
       while (*p != AV_SAMPLE_FMT_NONE)
       {
           if (*p == mInputAudioCodecContext->sample_fmt)
           {
               formatSupported = true;
               break;
           }
           p++;
       }
       if (formatSupported == false)
       {
           return false;
       }

       mInputAudioCodecContext->sample_rate = 48000;
       mInputAudioCodecContext->channel_layout = AV_CH_LAYOUT_STEREO;
       mInputAudioCodecContext->channels = av_get_channel_layout_nb_channels(mInputAudioCodecContext->channel_layout);
       //  open codec:
       if (avcodec_open2(mInputAudioCodecContext, mInputAudioCodec, NULL) &lt; 0)
       {
           return false;
       }
       mAudioPacket = av_packet_alloc();
       mAudioInputFrame = av_frame_alloc();
       mAudioInputFrame->nb_samples     = mInputAudioCodecContext->frame_size;
       mAudioInputFrame->format         = mInputAudioCodecContext->sample_fmt;
       mAudioInputFrame->channel_layout = mInputAudioCodecContext->channel_layout;
       mAudioInputFrame->sample_rate    = mInputAudioCodecContext->sample_rate;
       if (av_frame_get_buffer(mAudioInputFrame, 0) &lt; 0)
       {
           return false;
       }
       mFileOut = fopen("audio.aac","wb");

    Encoding :

    For the simplicity, I encode synthetic frames,just like in FFMPEG example.
    FLTP is planar format,so I write dummy data into two separate buffers.

       int ret = 0;
       ret = av_frame_make_writable(mAudioInputFrame);
       if (ret &lt; 0)
       {
           return;
       }
            //generate sound data:
               float* samples0 = (float*)mAudioInputFrame->data[0];
               float* samples1 = (float*)mAudioInputFrame->data[1];
               float t = 0;
               float tincr = 2 * M_PI * 440.0f / mInputAudioCodecContext->sample_rate;
               for (int j = 0; j &lt; mInputAudioCodecContext->frame_size; j++)
               {
                   *samples0 = (sin(t) * 10000);
                   *samples1 = (sin(t) * 10000);
                   samples0++;
                   samples1++;
                   t += tincr;
               }



               av_init_packet(mAudioPacket);
               mAudioPacket->data = NULL;
               mAudioPacket->size = 0;

               ret = avcodec_send_frame(mInputAudioCodecContext, mAudioInputFrame);
               if (ret &lt; 0)
               {
                   fprintf(stderr, "Error sending the frame to the encoder\n");
               }

               /* read all the available output packets (in general there may be any
               * number of them */
               while (ret >= 0)
               {
                   ret = avcodec_receive_packet(mInputAudioCodecContext, mAudioPacket);
                   if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
                   {
                       return;
                   }
                   else if (ret &lt; 0)
                   {
                       return;
                   }

                   fwrite(mAudioPacket->data, 1, mAudioPacket->size, mFileOut);


               }

               av_packet_unref(mAudioPacket);

    The encoding session performs ok, avcodec lib doesn’t spit any warnings or errors. But the resulting .aac file is not opened neither by VLC nor by any other audio player. It is corrupted. I ran FFProbe on the file and here is what it says :

    [aac @ 000000000274b840] Format aac detected only with low score of 1,
    misdetection possible ! [aac @ 00000000026fb2e0] More than one AAC RDB
    per ADTS frame is not implemented. Update your FFmpeg version to the
    newest one from Git. If the problem still occurs, it means that your
    file has a feature which has not been implemented. [aac @
    00000000026fb2e0] Sample rate index in program config element does not
    match the sample rate index configured by the container. [aac @
    00000000026fb2e0] Inconsistent channel configuration. [aac @
    00000000026fb2e0] get_buffer() failed [aac @ 00000000026fb2e0] channel
    element 2.15 is not allocated [aac @ 00000000026fb2e0] Assuming an
    incorrectly encoded 7.1 channel layout instead of a spec-compliant
    7.1(wide) layout, use -strict 1 to decode according to the specification instead. [aac @ 00000000026fb2e0] Multiple frames in a
    packet. [aac @ 00000000026fb2e0] Number of scalefactor bands in group
    (53) exceeds limit (51). [aac @ 00000000026fb2e0] channel element 2.1
    is not allocated [aac @ 000000000274b840] decoding for stream 0 failed
    [aac @ 000000000274b840] Estimating duration from bitrate, this may be
    inaccurate [aac @ 000000000274b840] Could not find codec parameters
    for stream 0 (Audio : aac (SSR), stereo, fltp, 254 kb/s) : unspecified
    sample rate Consider increasing the value for the ’analyzeduration’
    and ’probesize’ options Input #0, aac, from ’audio.aac’ : Duration :
    00:00:03.21, bitrate : 254 kb/s
    Stream #0:0 : Audio : aac (SSR), stereo, fltp, 254 kb/s

    What am I doing wrong here ?