⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mp4util.cpp

📁 mpeg4 demux 代码
💻 CPP
字号:


      /* <LIC_AMD_STD>
 * Copyright (C) 2003-2005 Advanced Micro Devices, Inc.  All Rights Reserved.
 *
 * Unless otherwise designated in writing, this software and any related
 * documentation are the confidential proprietary information of AMD.
 * THESE MATERIALS ARE PROVIDED "AS IS" WITHOUT ANY
 * UNLESS OTHERWISE NOTED IN WRITING, EXPRESS OR IMPLIED WARRANTY OF ANY
 * KIND, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY,
 * NONINFRINGEMENT, TITLE, FITNESS FOR ANY PARTICULAR PURPOSE AND IN NO
 * EVENT SHALL AMD OR ITS LICENSORS BE LIABLE FOR ANY DAMAGES WHATSOEVER.
 *
 * AMD does not assume any responsibility for any errors which may appear
 * in the Materials nor any responsibility to support or update the
 * Materials.  AMD retains the right to modify the Materials at any time,
 * without notice, and is not obligated to provide such modified
 * Materials to you. AMD is not obligated to furnish, support, or make
 * any further information available to you.
 * </LIC_AMD_STD>  */
/* <CTL_AMD_STD>
 * </CTL_AMD_STD>  */
/* <DOC_AMD_STD>
 * </DOC_AMD_STD>  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bytestrm.h"
#include "descriptors.h"
#include "atomparse.h"
#include "mp4util.h"

#define DPRINTF(_args_)     /* MAIOSDebugPrint _args_ */
#define APIPRINTF(_args_)   /* MAIOSDebugPrint _args_ */
#define INFOPRINTF(_args_)  /* MAIOSDebugPrint _args_ */

extern CTimeToSampleBox		*pTimeToSample[NUM_TRACK_TYPES];
extern CSampleToChunkBox	*pSampleToChunk[NUM_TRACK_TYPES];
extern CChunkOffsetBox		*pCunkOffset[NUM_TRACK_TYPES];
extern CSampleSizeBox		*pSampleSize[NUM_TRACK_TYPES];

extern "C" int check_file_type(unsigned char *pBuff,long lData,FILE_BRAND *File_brand)
{
  int flags=0;
  int no=0;
  unsigned char *ptemp;
  Byte_Stream input_stream;

	ptemp = pBuff;

  while(lData >= 8)
  {
    input_stream.Initialize(ptemp, lData);
    unsigned long dwLen =input_stream.GetUINT();
    unsigned int id = input_stream.GetUINT();
    if(dwLen == 1)
    {
      // TODO: 64-bit size
      int temp = 0;
    } else if(dwLen == 0)
      break; // end of file

    switch(id)
    {
    case FOURCC('f','t','y','p'):
      {
        unsigned int brand = input_stream.GetUINT();
        switch(brand)
        {
        case FOURCC('i','s','o','m'):
          INFOPRINTF((M_TEXT("File Type : ISO Base Media\n")));
          *File_brand = eISOM;
          return 0;
     			break;
        case FOURCC('m','p','4','1'):
          *File_brand = eMP41;
          INFOPRINTF((M_TEXT("File Type : ISO/IEC 14496-1 (MPEG-4 system) v1\n")));
          return 0;
          break;
        case FOURCC('m','p','4','2'):
          *File_brand = eMP42;
          INFOPRINTF((M_TEXT("File Type : ISO/IEC 14496-1 (MPEG-4 system) v2\n")));
          return 0;
          break;
        case FOURCC('M','4','A',' '):
          INFOPRINTF((M_TEXT("File Type : Apple iTunes AAC-LC Audio\n")));
          *File_brand = eAACAudio;
          break;
        case FOURCC('m','m','p','4'):
          INFOPRINTF((M_TEXT("File Type: Mobile ISO/IEC 14496-1 (MPEG-4 system)\n")));
          break;
        default:
          *File_brand = eUNKNOWN;
          INFOPRINTF((M_TEXT("Unknown File Type\n")));
        }
      }
      if(lData >= dwLen)
      {
        lData -= dwLen;
        ptemp += dwLen;
      } else
        lData=0;
      break;
    default:
      /* id unknown, advance */
		  if(lData >= dwLen)
      {
			  lData -= dwLen;
			  ptemp += dwLen;
			} else
        lData=0;
			break;
		}
	}
  return 0;
}

static m_u32 GetChunk(long SampleNum, TRACK_TYPES index,m_u32 *chunk_sample, m_u32 *chunk)
{
  m_u32 chunk1, chunk2;
  m_u32 chunk1samples, range_samples;
  m_u32 total = 0;
  m_u32 entry_count = 0;
  m_u32 chunk2entry;

  chunk1			= 1;
  chunk1samples	= 0;
  chunk2entry		= 0;
  entry_count = pSampleToChunk[index]->GetEntryCount();

  do
  {
    chunk2 = pSampleToChunk[index]->Getfirst_chunk(chunk2entry);
    *chunk = chunk2 - chunk1;
    range_samples = *chunk * chunk1samples;

    if (SampleNum < total + range_samples)
      break;

    chunk1samples = pSampleToChunk[index]->Getsamples_per_chunk(chunk2entry);
    chunk1 = chunk2;

    if(chunk2entry < entry_count)
    {
      chunk2entry++;
      total += range_samples;
    }
  } while (chunk2entry < entry_count);

  if (chunk1samples)
    *chunk = (SampleNum - total) / chunk1samples + chunk1;
  else
    *chunk = 1;

  *chunk_sample = total + (*chunk - chunk1) * chunk1samples;
  return 0;
}

static m_u32 chunk_to_offset(TRACK_TYPES index, const m_u32 chunk)
{
  m_u32 entry_count;

  entry_count = pCunkOffset[index]->GetEntryCount();
  if ( entry_count && (chunk > entry_count))
  {
    return  pCunkOffset[index]->GetChunk_offset(entry_count - 1);
  } else
  {
    if (entry_count)
      return pCunkOffset[index]->GetChunk_offset(chunk - 1);
    else
      return 8;
  }
  return 0;
}

static m_u32 sample_range_size(TRACK_TYPES index, const m_u32 chunk_sample, const m_u32 sample)
{
  m_u32 total,i,samplesize;

  samplesize = pSampleSize[index]->GetSampleSize();

  if (samplesize)
  {
    return (sample - chunk_sample) * samplesize;
  } else
  {
    if (sample >= pSampleSize[index]->GetSampleCount())
      return 0;

    for(i = chunk_sample, total = 0; i < sample; i++)
      total += pSampleSize[index]->GetEntrySize(i);
  }
  return total;
}

static m_u32 sample_to_offset(long SampleNum, TRACK_TYPES index)
{
  m_u32 chunk, chunk_sample;
  m_u32 chunk_offset1, chunk_offset2;

  GetChunk(SampleNum, index,&chunk_sample, &chunk);
  chunk_offset1 = chunk_to_offset(index, chunk);
  chunk_offset2 = chunk_offset1 + sample_range_size(index, chunk_sample, SampleNum);

  return chunk_offset2;
}

m_u32 GetSampleSize(long SampleNum, TRACK_TYPES index)
{
  if (pSampleSize[index]->GetSampleSize())
    return(pSampleSize[index]->GetSampleSize());
  else
    return(pSampleSize[index]->GetEntrySize(SampleNum));
}

m_u32 GetSampleOffset(long SampleNum, TRACK_TYPES index, m_u32 *pos)
{
  m_u32 offset;

  offset = sample_to_offset(SampleNum,index);
  *pos = offset;

  return 0;
}

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -