Recherche avancée

Médias (1)

Mot : - Tags -/Christian Nold

Autres articles (51)

  • Personnaliser en ajoutant son logo, sa bannière ou son image de fond

    5 septembre 2013, par

    Certains thèmes prennent en compte trois éléments de personnalisation : l’ajout d’un logo ; l’ajout d’une bannière l’ajout d’une image de fond ;

  • 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

  • Des sites réalisés avec MediaSPIP

    2 mai 2011, par

    Cette page présente quelques-uns des sites fonctionnant sous MediaSPIP.
    Vous pouvez bien entendu ajouter le votre grâce au formulaire en bas de page.

Sur d’autres sites (10313)

  • Convert .m4a to PCM using libavcodec

    17 décembre 2013, par gmcc051

    I'm trying to convert a .m4a file to raw PCM file so that I can play it back in Audacity.

    According to the AVCodecContext it is a 44100 Hz track using the sample format AV_SAMPLE_FMT_FLTP which, to my understanding, when decodeded using avcodec_decode_audio4, I should get two arrays of floating point values (one for each channel).

    I'm unsure of the significance of the AVCodecContext's bits_per_coded_sample = 16

    Unfortunately Audacity plays the result back as if I have the original track is mixed in with some white noise.

    Here is some sample code of what I've been done. Note that I've also added a case for a track that uses signed 16bit non-interleaved data (sample_format = AC_SAMPLE_FMT_S16P), which Audacity plays back fine.

    int AudioDecoder::decode(std::string path)
    {
     const char* input_filename=path.c_str();

     av_register_all();

     AVFormatContext* container=avformat_alloc_context();
     if(avformat_open_input(&container,input_filename,NULL,NULL)<0){
       printf("Could not open file");
     }

     if(avformat_find_stream_info(container, NULL)<0){
         printf("Could not find file info");
     }
     av_dump_format(container,0,input_filename,false);

     int stream_id=-1;
     int i;
     for(i=0;inb_streams;i++){
       if(container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){
           stream_id=i;
           break;
       }
     }
     if(stream_id==-1){
       printf("Could not find Audio Stream");
     }

     AVDictionary *metadata=container->metadata;
     AVCodecContext *ctx=container->streams[stream_id]->codec;
     AVCodec *codec=avcodec_find_decoder(ctx->codec_id);

     if(codec==NULL){
       printf("cannot find codec!");
     }

     if(avcodec_open2(ctx,codec,NULL)<0){
        printf("Codec cannot be found");
     }

     AVSampleFormat sfmt = ctx->sample_fmt;

     AVPacket packet;
     av_init_packet(&packet);
     AVFrame *frame = avcodec_alloc_frame();

     int buffer_size = AVCODEC_MAX_AUDIO_FRAME_SIZE+ FF_INPUT_BUFFER_PADDING_SIZE;;
     uint8_t buffer[buffer_size];
     packet.data=buffer;
     packet.size =buffer_size;

     FILE *outfile = fopen("test.raw", "wb");

     int len;
     int frameFinished=0;

     while(av_read_frame(container,&packet) >= 0)
     {
         if(packet.stream_index==stream_id)
         {
           //printf("Audio Frame read \n");
           int len=avcodec_decode_audio4(ctx, frame, &frameFinished, &packet);

           if(frameFinished)
           {      
             if (sfmt==AV_SAMPLE_FMT_S16P)
             { // Audacity: 16bit PCM little endian stereo
               int16_t* ptr_l = (int16_t*)frame->extended_data[0];
               int16_t* ptr_r = (int16_t*)frame->extended_data[1];
               for (int i=0; inb_samples; i++)
               {
                 fwrite(ptr_l++, sizeof(int16_t), 1, outfile);
                 fwrite(ptr_r++, sizeof(int16_t), 1, outfile);
               }
             }
             else if (sfmt==AV_SAMPLE_FMT_FLTP)
             { //Audacity: big endian 32bit stereo start offset 7 (but has noise)
               float* ptr_l = (float*)frame->extended_data[0];
               float* ptr_r = (float*)frame->extended_data[1];
               for (int i=0; inb_samples; i++)
               {
                   fwrite(ptr_l++, sizeof(float), 1, outfile);
                   fwrite(ptr_r++, sizeof(float), 1, outfile);
                }
              }            
           }
       }
    }
    fclose(outfile);
    av_close_input_file(container);
    return 0;  

    }

    I'm hoping I've just done a naive conversion (most/less significant bit issues), but at present I've been unable to figure it out. Note that Audacity can only import RAW float data if its 32bit or 64 bit float (big or little endian).

    Thanks for any insight.

  • omxplayer AVPacket

    6 février 2013, par Oleksandr Kyrpa

    Hi Im trying decode video usin OpenMax component and hello_video example from GitHub.
    I simply edit video.c and replace "main frame while" on AVFormat -read frame packet for read not only *.h264 files . (I need read mp4, mov, mkv....)..
    So with test.h264 files and other files that I generate with ffmpeg, all works fine and on screen I can see movie.

    ffmpeg -i file.mp4 -vcodec copy -vbsf h264_mp4toannexb out.h264

    But if I open test.mp4 file I can't see pictures on the screen, and log in console show me that data read correctly and parsed to the input buffer of the video decoder correctly.
    Can any explain me why I can't see anything on the screen on second test ?

       do{
       printf("###before DO!\n");
    status=av_read_frame(pFormatCtx,&packet);
    //only for video
    if(packet.stream_index==*video_stream_index){
    printf("=>Read frame, status: %d, index: %d, stream index: %d, packet duration: %d, size: %d\n",pstatus,index++,packet.stream_index,packet.duration,packet.size);
    int psize=packet.size;
    int preaded=0;
    double pts=packet.duration;
    while(psize!=0){
        buf = ilclient_get_input_buffer(video_decode, 130, 1);
         buf->nFlags = 0;
         buf->nOffset = 0;
         uint64_t val = (uint64_t)(pts == DVD_NOPTS_VALUE) ? 0 : pts;
        if(first_frame==true){buf->nFlags = OMX_BUFFERFLAG_STARTTIME;first_frame=false;}else{buf->nFlags = OMX_BUFFERFLAG_TIME_UNKNOWN;}
    buf->nTimeStamp = ToOMXTime(val);
    buf->nFilledLen = (psize > buf->nAllocLen) ? buf->nAllocLen : psize;
    memcpy(buf->pBuffer, packet.data+preaded,buf->nFilledLen);
    psize-=buf->nFilledLen;
    preaded+=buf->nFilledLen;
        if(psize == 0){buf->nFlags|=OMX_BUFFERFLAG_ENDOFFRAME;printf("#######################################OMX_BUFFERFLAG_ENDOFFRAME\n");}
    printf("=>BUFF size: %d\n",buf->nFilledLen);
    OMX_ERRORTYPE r;
    if(pstatus==0){if(r=OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone){status = -6;printf("Failed, OMX_EmptyThisBuffer, error: 0x%08x , buf allocate: %d, buf lenght: %d \n", r,buf->nAllocLen,buf->nFilledLen);break;}}
    }//while psize
    av_free_packet(&packet);
      }//if index  
    }//do
    while(pstatus==0);
  • FFmpeg C++ api decode h264 error

    29 mai 2015, par arms

    I’m trying to use the C++ API of FFMpeg (version 20150526) under Windows using the prebuilt binaries to decode an h264 video file (*.ts).

    I’ve written a very simple code that automatically detects the required codec from the file itself (and it is AV_CODEC_ID_H264, as expected).

    Then I re-open the video file in read-binary mode and I read a fixed-size buffer of bytes from it and provide the read bytes to the decoder within a while-loop until the end of file. However when I call the function avcodec_decode_video2 a large amount of errors happen like the following ones :

    [h264 @ 008df020] top block unavailable for requested intro mode at 34 0

    [h264 @ 008df020] error while decoding MB 34 0, bytestream 3152

    [h264 @ 008df020] decode_slice_header error

    Sometimes the function avcodec_decode_video2 sets the value of got_picture_ptr to 1 and hence I expect to find a good frame. Instead, though all the computations are successful, when I view the decoded frame (using OpenCV only for visualization purposes) I see a gray one with some artifacts.

    If I employ the same code to decode an *.avi file it works fine.

    Reading the examples of FFMpeg I did not find a solution to my problem. I’ve also implemented the solution proposed in the simlar question FFmpeg c++ H264 decoding error but it did not work.

    Does anyone know where the error is ?

    Thank you in advance for any reply !

    The code is the following [EDIT : code updated including the parser management] :

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>

    #include <opencv2></opencv2>opencv.hpp>

    #ifdef __cplusplus
    extern "C"
    {
    #endif // __cplusplus
    #include <libavcodec></libavcodec>avcodec.h>
    #include <libavdevice></libavdevice>avdevice.h>
    #include <libavfilter></libavfilter>avfilter.h>
    #include <libavformat></libavformat>avformat.h>
    #include <libavformat></libavformat>avio.h>
    #include <libavutil></libavutil>avutil.h>
    #include <libpostproc></libpostproc>postprocess.h>
    #include <libswresample></libswresample>swresample.h>
    #include <libswscale></libswscale>swscale.h>
    #ifdef __cplusplus
    } // end extern "C".
    #endif // __cplusplus

    #define INBUF_SIZE  4096

    void main()
    {
       AVCodec*            l_pCodec;
       AVCodecContext*     l_pAVCodecContext;
       SwsContext*         l_pSWSContext;
       AVFormatContext*    l_pAVFormatContext;
       AVFrame*            l_pAVFrame;
       AVFrame*            l_pAVFrameBGR;
       AVPacket            l_AVPacket;
       AVPacket            l_AVPacket_out;
       AVStream*           l_pStream;
       AVCodecParserContext*   l_pParser;
       FILE*               l_pFile_in;
       FILE*               l_pFile_out;
       std::string         l_sFile;
       int                 l_iResult;
       int                 l_iFrameCount;
       int                 l_iGotFrame;
       int                 l_iBufLength;
       int                 l_iParsedBytes;
       int                 l_iPts;
       int                 l_iDts;
       int                 l_iPos;
       int                 l_iSize;
       int                 l_iDecodedBytes;
       uint8_t             l_auiInBuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
       uint8_t*            l_pData;
       cv::Mat             l_cvmImage;

       l_pCodec = NULL;
       l_pAVCodecContext = NULL;
       l_pSWSContext = NULL;
       l_pAVFormatContext = NULL;
       l_pAVFrame = NULL;
       l_pAVFrameBGR = NULL;
       l_pParser = NULL;
       l_pStream = NULL;
       l_pFile_in = NULL;
       l_pFile_out = NULL;
       l_iPts = 0;
       l_iDts = 0;
       l_iPos = 0;
       l_pData = NULL;

       l_sFile = "myvideo.ts";

       avdevice_register_all();
       avfilter_register_all();
       avcodec_register_all();
       av_register_all();
       avformat_network_init();

       l_pAVFormatContext = avformat_alloc_context();

       l_iResult = avformat_open_input(&amp;l_pAVFormatContext,
                                       l_sFile.c_str(),
                                       NULL,
                                       NULL);

       if (l_iResult >= 0)
       {
           l_iResult = avformat_find_stream_info(l_pAVFormatContext, NULL);

           if (l_iResult >= 0)
           {
               for (int i=0; inb_streams; i++)
               {
                   if (l_pAVFormatContext->streams[i]->codec->codec_type ==
                           AVMEDIA_TYPE_VIDEO)
                   {
                       l_pCodec = avcodec_find_decoder(
                                   l_pAVFormatContext->streams[i]->codec->codec_id);

                       l_pStream = l_pAVFormatContext->streams[i];
                   }
               }
           }
       }

       av_init_packet(&amp;l_AVPacket);
       av_init_packet(&amp;l_AVPacket_out);

       memset(l_auiInBuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);

       if (l_pCodec)
       {
           l_pAVCodecContext = avcodec_alloc_context3(l_pCodec);

           l_pParser = av_parser_init(l_pAVCodecContext->codec_id);

           if (l_pParser)
           {
               av_register_codec_parser(l_pParser->parser);
           }

           if (l_pAVCodecContext)
           {
               if (l_pCodec->capabilities &amp; CODEC_CAP_TRUNCATED)
               {
                   l_pAVCodecContext->flags |= CODEC_FLAG_TRUNCATED;
               }

               l_iResult = avcodec_open2(l_pAVCodecContext, l_pCodec, NULL);

               if (l_iResult >= 0)
               {
                   l_pFile_in = fopen(l_sFile.c_str(), "rb");

                   if (l_pFile_in)
                   {
                       l_pAVFrame = av_frame_alloc();
                       l_pAVFrameBGR = av_frame_alloc();

                       if (l_pAVFrame)
                       {
                           l_iFrameCount = 0;

                           avcodec_get_frame_defaults(l_pAVFrame);

                           while (1)
                           {
                               l_iBufLength = fread(l_auiInBuf,
                                                    1,
                                                    INBUF_SIZE,
                                                    l_pFile_in);

                               if (l_iBufLength == 0)
                               {
                                   break;
                               }
                               else
                               {
                                   l_pData = l_auiInBuf;
                                   l_iSize = l_iBufLength;

                                   while (l_iSize > 0)
                                   {
                                       if (l_pParser)
                                       {
                                           l_iParsedBytes = av_parser_parse2(
                                                       l_pParser,
                                                       l_pAVCodecContext,
                                                       &amp;l_AVPacket_out.data,
                                                       &amp;l_AVPacket_out.size,
                                                       l_pData,
                                                       l_iSize,
                                                       l_AVPacket.pts,
                                                       l_AVPacket.dts,
                                                       AV_NOPTS_VALUE);

                                           if (l_iParsedBytes &lt;= 0)
                                           {
                                               break;
                                           }

                                           l_AVPacket.pts = l_AVPacket.dts = AV_NOPTS_VALUE;
                                           l_AVPacket.pos = -1;
                                       }
                                       else
                                       {
                                           l_AVPacket_out.data = l_pData;
                                           l_AVPacket_out.size = l_iSize;
                                       }

                                       l_iDecodedBytes =
                                               avcodec_decode_video2(
                                                   l_pAVCodecContext,
                                                   l_pAVFrame,
                                                   &amp;l_iGotFrame,
                                                   &amp;l_AVPacket_out);

                                       if (l_iDecodedBytes >= 0)
                                       {
                                           if (l_iGotFrame)
                                           {
                                               l_pSWSContext = sws_getContext(
                                                           l_pAVCodecContext->width,
                                                           l_pAVCodecContext->height,
                                                           l_pAVCodecContext->pix_fmt,
                                                           l_pAVCodecContext->width,
                                                           l_pAVCodecContext->height,
                                                           AV_PIX_FMT_BGR24,
                                                           SWS_BICUBIC,
                                                           NULL,
                                                           NULL,
                                                           NULL);

                                               if (l_pSWSContext)
                                               {
                                                   l_iResult = avpicture_alloc(
                                                               reinterpret_cast(l_pAVFrameBGR),
                                                               AV_PIX_FMT_BGR24,
                                                               l_pAVFrame->width,
                                                               l_pAVFrame->height);

                                                   l_iResult = sws_scale(
                                                               l_pSWSContext,
                                                               l_pAVFrame->data,
                                                               l_pAVFrame->linesize,
                                                               0,
                                                               l_pAVCodecContext->height,
                                                               l_pAVFrameBGR->data,
                                                               l_pAVFrameBGR->linesize);

                                                   if (l_iResult > 0)
                                                   {
                                                       l_cvmImage = cv::Mat(
                                                                   l_pAVFrame->height,
                                                                   l_pAVFrame->width,
                                                                   CV_8UC3,
                                                                   l_pAVFrameBGR->data[0],
                                                               l_pAVFrameBGR->linesize[0]);

                                                       if (l_cvmImage.empty() == false)
                                                       {
                                                           cv::imshow("image", l_cvmImage);
                                                           cv::waitKey(10);
                                                       }
                                                   }
                                               }

                                               l_iFrameCount++;
                                           }
                                       }
                                       else
                                       {
                                           break;
                                       }

                                       l_pData += l_iParsedBytes;
                                       l_iSize -= l_iParsedBytes;
                                   }
                               }

                           } // end while(1).
                       }

                       fclose(l_pFile_in);
                   }
               }
           }
       }
    }
    </sstream></string></iomanip></iostream>

    EDIT : The following is the final code that solves my problem, thanks to the suggestions of Ronald.

    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>

    #include <opencv2></opencv2>opencv.hpp>

    #ifdef __cplusplus
    extern "C"
    {
    #endif // __cplusplus
    #include <libavcodec></libavcodec>avcodec.h>
    #include <libavdevice></libavdevice>avdevice.h>
    #include <libavfilter></libavfilter>avfilter.h>
    #include <libavformat></libavformat>avformat.h>
    #include <libavformat></libavformat>avio.h>
    #include <libavutil></libavutil>avutil.h>
    #include <libpostproc></libpostproc>postprocess.h>
    #include <libswresample></libswresample>swresample.h>
    #include <libswscale></libswscale>swscale.h>
    #ifdef __cplusplus
    } // end extern "C".
    #endif // __cplusplus

    void main()
    {
       AVCodec*            l_pCodec;
       AVCodecContext*     l_pAVCodecContext;
       SwsContext*         l_pSWSContext;
       AVFormatContext*    l_pAVFormatContext;
       AVFrame*            l_pAVFrame;
       AVFrame*            l_pAVFrameBGR;
       AVPacket            l_AVPacket;
       std::string         l_sFile;
       uint8_t*            l_puiBuffer;
       int                 l_iResult;
       int                 l_iFrameCount;
       int                 l_iGotFrame;
       int                 l_iDecodedBytes;
       int                 l_iVideoStreamIdx;
       int                 l_iNumBytes;
       cv::Mat             l_cvmImage;

       l_pCodec = NULL;
       l_pAVCodecContext = NULL;
       l_pSWSContext = NULL;
       l_pAVFormatContext = NULL;
       l_pAVFrame = NULL;
       l_pAVFrameBGR = NULL;
       l_puiBuffer = NULL;

       l_sFile = "myvideo.ts";

       av_register_all();

       l_iResult = avformat_open_input(&amp;l_pAVFormatContext,
                                       l_sFile.c_str(),
                                       NULL,
                                       NULL);

       if (l_iResult >= 0)
       {
           l_iResult = avformat_find_stream_info(l_pAVFormatContext, NULL);

           if (l_iResult >= 0)
           {
               for (int i=0; inb_streams; i++)
               {
                   if (l_pAVFormatContext->streams[i]->codec->codec_type ==
                           AVMEDIA_TYPE_VIDEO)
                   {
                       l_iVideoStreamIdx = i;

                       l_pAVCodecContext =
                               l_pAVFormatContext->streams[l_iVideoStreamIdx]->codec;

                       if (l_pAVCodecContext)
                       {
                           l_pCodec = avcodec_find_decoder(l_pAVCodecContext->codec_id);
                       }

                       break;
                   }
               }
           }
       }

       if (l_pCodec &amp;&amp; l_pAVCodecContext)
       {
           l_iResult = avcodec_open2(l_pAVCodecContext, l_pCodec, NULL);

           if (l_iResult >= 0)
           {
               l_pAVFrame = av_frame_alloc();
               l_pAVFrameBGR = av_frame_alloc();

               l_iNumBytes = avpicture_get_size(PIX_FMT_BGR24,
                                                l_pAVCodecContext->width,
                                                l_pAVCodecContext->height);

               l_puiBuffer = (uint8_t *)av_malloc(l_iNumBytes*sizeof(uint8_t));

               avpicture_fill((AVPicture *)l_pAVFrameBGR,
                              l_puiBuffer,
                              PIX_FMT_RGB24,
                              l_pAVCodecContext->width,
                              l_pAVCodecContext->height);

               l_pSWSContext = sws_getContext(
                           l_pAVCodecContext->width,
                           l_pAVCodecContext->height,
                           l_pAVCodecContext->pix_fmt,
                           l_pAVCodecContext->width,
                           l_pAVCodecContext->height,
                           AV_PIX_FMT_BGR24,
                           SWS_BICUBIC,
                           NULL,
                           NULL,
                           NULL);

               while (av_read_frame(l_pAVFormatContext, &amp;l_AVPacket) >= 0)
               {
                   if (l_AVPacket.stream_index == l_iVideoStreamIdx)
                   {
                       l_iDecodedBytes = avcodec_decode_video2(
                                   l_pAVCodecContext,
                                   l_pAVFrame,
                                   &amp;l_iGotFrame,
                                   &amp;l_AVPacket);

                       if (l_iGotFrame)
                       {
                           if (l_pSWSContext)
                           {
                               l_iResult = sws_scale(
                                           l_pSWSContext,
                                           l_pAVFrame->data,
                                           l_pAVFrame->linesize,
                                           0,
                                           l_pAVCodecContext->height,
                                           l_pAVFrameBGR->data,
                                           l_pAVFrameBGR->linesize);

                               if (l_iResult > 0)
                               {
                                   l_cvmImage = cv::Mat(
                                               l_pAVFrame->height,
                                               l_pAVFrame->width,
                                               CV_8UC3,
                                               l_pAVFrameBGR->data[0],
                                           l_pAVFrameBGR->linesize[0]);

                                   if (l_cvmImage.empty() == false)
                                   {
                                       cv::imshow("image", l_cvmImage);
                                       cv::waitKey(1);
                                   }
                               }
                           }

                           l_iFrameCount++;
                       }
                   }
               }
           }
       }
    }
    </sstream></string></iomanip></iostream>