📄 maimp3enc.c
字号:
/* <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> * Filename: maiMP3ENC.c - MPEG Audio decoder interfaces for MAIengine * </DOC_AMD_STD> */#if defined(WIN32) || defined(UNDER_CE) #include <windows.h>#else #include <fcntl.h> #include <sys/mman.h>#endif#include <stdio.h>#include <stdlib.h>//#include "mp3enc.h"#include "mai_osal.h" /* needed for threads */#include "mai_component.h" #include "mai_compbase.h"#include "audio_change_info.h"#define DPRINTF(_args_) printf _args_ #define APIPRINTF(_args_) printf _args_ #define ERRORPRINTF(_args_) printf _args_ //#define DPRINTF(_args_) /* printf _args_ *///#define APIPRINTF(_args_) /* printf _args_ *///#define ERRORPRINTF(_args_) /* printf _args_ */#define DEFAULT_INBUFSIZE 8*1024#define DEFAULT_INBUFCOUNT 64//#define DUMP_TO_FILE /* Decoder State Definitions: MAI_RUNSTATE_NONE - decoder not initialized MAI_RUNSTATE_INIT - decoder initialized, decodes not started yet MAI_RUNSTATE_PLAYING - decoder initialized, decodes active MAI_RUNSTATE_PAUSED - decoder initialized, decodes paused MAI_RUNSTATE_DESTROYING - MAICompEnd() in progress - destroying decoder*//* Memory Mapped Registers for board */#define DB1200_PHYS_ADDR 0x19800000#define PB1200_PHYS_ADDR 0x0D800000#if defined(WIN32)static int find_board(unsigned int addr, short match, short mask) { return 1;}#elsestatic int find_board(unsigned int addr, short match, short mask) { int result; int fmem = 0; short *mmap_board = NULL; /* map the device */ /* Open /dev/mem for writing physical memory */ fmem = open("/dev/mem", O_RDWR); if (fmem != 0) { mmap_board = (short *)mmap(0, sizeof(int), PROT_READ, MAP_SHARED, fmem, addr); } else { printf("/dev/mem cannot be opened\n"); return 0; } if (mmap_board == NULL) { printf("/dev/mem cannot be mapped\n"); close(fmem); return 0; } /* check the board register to see if it matches */ //printf("mmap_board match val %hx, result %hx\n", match, (*mmap_board & mask) == match); result = (*mmap_board & mask) == match; if (mmap_board) munmap((void *)mmap_board, sizeof(int)); if (fmem!=0) { close(fmem); /*compile error */ fmem = 0; } mmap_board = NULL; return result;}#endifstatic int is_dev_platform(void) { if (find_board(DB1200_PHYS_ADDR, (short)0x0C00, (short)0xFE00)) { return 1; } else { if (find_board(PB1200_PHYS_ADDR, (short)0x0A00, (short)0xFE00)) { return 1; } } return 0; // no dev board found}/* Our component's private processor variables are stored here */typedef struct ProcessorInfo_s { unsigned int m_bNewFormat;// struct MP3ENC_INFO encInfo;// struct MP3_ENCODER *pmp3;// struct MP3ENC_CONFIG config; unsigned char *outputbuffer;} ProcessorInfo_t;static MAIStatus_e open_mp3enc(MAICompHandle_t hComp){ ProcessorInfo_t *pPInfo=(ProcessorInfo_t *)HCOMP_TO_USERINFO(hComp); APIPRINTF(("MP3ENC: open_mp3enc() Init codec\n")); pPInfo->m_bNewFormat = 1;// pPInfo->pmp3 = NULL; if (pPInfo->outputbuffer != NULL) free(pPInfo->outputbuffer); pPInfo->outputbuffer = NULL; if (!is_dev_platform()) { return MAI_STATUS_OK; // leaving things in a bad state } pPInfo->outputbuffer = malloc(1152 * 4); if (pPInfo->outputbuffer == NULL) return MAI_STATUS_MEMORY; else return MAI_STATUS_OK;}MAIStatus_e _startprocessing(MAICompHandle_t hComp){ MAIStatus_e eStatus = MAI_STATUS_OK; APIPRINTF(("MP3ENC: _startprocessing() enter\n")); if (!MAICompBase_IsState(hComp, MAI_RUNSTATE_NONE)) eStatus=MAI_STATUS_WRONGSTATE; else { APIPRINTF(("MP3ENC: _startprocessing() Init codec\n")); open_mp3enc(hComp); } if (eStatus==MAI_STATUS_OK) { MAICompHandleInfo_t *pCompInfo=HCOMP_TO_HANDLEINFO(hComp); /* call default StartProcessing */ MAICompBase_StartProcessing(hComp); } APIPRINTF(("MP3ENC: _startprocessing() exit: status=0x%X\n", eStatus)); return eStatus;}static void close_mp3enc(MAICompHandle_t hComp){ ProcessorInfo_t *pPInfo=(ProcessorInfo_t *)HCOMP_TO_USERINFO(hComp); APIPRINTF(("MP3ENC: close_mp3enc()\n"));// mp3encClose(pPInfo->pmp3);// mp3encDone();// pPInfo->pmp3 = NULL; if (pPInfo->outputbuffer != NULL) free(pPInfo->outputbuffer); pPInfo->outputbuffer = NULL;}MAIStatus_e _endprocessing(MAICompHandle_t hComp){ ProcessorInfo_t *pPInfo=(ProcessorInfo_t *)HCOMP_TO_USERINFO(hComp); MAIStatus_e eStatus = MAI_STATUS_OK; APIPRINTF(("MP3ENC: _endprocessing() enter\n")); if (MAICompBase_IsState(hComp, MAI_RUNSTATE_NONE)) eStatus=MAI_STATUS_WRONGSTATE; else { close_mp3enc(hComp); } MAICompBase_EndProcessing(hComp); APIPRINTF(("MP3ENC: _endprocessing() exit: status=0x%X\n", eStatus)); return eStatus;}MAIStatus_e _processbuffer(MAICompHandle_t hComp, MAICompBuffer_t *pInBufferInfo){ ProcessorInfo_t *pPInfo=(ProcessorInfo_t *)HCOMP_TO_USERINFO(hComp); MAIStatus_e eStatus = MAI_STATUS_OK; MAICompBuffer_t BufferInfo; short *short_buffer; int outsize, insize; int error_bail_count = 0; int eof_bail_count = 0; if ((pInBufferInfo->dwFlags&MAICOMPBUF_FLAG_EOS) && (pInBufferInfo->uiDataSize == 0)) { APIPRINTF(("MP3ENC EOS received, size %d\n", pInBufferInfo->uiDataSize)); // note that MAICompBase_SendEOS(hComp, 0); happens in MAICompBase_ProcessorThread(); close_mp3enc(hComp); return eStatus; } DPRINTF(("MP3ENC: _processbuffer() enter\n"));#ifdef DUMP_TO_FILE { static FILE *ofp = NULL; DPRINTF(("pcm dump to file\n")); if (ofp == NULL) ofp = fopen("dump.pcm", "wb"); if (ofp != NULL) fwrite(pInBufferInfo->pBuffer, 1, pInBufferInfo->uiDataSize, ofp); MAIOSSleep(200); return 0; }#endif // nSamplesRead = fread(&inputBuffer[0][0], sizeof(short), MP3ENC_BLOCKSIZE * numberOfChannels, fIn); insize = (int)pInBufferInfo->uiDataSize; short_buffer = (short *)pInBufferInfo->pBuffer; while ((eStatus==MAI_STATUS_OK) && (insize > 0)) { int iread; int err;#if 0 if (pPInfo->m_bNewFormat || (pPInfo->pmp3 == NULL)) { int err; MAIMediaTypeAudio_t *pAudioInfo = (MAIMediaTypeAudio_t *)pInBufferInfo->pBuffer; pPInfo->m_bNewFormat = 0; pPInfo->config.sampleRate = 44100; pPInfo->config.bitRate = 128000; pPInfo->config.nChannelsIn = 2; pPInfo->config.nChannelsOut = 2; pPInfo->config.bandWidth = 0; pPInfo->config.fVbrMode = (pPInfo->config.bitRate == 0); pPInfo->config.vbrQuality = pPInfo->config.fVbrMode ? 5 : 0; pPInfo->config.fCrc = 0; pPInfo->config.fNoIntensity = 1; pPInfo->config.paddingMode = MP3ENC_PADDING_ISO; pPInfo->config.fFullHuffman = 0; pPInfo->config.privateBit = 0; pPInfo->config.copyRightBit = 0; pPInfo->config.originalCopyBit = 0; if (pAudioInfo->uiSamplesPerSec <= 48000) pPInfo->config.sampleRate = pAudioInfo->uiSamplesPerSec; if (pAudioInfo->uiChannels <= 2) pPInfo->config.nChannelsOut = pPInfo->config.nChannelsIn = pAudioInfo->uiChannels;printf("channels %d rate %d\n", pPInfo->config.nChannelsIn, pPInfo->config.sampleRate); err = mp3encOpen(&pPInfo->pmp3, &pPInfo->config); if (!err) { printf("fatal error %x during initialization\n", err); return MAI_STATUS_WRONGSTATE; } } if (insize >= (int)(MP3ENC_BLOCKSIZE * pPInfo->config.nChannelsIn * sizeof(short))) { iread = MP3ENC_BLOCKSIZE * pPInfo->config.nChannelsIn; } else { iread = insize/4; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -