/**
        \fn getPacket
*/
uint8_t ADM_audioStreamDCA::getPacket(uint8_t *obuffer,uint32_t *osize, uint32_t sizeMax,uint32_t *nbSample,uint64_t *dts)
{
#define ADM_LOOK_AHEAD DTS_HEADER_SIZE // Need 10 bytes...
uint8_t data[ADM_LOOK_AHEAD];
uint32_t offset;
ADM_DCA_INFO info;
    while(1)
    {
        // Do we have sync ?
        if(needBytes(ADM_LOOK_AHEAD)==false) 
        {
            ADM_warning("DCA: Not sync found in buffer\n");
            return false;
        }
            
        // Peek
        peek(ADM_LOOK_AHEAD,data);
        // Search start seq
        if(buffer[start]!=0x7F || buffer[start+1]!=0xFE)
        {
            read8();
            continue;
        }
        if(buffer[start+2]!=0x80 || buffer[start+3]!=0x1)
        {
            read8();
            read8();
            continue;
        }

        if(false== ADM_DCAGetInfo(buffer+start, limit-start,&info,&offset))
        {
            read8();
            read8();
            read8();
            read8();
            continue;
        }
        ADM_assert(info.frameSizeInBytes<=sizeMax);
        if(needBytes(info.frameSizeInBytes)==false)
        {
            ADM_warning("DCA: Not enough data\n");
            return false;
        }
        *osize=info.frameSizeInBytes;
        read(*osize,obuffer);
        *nbSample=info.samples;
        *dts=lastDts;
        advanceDtsBySample(*nbSample);
        return 1;

    }
}
Ejemplo n.º 2
0
uint8_t dmxAudioStream::probeAudio (void)
{
uint32_t read,offset,offset2,fq,br,chan,myPes,blocksize;          
uint8_t buffer[PROBE_SIZE];
MpegAudioInfo mpegInfo;
WAVHeader *hdr;

      for(int i=0;i<nbTrack;i++)
      {
        hdr=&(_tracks[i].wavHeader);
        // Change demuxer...

        demuxer->changePid(_tracks[i].myPid,_tracks[i].myPes);
        demuxer->setPos(0,0);
        printf("Probing track:%d, pid: %x pes:%x\n",i,_tracks[i].myPid,_tracks[i].myPes);
         //
        if(PROBE_SIZE!=(blocksize=demuxer->read(buffer,PROBE_SIZE)))
        {
           printf("DmxAudio: Reading for track %d failed (%u/%u)\n",i,blocksize,PROBE_SIZE);
           return 0;
        }
        myPes=_tracks[i].myPes;
        // Try mp2/3
        if(myPes>=0xC0 && myPes<=0xC9)
        {
                if(getMpegFrameInfo(buffer,PROBE_SIZE,&mpegInfo,NULL,&offset))
                {
                        if(getMpegFrameInfo(buffer+offset,PROBE_SIZE-offset,&mpegInfo,NULL,&offset2))
                                if(!offset2)
                                {
                                        hdr->byterate=(1000*mpegInfo.bitrate)>>3;
                                        hdr->frequency=mpegInfo.samplerate;

                                        if(mpegInfo.mode!=3) hdr->channels=2;
                                        else hdr->channels=1;

                                        if(mpegInfo.layer==3) hdr->encoding=WAV_MP3;
                                        else hdr->encoding=WAV_MP2;
                                        continue;
                                }
                }
        }
        // Try AC3
        if(myPes<9)
        {
                if(ADM_AC3GetInfo(buffer,PROBE_SIZE,&fq,&br,&chan,&offset))
                {
                        if(ADM_AC3GetInfo(buffer+offset,PROBE_SIZE-offset,&fq,&br,&chan,&offset2))
                        {
                                hdr->byterate=br; //(1000*br)>>3;
                                hdr->frequency=fq;                                
                                hdr->encoding=WAV_AC3;
                                hdr->channels=chan;
                                continue;
                        }
                }
        }

        if(myPes<=0x49 && myPes>=0x40)
        {
             
              uint32_t flags,samplerate,bitrate,framelength,syncoff,chan,nbs;
//int ADM_DCAGetInfo(uint8_t *buf, uint32_t len, uint32_t *fq, uint32_t *br, uint32_t *chan,uint32_t *syncoff,uint32_t *flags);
              if(ADM_DCAGetInfo(buffer,PROBE_SIZE,&samplerate,&bitrate,&chan,&syncoff,&flags,&nbs))
              {
                                hdr->byterate=bitrate/8;
                                hdr->frequency=samplerate;
                                hdr->encoding=WAV_DTS;
                                hdr->channels=chan;
                                continue;
               }
        }
        // Default 48khz stereo lpcm
        if(myPes>=0xA0 && myPes<0xA9)
        {
                hdr->byterate=(48000*4);
                hdr->frequency=48000;                                
                hdr->encoding=WAV_LPCM;
                hdr->channels=2;
                continue;
        }
         //AAC, can happen in TS file with H264
        if(myPes>=0xB0 && myPes<0xB9)
        {
          AacAudioInfo info;
          if(!getAACFrameInfo(buffer,blocksize, &info,NULL,&offset))
          {
            printf("\n Cannot get AAC sync info (not ADTS ?)\n");
                hdr->byterate=(128000)>>3;
                hdr->frequency=44100;
                hdr->encoding=WAV_AAC;
                hdr->channels=2;
                continue;
          }
Ejemplo n.º 3
0
/**
    \fn addAudioTrack
    \brief gather information about audio & add audio track to the list

*/
bool addAudioTrack(int pid, listOfPsAudioTracks *list, psPacketLinearTracker *p)
{

uint8_t audioBuffer[PROBE_ANALYZE_SIZE];
        uint64_t pts,dts,startAt;
        uint32_t packetSize;

        //
        int masked=pid&0xF0;
        if(masked!=MP2_AUDIO_VALUE &&  // MP2
            masked!=LPCM_AUDIO_VALUE && // PCM
            masked!=DTS_AC3_AUDIO_VALUE  // AC3 & DTS
            ) 
        {
            ADM_info("Not a type we know %x\n",(int)masked);
            return false;
        }

        // Go back where we were
        p->changePid(pid);
        p->getPacketOfType(pid,PROBE_ANALYZE_SIZE, &packetSize,&pts,&dts,audioBuffer,&startAt);
        //Realign
        p->seek(startAt,0);
        int rd=packetSize*2;
        if(rd>PROBE_ANALYZE_SIZE) rd=PROBE_ANALYZE_SIZE;
        if(!p->read(rd,audioBuffer))
        {
            ADM_info("Cannot read %d bytes of type %x\n",packetSize*2,pid);
            return false;
        }
        psAudioTrackInfo *info=new psAudioTrackInfo;
        info->esID=pid;
        uint32_t fq,br,chan,off;
        switch(pid & 0xF0)
        {
            case LPCM_AUDIO_VALUE: // LPCM
                            info->header.frequency=48000;
                            info->header.channels=2;
                            info->header.byterate=48000*4;
                            info->header.encoding=WAV_LPCM;
                            break;
            case MP2_AUDIO_VALUE: // MP2
                            {
                                if(! psCheckMp2Audio(&(info->header),audioBuffer,rd))
                                {
                                    ADM_warning("[PsProbeAudio] Failed to get info on track :%x (MP2)\n",pid);
                                    goto er;
                                }
                            }
                            break;
            case DTS_AC3_AUDIO_VALUE: // AC3 or DTS
                            if(pid>=0x8) // DTS
                            {
                                info->header.encoding=WAV_DTS;
                                ADM_DCA_INFO dcainfo;

                                if(false==ADM_DCAGetInfo(audioBuffer, rd, &dcainfo,&off))
                                {
                                        ADM_warning("[PsProbeAudio] Failed to get info on track :%x\n",pid);
                                        goto er;
                                }
                                info->header.frequency=dcainfo.frequency;
                                info->header.channels=dcainfo.channels;
                                info->header.byterate=dcainfo.bitrate/8;
                                break;
                            }else // AC3
                            {
                                info->header.encoding=WAV_AC3;
                                if(!ADM_AC3GetInfo(audioBuffer, rd, &fq, &br, &chan,&off))
                                {
                                        ADM_warning("[PsProbeAudio] Failed to get info on track :%x\n",pid);
                                        goto er;
                                }
                                info->header.frequency=fq;
                                info->header.channels=chan;
                                info->header.byterate=(br);
                                break;
                            }

            default:
                        ADM_assert(0);

        }
        list->append(info);
        return true;
er:
        delete info;
        return false;
}
Ejemplo n.º 4
0
uint8_t dmx_probePS(const char *file,  uint32_t *nbTracks,MPEG_TRACK **tracks)
{
uint8_t dummy[10];
uint64_t seen[256],abs,rel;
int     audio,video;

        // It is mpeg PS
        // Create a fake demuxer, set a probe limite and collect info for all streams found
        dmx_demuxerPS *demuxer;
        MPEG_TRACK    pseudo;

               pseudo.pes=0xea; // Hopefully not used
               pseudo.pid=0;
               demuxer=new dmx_demuxerPS(256,&pseudo,0);
               if(!demuxer->open(file))
                {
                        delete demuxer;
                        printf("Cannot open file file demuxer (%s)\n",file);
                        return 0;
                }
                DIA_StartBusy();
                demuxer->setProbeSize(MAX_PROBE);
                demuxer->read(dummy,1);
                demuxer->getStats(seen);
                demuxer->getPos( &abs,&rel);
                //abs>>=20;
                printf("Stopped at %"LLU", %"LLU" MB\n",abs,abs>>20);

                DIA_StopBusy();
        // Now analyze...
        video=0;
        // Take the first video track suitable
        for(int i=0xE0;i<0xE9;i++)
        {
                if(seen[i]>MIN_DETECT)
                {
                        video=i;
                        break;
                }
        }
        if(!video)
        {
                printf("Cannot find any video stream\n");
                delete demuxer;
                return 0;
        }
        
        audio=0;
#if 1
        for(int i=0;i<256;i++)
        {
                if(seen[i]) printf("%x: there is something %lu kb\n",i,seen[i]>>10);
        }
#endif
        // 1 count how much audio we have
        for(int i=0;i<9;i++) if(seen[i]>MIN_DETECT) audio++;
        for(int i=0xc0;i<0xc9;i++) if(seen[i]>MIN_DETECT) audio++;
        for(int i=0xA0;i<0xA9;i++) if(seen[i]>MIN_DETECT) audio++;
        for(int i=0x40;i<0x49;i++) if(seen[i]>MIN_DETECT) audio++;

        *nbTracks=audio+1;      
        *tracks=new MPEG_TRACK[*nbTracks];
        
        memset(*tracks,0,(audio+1)*sizeof(MPEG_TRACK));
        (*tracks)[0].pes=video;
        (*tracks)[0].streamType=ADM_STREAM_MPEG_VIDEO;
        audio=1;
#define DOME {(*tracks)[audio].pes=i;audio++;}
        for(int i=0;i<9;i++) if(seen[i]>MIN_DETECT) DOME;
        for(int i=0xc0;i<0xc9;i++) if(seen[i]>MIN_DETECT) DOME;
        for(int i=0xA0;i<0xA9;i++) if(seen[i]>MIN_DETECT) DOME;
        for(int i=0x40;i<0x49;i++) if(seen[i]>MIN_DETECT) DOME;

        // Now go a bit deeper and try to extract infos


        uint8_t buffer[BUFFER_SIZE];
        uint32_t read;
        uint32_t br,fq,offset,pes,chan;
        MpegAudioInfo mpegInfo;        

        for(int i=1;i<audio;i++)
        {

                pes=(*tracks)[i].pes;
                // Anything but PCM
                if((pes<0xC9 && pes>=0xc0) || ((pes<9)) || ((pes>=0x40 && pes<=0x49)))
                {
                        demuxer->changePid(0,pes);
                        demuxer->setPos(0,0);
                        read=demuxer->read(buffer,BUFFER_SIZE);
                        // We need about 5 Ko...
                        if(read>BUFFER_SIZE>>1)
                        {
                                if(pes<9)
                                {
                                        if(ADM_AC3GetInfo(buffer,read,&fq,&br,&chan,&offset))
                                        {
                                                (*tracks)[i].channels=chan;
                                                (*tracks)[i].bitrate=(8*br)/1000;
                                                (*tracks)[i].streamType=ADM_STREAM_AC3;
                                        }
                                }else
                                {

                                    if(pes>=0x40 && pes<=0x49)
                                    {
                                    uint32_t chan,samplerate,bitrate,framelength,syncoff,flags,nbs;
                                        if(ADM_DCAGetInfo(buffer,read,&samplerate,&bitrate,&chan, &syncoff,&flags,&nbs))
                                        {
                                                (*tracks)[i].channels=chan;
                                                (*tracks)[i].bitrate=bitrate;
                                                (*tracks)[i].streamType=ADM_STREAM_DTS;
                                                if(syncoff) printf("[probe] There are some %u heading bytes\n",syncoff);
                                        }

                                    }
                                    else
                                        if(getMpegFrameInfo(buffer,read,&mpegInfo,NULL,&offset))
                                        {
                                                if(mpegInfo.mode!=3)  (*tracks)[i].channels=2;
                                                         else  (*tracks)[i].channels=1;
                                                
                                                (*tracks)[i].bitrate=mpegInfo.bitrate;
                                                (*tracks)[i].streamType=ADM_STREAM_MPEG_AUDIO;
                                        }
                                }                

                        }
                }

        }
uint8_t		AVDMGenericAudioStream::getPacketDTS(uint8_t *dest, uint32_t *len,uint32_t *samples)
{
uint32_t instock,rd;
uint32_t startOffset,endOffset;
uint8_t  lock=0;
uint8_t  headerfound=0;

                ADM_assert(_wavheader->encoding==WAV_DTS);
                ADM_assert(packetTail>=packetHead);
                if(packetTail<packetHead+DTS_HEADER_SIZE)
                {
                        printf("PKTZ:DTS Buffer empty\n");
                        return 0;
                }


                uint32_t flags,sample_rate,bit_rate,syncoff,chan,nbs;
                int found=0;
                uint32_t start,size=0;

                start=packetHead;

                while(start+DTS_HEADER_SIZE<packetTail)
                {
                        if(packetBuffer[start]!=0x07f || packetBuffer[start+1]!=0xfe|| 
                            packetBuffer[start+2]!=0x80|| packetBuffer[start+3]!=0x01)
                        {	
                            if(!lock)
                            {
                                    printf("DTS: searching sync ");
                                    lock=1;
                            }
                            else
                            {
                                    printf(".");
                            }
                        start++;
                        continue;
                        }
//int ADM_DCAGetInfo(uint8_t *buf, uint32_t len, uint32_t *fq, uint32_t *br, uint32_t *chan,uint32_t *syncoff,uint32_t *flags);
                        size= ADM_DCAGetInfo (&packetBuffer[start],packetTail-start,  &sample_rate,&bit_rate, &chan,&syncoff,&flags,&nbs);
                        if(!size)
                        {
                                printf("DTS: Cannot sync\n");
                                start++;
                                continue;
                        }
                        if(size+packetHead<=packetTail)
                        {
                                // Found
                                memcpy(dest,&packetBuffer[start],size);
                                packetHead=start+size;
                                found=1;
                                break;
                        }
                        else
                        {	// not enought data left
                                headerfound=1;
                                printf("DtsPkt: Need %lu have:%lu\n",size,packetTail-packetHead);
                                break;
                        }
                }
                if(!found)
                {
                        printf("DTSpak: Cannot find packet (%lu)\n",packetTail-packetHead);
                        if(!headerfound)
                        {
                            // no header found, we can skip up to the 6 last bytes
                            uint32_t left;
                                left=packetTail-packetHead;
                                if(left>DTS_HEADER_SIZE-1) left=DTS_HEADER_SIZE-1;
                                packetHead=packetTail-left;
                                printf("DtsPak: No dts header found, purging buffer (%lu - %lu)\n",packetHead,packetTail);
                        }
                        return 0;
                }
                *len=size;
                *samples=nbs;
                return 1;
}