Recherche avancée

Médias (1)

Mot : - Tags -/lev manovitch

Autres articles (58)

  • Le profil des utilisateurs

    12 avril 2011, par

    Chaque utilisateur dispose d’une page de profil lui permettant de modifier ses informations personnelle. Dans le menu de haut de page par défaut, un élément de menu est automatiquement créé à l’initialisation de MediaSPIP, visible uniquement si le visiteur est identifié sur le site.
    L’utilisateur a accès à la modification de profil depuis sa page auteur, un lien dans la navigation "Modifier votre profil" est (...)

  • Configurer la prise en compte des langues

    15 novembre 2010, par

    Accéder à la configuration et ajouter des langues prises en compte
    Afin de configurer la prise en compte de nouvelles langues, il est nécessaire de se rendre dans la partie "Administrer" du site.
    De là, dans le menu de navigation, vous pouvez accéder à une partie "Gestion des langues" permettant d’activer la prise en compte de nouvelles langues.
    Chaque nouvelle langue ajoutée reste désactivable tant qu’aucun objet n’est créé dans cette langue. Dans ce cas, elle devient grisée dans la configuration et (...)

  • Sélection de projets utilisant MediaSPIP

    29 avril 2011, par

    Les exemples cités ci-dessous sont des éléments représentatifs d’usages spécifiques de MediaSPIP pour certains projets.
    Vous pensez avoir un site "remarquable" réalisé avec MediaSPIP ? Faites le nous savoir ici.
    Ferme MediaSPIP @ Infini
    L’Association Infini développe des activités d’accueil, de point d’accès internet, de formation, de conduite de projets innovants dans le domaine des Technologies de l’Information et de la Communication, et l’hébergement de sites. Elle joue en la matière un rôle unique (...)

Sur d’autres sites (3835)

  • H.264 muxed to MP4 using libavformat not playing back

    14 mai 2015, par Brad Mitchell

    I am trying to mux H.264 data into a MP4 file. There appear to be no errors in saving this H.264 Annex B data out to an MP4 file, but the file fails to playback.

    I’ve done a binary comparison on the files and the issue seems to be somewhere in what is being written to the footer (trailer) of the MP4 file.

    I suspect it has to be something with the way the stream is being created or something.

    Init :

    AVOutputFormat* fmt = av_guess_format( 0, "out.mp4", 0 );
    oc = avformat_alloc_context();
    oc->oformat = fmt;
    strcpy(oc->filename, filename);

    Part of this prototype app I have is creating a png file for each IFrame. So when the first IFrame is encountered, I create the video stream and write the av header etc :

    void addVideoStream(AVCodecContext* decoder)
    {
       videoStream = av_new_stream(oc, 0);
       if (!videoStream)
       {
            cout << "ERROR creating video stream" << endl;
            return;        
       }
       vi = videoStream->index;    
       videoContext = videoStream->codec;      
       videoContext->codec_type = AVMEDIA_TYPE_VIDEO;
       videoContext->codec_id = decoder->codec_id;
       videoContext->bit_rate = 512000;
       videoContext->width = decoder->width;
       videoContext->height = decoder->height;
       videoContext->time_base.den = 25;
       videoContext->time_base.num = 1;    
       videoContext->gop_size = decoder->gop_size;
       videoContext->pix_fmt = decoder->pix_fmt;      

       if (oc->oformat->flags & AVFMT_GLOBALHEADER)
           videoContext->flags |= CODEC_FLAG_GLOBAL_HEADER;

       av_dump_format(oc, 0, filename, 1);

       if (!(oc->oformat->flags & AVFMT_NOFILE))
       {
           if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) {
           cout << "Error opening file" << endl;
       }
       avformat_write_header(oc, NULL);
    }

    I write packets out :

    unsigned char* data = block->getData();
    unsigned char videoFrameType = data[4];
    int dataLen = block->getDataLen();

    // store pps
    if (videoFrameType == 0x68)
    {
       if (ppsFrame != NULL)
       {
           delete ppsFrame; ppsFrameLength = 0; ppsFrame = NULL;
       }
       ppsFrameLength = block->getDataLen();
       ppsFrame = new unsigned char[ppsFrameLength];
       memcpy(ppsFrame, block->getData(), ppsFrameLength);
    }
    else if (videoFrameType == 0x67)
    {
       // sps
       if (spsFrame != NULL)
       {
           delete spsFrame; spsFrameLength = 0; spsFrame = NULL;
    }
       spsFrameLength = block->getDataLen();
       spsFrame = new unsigned char[spsFrameLength];
       memcpy(spsFrame, block->getData(), spsFrameLength);                
    }                                          

    if (videoFrameType == 0x65 || videoFrameType == 0x41)
    {
       videoFrameNumber++;
    }
    if (videoFrameType == 0x65)
    {
       decodeIFrame(videoFrameNumber, spsFrame, spsFrameLength, ppsFrame, ppsFrameLength, data, dataLen);
    }

    if (videoStream != NULL)
    {
       AVPacket pkt = { 0 };
       av_init_packet(&pkt);
       pkt.stream_index = vi;
       pkt.flags = 0;                      
       pkt.pts = pkt.dts = 0;                                  

       if (videoFrameType == 0x65)
       {
           // combine the SPS PPS & I frames together
           pkt.flags |= AV_PKT_FLAG_KEY;                                                  
           unsigned char* videoFrame = new unsigned char[spsFrameLength+ppsFrameLength+dataLen];
           memcpy(videoFrame, spsFrame, spsFrameLength);
           memcpy(&videoFrame[spsFrameLength], ppsFrame, ppsFrameLength);
           memcpy(&videoFrame[spsFrameLength+ppsFrameLength], data, dataLen);

           // overwrite the start code (00 00 00 01 with a 32-bit length)
           setLength(videoFrame, spsFrameLength-4);
           setLength(&videoFrame[spsFrameLength], ppsFrameLength-4);
           setLength(&videoFrame[spsFrameLength+ppsFrameLength], dataLen-4);
           pkt.size = dataLen + spsFrameLength + ppsFrameLength;
           pkt.data = videoFrame;
           av_interleaved_write_frame(oc, &pkt);
           delete videoFrame; videoFrame = NULL;
       }
       else if (videoFrameType != 0x67 && videoFrameType != 0x68)
       {  
           // Send other frames except pps & sps which are caught and stored                  
           pkt.size = dataLen;
           pkt.data = data;
           setLength(data, dataLen-4);                    
           av_interleaved_write_frame(oc, &pkt);
       }

    Finally to close the file off :

    av_write_trailer(oc);
    int i = 0;
    for (i = 0; i < oc->nb_streams; i++)
    {
       av_freep(&oc->streams[i]->codec);
       av_freep(&oc->streams[i]);      
    }

    if (!(oc->oformat->flags & AVFMT_NOFILE))
    {
       avio_close(oc->pb);
    }
    av_free(oc);

    If I take the H.264 data alone and convert it :

    ffmpeg -i recording.h264 -vcodec copy recording.mp4

    All but the "footer" of the files are the same.

    Output from my program :
    readrec recording.tcp out.mp4
    ** START * 01-03-2013 14:26:01 180000
    Output #0, mp4, to ’out.mp4’ :
    Stream #0:0 : Video : h264, yuv420p, 352x288, q=2-31, 512 kb/s, 90k tbn, 25 tbc
    * END ** 01-03-2013 14:27:01 102000
    Wrote 1499 video frames.

    If I try to convert using ffmpeg the MP4 file created using CODE :

    ffmpeg -i out.mp4 -vcodec copy out2.mp4
    ffmpeg version 0.11.1 Copyright (c) 2000-2012 the FFmpeg developers
         built on Mar  7 2013 12:49:22 with suncc 0x5110
         configuration: --extra-cflags=-KPIC -g --disable-mmx
         --disable-protocol=udp --disable-encoder=nellymoser --cc=cc --cxx=CC
    libavutil      51. 54.100 / 51. 54.100
    libavcodec     54. 23.100 / 54. 23.100
    libavformat    54.  6.100 / 54.  6.100
    libavdevice    54.  0.100 / 54.  0.100
    libavfilter     2. 77.100 /  2. 77.100
    libswscale      2.  1.100 /  2.  1.100
    libswresample   0. 15.100 /  0. 15.100
    h264 @ 12eaac0] no frame!
       Last message repeated 1 times
    [h264 @ 12eaac0] slice type too large (0) at 0 0
    [h264 @ 12eaac0] decode_slice_header error
    [h264 @ 12eaac0] no frame!
       Last message repeated 23 times
    [h264 @ 12eaac0] slice type too large (0) at 0 0
    [h264 @ 12eaac0] decode_slice_header error
    [h264 @ 12eaac0] no frame!
       Last message repeated 74 times
    [h264 @ 12eaac0] slice type too large (0) at 0 0
    [h264 @ 12eaac0] decode_slice_header error
    [h264 @ 12eaac0] no frame!
       Last message repeated 64 times
    [h264 @ 12eaac0] slice type too large (0) at 0 0
    [h264 @ 12eaac0] decode_slice_header error
    [h264 @ 12eaac0] no frame!
       Last message repeated 34 times
    [h264 @ 12eaac0] slice type too large (0) at 0 0
    [h264 @ 12eaac0] decode_slice_header error
    [h264 @ 12eaac0] no frame!
       Last message repeated 49 times
    [h264 @ 12eaac0] slice type too large (0) at 0 0
    [h264 @ 12eaac0] decode_slice_header error
    [h264 @ 12eaac0] no frame!
       Last message repeated 24 times
    [h264 @ 12eaac0] Partitioned H.264 support is incomplete
    [h264 @ 12eaac0] no frame!
       Last message repeated 23 times
    [h264 @ 12eaac0] sps_id out of range
    [h264 @ 12eaac0] no frame!
       Last message repeated 148 times
    [h264 @ 12eaac0] sps_id (32) out of range
       Last message repeated 1 times
    [h264 @ 12eaac0] no frame!
       Last message repeated 33 times
    [h264 @ 12eaac0] slice type too large (0) at 0 0
    [h264 @ 12eaac0] decode_slice_header error
    [h264 @ 12eaac0] no frame!
       Last message repeated 128 times
    [h264 @ 12eaac0] sps_id (32) out of range
       Last message repeated 1 times
    [h264 @ 12eaac0] no frame!
       Last message repeated 3 times
    [h264 @ 12eaac0] slice type too large (0) at 0 0
    [h264 @ 12eaac0] decode_slice_header error
    [h264 @ 12eaac0] no frame!
       Last message repeated 3 times
    [h264 @ 12eaac0] slice type too large (0) at 0 0
    [h264 @ 12eaac0] decode_slice_header error
    [h264 @ 12eaac0] no frame!
       Last message repeated 309 times
    [h264 @ 12eaac0] sps_id (32) out of range
       Last message repeated 1 times
    [h264 @ 12eaac0] no frame!
       Last message repeated 192 times
    [h264 @ 12eaac0] Partitioned H.264 support is incomplete
    [h264 @ 12eaac0] no frame!
       Last message repeated 73 times
    [h264 @ 12eaac0] sps_id (32) out of range
       Last message repeated 1 times
    [h264 @ 12eaac0] no frame!
       Last message repeated 99 times
    [h264 @ 12eaac0] sps_id (32) out of range
       Last message repeated 1 times
    [h264 @ 12eaac0] no frame!
       Last message repeated 197 times
    [mov,mp4,m4a,3gp,3g2,mj2 @ 12e3100] decoding for stream 0 failed
    [mov,mp4,m4a,3gp,3g2,mj2 @ 12e3100] Could not find codec parameters
    (Video: h264 (avc1 / 0x31637661), 393539 kb/s)
    out.mp4: could not find codec parameters

    I really do not know where the issue is, except it has to be something to do with the way the streams are being set up. I’ve looked at bits of code from where other people are doing a similar thing, and tried to use this advice in setting up the streams, but to no avail !


    The final code which gave me a H.264/AAC muxed (synced) file is as follows. First a bit of background information. The data is coming from an IP camera. The data is presented via a 3rd party API as video/audio packets. The video packets are presented as the RTP payload data (no header) and consist of NALU’s that are reconstructed and converted to H.264 video in Annex B format. AAC audio is presented as raw AAC and is converted to adts format to enable playback. These packets have been put into a bitstream format that allows the transmission of the timestamp (64 bit milliseconds since Jan 1 1970) along with a few other things.

    This is more or less a prototype and is not clean in any respects. It probably leaks bad. I do however, hope this helps anyone else out trying to achieve something similar to what I am.

    Globals :

    AVFormatContext* oc = NULL;
    AVCodecContext* videoContext = NULL;
    AVStream* videoStream = NULL;
    AVCodecContext* audioContext = NULL;
    AVStream* audioStream = NULL;
    AVCodec* videoCodec = NULL;
    AVCodec* audioCodec = NULL;
    int vi = 0;  // Video stream
    int ai = 1;  // Audio stream

    uint64_t firstVideoTimeStamp = 0;
    uint64_t firstAudioTimeStamp = 0;
    int audioStartOffset = 0;

    char* filename = NULL;

    Boolean first = TRUE;

    int videoFrameNumber = 0;
    int audioFrameNumber = 0;

    Main :

    int main(int argc, char* argv[])
    {
       if (argc != 3)
       {  
           cout &lt;&lt; argv[0] &lt;&lt; " <stream playback="playback" file="file"> <output mp4="mp4" file="file">" &lt;&lt; endl;
           return 0;
       }
       char* input_stream_file = argv[1];
       filename = argv[2];

       av_register_all();    

       fstream inFile;
       inFile.open(input_stream_file, ios::in);

       // Used to store the latest pps &amp; sps frames
       unsigned char* ppsFrame = NULL;
       int ppsFrameLength = 0;
       unsigned char* spsFrame = NULL;
       int spsFrameLength = 0;

       // Setup MP4 output file
       AVOutputFormat* fmt = av_guess_format( 0, filename, 0 );
       oc = avformat_alloc_context();
       oc->oformat = fmt;
       strcpy(oc->filename, filename);

       // Setup the bitstream filter for AAC in adts format.  Could probably also achieve
       // this by stripping the first 7 bytes!
       AVBitStreamFilterContext* bsfc = av_bitstream_filter_init("aac_adtstoasc");
       if (!bsfc)
       {      
           cout &lt;&lt; "Error creating adtstoasc filter" &lt;&lt; endl;
           return -1;
       }

       while (inFile.good())
       {
           TcpAVDataBlock* block = new TcpAVDataBlock();
           block->readStruct(inFile);
           DateTime dt = block->getTimestampAsDateTime();
           switch (block->getPacketType())
           {
               case TCP_PACKET_H264:
               {      
                   if (firstVideoTimeStamp == 0)
                       firstVideoTimeStamp = block->getTimeStamp();
                   unsigned char* data = block->getData();
                   unsigned char videoFrameType = data[4];
                   int dataLen = block->getDataLen();

                   // pps
                   if (videoFrameType == 0x68)
                   {
                       if (ppsFrame != NULL)
                       {
                           delete ppsFrame; ppsFrameLength = 0;
                           ppsFrame = NULL;
                       }
                       ppsFrameLength = block->getDataLen();
                       ppsFrame = new unsigned char[ppsFrameLength];
                       memcpy(ppsFrame, block->getData(), ppsFrameLength);
                   }
                   else if (videoFrameType == 0x67)
                   {
                       // sps
                       if (spsFrame != NULL)
                       {
                           delete spsFrame; spsFrameLength = 0;
                           spsFrame = NULL;
                       }
                       spsFrameLength = block->getDataLen();
                       spsFrame = new unsigned char[spsFrameLength];
                       memcpy(spsFrame, block->getData(), spsFrameLength);                  
                   }                                          

                   if (videoFrameType == 0x65 || videoFrameType == 0x41)
                   {
                       videoFrameNumber++;
                   }
                   // Extract a thumbnail for each I-Frame
                   if (videoFrameType == 0x65)
                   {
                       decodeIFrame(h264, spsFrame, spsFrameLength, ppsFrame, ppsFrameLength, data, dataLen);
                   }
                   if (videoStream != NULL)
                   {
                       AVPacket pkt = { 0 };
                       av_init_packet(&amp;pkt);
                       pkt.stream_index = vi;
                       pkt.flags = 0;          
                       pkt.pts = videoFrameNumber;
                       pkt.dts = videoFrameNumber;          
                       if (videoFrameType == 0x65)
                       {
                           pkt.flags = 1;                          

                           unsigned char* videoFrame = new unsigned char[spsFrameLength+ppsFrameLength+dataLen];
                           memcpy(videoFrame, spsFrame, spsFrameLength);
                           memcpy(&amp;videoFrame[spsFrameLength], ppsFrame, ppsFrameLength);

                           memcpy(&amp;videoFrame[spsFrameLength+ppsFrameLength], data, dataLen);
                           pkt.data = videoFrame;
                           av_interleaved_write_frame(oc, &amp;pkt);
                           delete videoFrame; videoFrame = NULL;
                       }
                       else if (videoFrameType != 0x67 &amp;&amp; videoFrameType != 0x68)
                       {                      
                           pkt.size = dataLen;
                           pkt.data = data;
                           av_interleaved_write_frame(oc, &amp;pkt);
                       }                      
                   }
                   break;
               }

           case TCP_PACKET_AAC:

               if (firstAudioTimeStamp == 0)
               {
                   firstAudioTimeStamp = block->getTimeStamp();
                   uint64_t millseconds_difference = firstAudioTimeStamp - firstVideoTimeStamp;
                   audioStartOffset = millseconds_difference * 16000 / 1000;
                   cout &lt;&lt; "audio offset: " &lt;&lt; audioStartOffset &lt;&lt; endl;
               }

               if (audioStream != NULL)
               {
                   AVPacket pkt = { 0 };
                   av_init_packet(&amp;pkt);
                   pkt.stream_index = ai;
                   pkt.flags = 1;          
                   pkt.pts = audioFrameNumber*1024;
                   pkt.dts = audioFrameNumber*1024;
                   pkt.data = block->getData();
                   pkt.size = block->getDataLen();
                   pkt.duration = 1024;

                   AVPacket newpacket = pkt;                      
                   int rc = av_bitstream_filter_filter(bsfc, audioContext,
                       NULL,
                       &amp;newpacket.data, &amp;newpacket.size,
                       pkt.data, pkt.size,
                       pkt.flags &amp; AV_PKT_FLAG_KEY);

                   if (rc >= 0)
                   {
                       //cout &lt;&lt; "Write audio frame" &lt;&lt; endl;
                       newpacket.pts = audioFrameNumber*1024;
                       newpacket.dts = audioFrameNumber*1024;
                       audioFrameNumber++;
                       newpacket.duration = 1024;                  

                       av_interleaved_write_frame(oc, &amp;newpacket);
                       av_free_packet(&amp;newpacket);
                   }  
                   else
                   {
                       cout &lt;&lt; "Error filtering aac packet" &lt;&lt; endl;

                   }
               }
               break;

           case TCP_PACKET_START:
               break;

           case TCP_PACKET_END:
               break;
           }
           delete block;
       }
       inFile.close();

       av_write_trailer(oc);
       int i = 0;
       for (i = 0; i &lt; oc->nb_streams; i++)
       {
           av_freep(&amp;oc->streams[i]->codec);
           av_freep(&amp;oc->streams[i]);      
       }

       if (!(oc->oformat->flags &amp; AVFMT_NOFILE))
       {
           avio_close(oc->pb);
       }

       av_free(oc);

       delete spsFrame; spsFrame = NULL;
       delete ppsFrame; ppsFrame = NULL;

       cout &lt;&lt; "Wrote " &lt;&lt; videoFrameNumber &lt;&lt; " video frames." &lt;&lt; endl;

       return 0;
    }
    </output></stream>

    The stream stream/codecs are added and the header is created in a function called addVideoAndAudioStream(). This function is called from decodeIFrame() so there are a few assumptions (which aren’t necessarily good)
    1. A video packet comes first
    2. AAC is present

    The decodeIFrame was kind of a separate prototype by where I was creating a thumbnail for each I Frame. The code to generate thumbnails was from : https://gnunet.org/svn/Extractor/src/plugins/thumbnailffmpeg_extractor.c

    The decodeIFrame function passes an AVCodecContext into addVideoAudioStream :

    void addVideoAndAudioStream(AVCodecContext* decoder = NULL)
    {
       videoStream = av_new_stream(oc, 0);
       if (!videoStream)
       {
           cout &lt;&lt; "ERROR creating video stream" &lt;&lt; endl;
           return;      
       }
       vi = videoStream->index;  
       videoContext = videoStream->codec;      
       videoContext->codec_type = AVMEDIA_TYPE_VIDEO;
       videoContext->codec_id = decoder->codec_id;
       videoContext->bit_rate = 512000;
       videoContext->width = decoder->width;
       videoContext->height = decoder->height;
       videoContext->time_base.den = 25;
       videoContext->time_base.num = 1;
       videoContext->gop_size = decoder->gop_size;
       videoContext->pix_fmt = decoder->pix_fmt;      

       audioStream = av_new_stream(oc, 1);
       if (!audioStream)
       {
           cout &lt;&lt; "ERROR creating audio stream" &lt;&lt; endl;
           return;
       }
       ai = audioStream->index;
       audioContext = audioStream->codec;
       audioContext->codec_type = AVMEDIA_TYPE_AUDIO;
       audioContext->codec_id = CODEC_ID_AAC;
       audioContext->bit_rate = 64000;
       audioContext->sample_rate = 16000;
       audioContext->channels = 1;

       if (oc->oformat->flags &amp; AVFMT_GLOBALHEADER)
       {
           videoContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
           audioContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
       }

       av_dump_format(oc, 0, filename, 1);

       if (!(oc->oformat->flags &amp; AVFMT_NOFILE))
       {
           if (avio_open(&amp;oc->pb, filename, AVIO_FLAG_WRITE) &lt; 0) {
               cout &lt;&lt; "Error opening file" &lt;&lt; endl;
           }
       }

       avformat_write_header(oc, NULL);
    }

    As far as I can tell, a number of assumptions didn’t seem to matter, for example :
    1. Bit Rate. The actual video bit rate was 262k whereas I specified 512kbit
    2. AAC channels. I specified mono, although the actual output was Stereo from memory

    You would still need to know what the frame rate (time base) is for the video & audio.

    Contrary to a lot of other examples, when setting pts & dts on the video packets, it was not playable. I needed to know the time base (25fps) and then set the pts & dts according to that time base, i.e. first frame = 0 (PPS, SPS, I), second frame = 1 (intermediate frame, whatever its called ;)).

    AAC I also had to make the assumption that it was 16000 hz. 1024 samples per AAC packet (You can also have AAC @ 960 samples I think) to determine the audio "offset". I added this to the pts & dts. So the pts/dts are the sample number that it is to played back at. You also need to make sure that the duration of 1024 is set in the packet before writing also.

    I have found additionally today that Annex B isn’t really compatible with any other player so AVCC format should really be used.

    These URLS helped :
    Problem to Decode H264 video over RTP with ffmpeg (libavcodec)
    http://aviadr1.blogspot.com.au/2010/05/h264-extradata-partially-explained-for.html

    When constructing the video stream, I filled out the extradata & extradata_size :

    // Extradata contains PPS &amp; SPS for AVCC format
    int extradata_len = 8 + spsFrameLen-4 + 1 + 2 + ppsFrameLen-4;
    videoContext->extradata = (uint8_t*)av_mallocz(extradata_len);
    videoContext->extradata_size = extradata_len;
    videoContext->extradata[0] = 0x01;
    videoContext->extradata[1] = spsFrame[4+1];
    videoContext->extradata[2] = spsFrame[4+2];
    videoContext->extradata[3] = spsFrame[4+3];
    videoContext->extradata[4] = 0xFC | 3;
    videoContext->extradata[5] = 0xE0 | 1;
    int tmp = spsFrameLen - 4;
    videoContext->extradata[6] = (tmp >> 8) &amp; 0x00ff;
    videoContext->extradata[7] = tmp &amp; 0x00ff;
    int i = 0;
    for (i=0;iextradata[8+i] = spsFrame[4+i];
    videoContext->extradata[8+tmp] = 0x01;
    int tmp2 = ppsFrameLen-4;  
    videoContext->extradata[8+tmp+1] = (tmp2 >> 8) &amp; 0x00ff;
    videoContext->extradata[8+tmp+2] = tmp2 &amp; 0x00ff;
    for (i=0;iextradata[8+tmp+3+i] = ppsFrame[4+i];

    When writing out the frames, don’t prepend the SPS & PPS frames, just write out the I Frame & P frames. In addition, replace the Annex B start code contained in the first 4 bytes (0x00 0x00 0x00 0x01) with the size of the I/P frame.

  • Encoder/Decoder PCM to AMR Android

    2 mars 2013, par Syred

    I've been looking for a while now for any java library that allows me to encode and decode a PCM-AMR audio stream that is sent through a TCP socket connection. Without having to use Android's JNI.

    Is there anything that can help me ?

    In the worst case scenario. How can I do it using any C++ library with JNI ? (any reference of how to use ffmpeg with JNI will be appreciated)

    Hope you can help me.

  • Tour of Part of the VP8 Process

    18 novembre 2010, par Multimedia Mike — VP8

    My toy VP8 encoder outputs a lot of textual data to illustrate exactly what it’s doing. For those who may not be exactly clear on how this or related algorithms operate, this may prove illuminating.

    Let’s look at subblock 0 of macroblock 0 of a luma plane :

     subblock 0 (original)
      92  91  89  86
      91  90  88  86
      89  89  89  88
      89  87  88  93
    

    Since it’s in the top-left corner of the image to be encoded, the phantom samples above and to the left are implicitly 128 for the purpose of intra prediction (in the VP8 algorithm).

     subblock 0 (original)
         128 128 128 128
     128  92  91  89  86
     128  91  90  88  86
     128  89  89  89  88
     128  89  87  88  93
    


    Using the 4×4 DC prediction mode means averaging the 4 top predictors and 4 left predictors. So, the predictor is 128. Subtract this from each element of the subblock :

     subblock 0, predictor removed
     -36 -37 -39 -42
     -37 -38 -40 -42
     -39 -39 -39 -40
     -39 -41 -40 -35
    

    Next, run the subblock through the forward transform :

     subblock 0, transformed
     -312   7   1   0
        1  12  -5   2
        2  -3   3  -1
        1   0  -2   1
    

    Quantize (integer divide) each element ; the DC (first element) and AC (rest of the elements) quantizers are both 4 :

     subblock 0, quantized
     -78   1   0   0
       0   3  -1   0
       0   0   0   0
       0   0   0   0
    

    The above block contains the coefficients that are actually transmitted (zigzagged and entropy-encoded) through the bitstream and decoded on the other end.

    The decoding process looks something like this– after the same coefficients are decoded and rearranged, they are dequantized (multiplied) by the original quantizers :

     subblock 0, dequantized
     -312   4   0   0
        0  12  -4   0
        0   0   0   0
        0   0   0   0
    

    Note that these coefficients are not exactly the same as the original, pre-quantized coefficients. This is a large part of where the “lossy” in “lossy video compression” comes from.

    Next, the decoder generates a base predictor subblock. In this case, it’s all 128 (DC prediction for top-left subblock) :

     subblock 0, predictor
      128 128 128 128
      128 128 128 128
      128 128 128 128
      128 128 128 128
    

    Finally, the dequantized coefficients are shoved through the inverse transform and added to the base predictor block :

     subblock 0, reconstructed
      91  91  89  85
      90  90  89  87
      89  88  89  90
      88  88  89  92
    

    Again, not exactly the same as the original block, but an incredible facsimile thereof.

    Note that this decoding-after-encoding demonstration is not merely pedagogical– the encoder has to decode the subblock because the encoding of successive subblocks may depend on this subblock. The encoder can’t rely on the original representation of the subblock because the decoder won’t have that– it will have the reconstructed block.

    For example, here’s the next subblock :

     subblock 1 (original)
      84  84  87  90
      85  85  86  93
      86  83  83  89
      91  85  84  87
    

    Let’s assume DC prediction once more. The 4 top predictors are still all 128 since this subblock lies along the top row. However, the 4 left predictors are the right edge of the subblock reconstructed in the previous example :

     subblock 1 (original)
        128 128 128 128
     85  84  84  87  90
     87  85  85  86  93
     90  86  83  83  89
     92  91  85  84  87
    

    The DC predictor is computed as (128 + 128 + 128 + 128 + 85 + 87 + 90 + 92 + 4) / 8 = 108 (the extra +4 is for rounding considerations). (Note that in this case, using the original subblock’s right edge would also have resulted in 108, but that’s beside the point.)

    Continuing through the same process as in subblock 0 :

     subblock 1, predictor removed
     -24 -24 -21 -18
     -23 -23 -22 -15
     -22 -25 -25 -19
     -17 -23 -24 -21
    

    subblock 1, transformed
    -173 -9 14 -1
    2 -11 -4 0
    1 6 -2 3
    -5 1 0 1

    subblock 1, quantized
    -43 -2 3 0
    0 -2 -1 0
    0 1 0 0
    -1 0 0 0

    subblock 1, dequantized
    -172 -8 12 0
    0 -8 -4 0
    0 4 0 0
    -4 0 0 0

    subblock 1, predictor
    108 108 108 108
    108 108 108 108
    108 108 108 108
    108 108 108 108

    subblock 1, reconstructed
    84 84 87 89
    86 85 87 91
    86 83 84 89
    90 85 84 88

    I hope this concrete example (straight from a working codec) clarifies this part of the VP8 process.