Minidlna源码交叉编译支持DSD歌曲

996 阅读8分钟

安装交叉编译器

根据自己的开发板下载不同的交叉编译器,或者你已经有交叉编译器了就不需要下载安装了

Linaro Toolchain

可以从上述链接中下载相应的交叉编译器,以下以我的环境为主,我下载了aarch64-linux-gnu编译器 下载上面链接中的 gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu.tar.xz,压缩包有可能更新了,日期和版本会变化。 或者用命令下载

$ cd /usr/src/
# 下载时请注意压缩包的日期,否则有可能404,命令下载较慢,可以通过pc端的迅雷下载较快
$ wget https://releases.linaro.org/components/toolchain/binaries/latest-7/aarch64-linux-gnu/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz
# 压缩包版本日期也许有变化
$ sudo tar -xvf  gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu.tar.xz -C /usr/src/
$ cd /usr/src/
$ sudo mv gcc-linaro-7.4.1-2019.02-x86_64_aarch64-linux-gnu aarch64-linux-gnu
$ sudo vim /etc/profile
append_path '/usr/src/aarch64-linux-gnu/bin'
# 或者编辑.bashrc
$ sudo vim ~/.bashrc
# 末尾添加以下内容
export ARCH_HOME=/usr/src/aarch64-linux-gnu
export PATH=$PATH:$ARCH_HOME/bin
# 生效配置
$ source /etc/profile
or
$ source ~/.bashrc

Minidlna

找到自己想要的minidlna版本下载源码,根据源码中的README需要编译依赖库,所以在正式编译minidlna之前需要把这些依赖库都交叉编译了,很麻烦

  • libexif
  • libjpeg
  • libid3tag
  • libFLAC
  • libvorbis
  • libsqlite3
  • libavformat (the ffmpeg libraries)
# 创建工作目录,后续操作都在此目录
$ mkdir ~/build && cd ~/build

交叉编译libexif

$ cd ~/build

# 下载libexif源码
$ wget https://sourceforge.net/projects/libexif/files/libexif/0.6.21/libexif-0.6.21.tar.bz2

$ tar -xvf libexif-0.6.21.tar.bz2 && cd libexif-0.6.21/

# host要换成自己交叉编译器的前缀  
# --prefix 是安装时的目录,此处我安装到交叉编译器的目录中
# --disable-shared 是静态编译
$ ./configure --host=aarch64-linux-gnu --prefix=/usr/src/aarch64-linux-gnu --disable-shared

$ sudo make && sudo make install

# 如果碰到 “checking build system type... Invalid configuration `aarch64-linux': machine `aarch64' not recognized” 错误
# 删除当前的config.guess文件和config.sub,然后下载文件替换
$ rm -rf config.guess config.sub
$ wget -O config.guess 'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD'
$ wget -O config.sub 'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD'

交叉编译libjpeg

$ cd ~/build

# 下载libjpeg源代码
$ wget https://sourceforge.net/projects/libjpeg/files/libjpeg/6b/jpegsrc.v6b.tar.gz

$ tar -xvf jpegsrc.v6b.tar.gz && cd jpeg-6b/

$ cp /usr/bin/libtool ./
$ cp /usr/share/libtool/config/config.guess .
$ cp /usr/share/libtool/config/config.sub .

# 如果 /usr/share/libtool/config/config.guess 没有这个文件则通过以下形式生成文件
$ wget -O config.guess 'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD'
$ wget -O config.sub 'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD'

$ ./configure --host=aarch64 CC=aarch64-linux-gnu-gcc --prefix=/usr/src/aarch64-linux-gnu --enable-shared=no


# make install之前需要在安装的目录创建以下目录
$ mkdir -p /usr/src/aarch64-linux-gnu/include
$ mkdir -p /usr/src/aarch64-linux-gnu/lib
$ mkdir -p /usr/src/aarch64-linux-gnu/bin
$ mkdir -p /usr/src/aarch64-linux-gnu/man/man1

$ sudo make && sudo make install && sudo make install-lib

交叉编译libid3tag

在安装此依赖时需要先安装zlib,否则会报如下错误

报错信息

configure: error: zlib.h was not found *** You must first install zlib (libz) before you can build this package. *** If zlib is already installed, you may need to use the CPPFLAGS *** environment variable to specify its installed location, e.g. -I

.

$ cd ~/build
# 下载zlib源码
$ wget https://sourceforge.net/projects/libpng/files/zlib/1.2.11/zlib-1.2.11.tar.gz
$ tar -xvf zlib-1.2.11.tar.gz && cd zlib-1.2.11/
$ AR=aarch64-linux-gnu-ar CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ \
  ./configure --prefix=/usr/src/aarch64-linux-gnu --static
$ sudo make && sudo make install


$ cd ~/build
# 下载libid3tag源代码
$ wget https://sourceforge.net/projects/mad/files/libid3tag/0.15.1b/libid3tag-0.15.1b.tar.gz
$ tar -xvf libid3tag-0.15.1b.tar.gz && cd libid3tag-0.15.1b/
$ ./configure --host=aarch64-linux-gnu \
CFLAGS=-I/usr/src/aarch64-linux-gnu/include \
CPPFLAGS=-I/usr/src/aarch64-linux-gnu/include \
LDFLAGS=-L/usr/src/aarch64-linux-gnu/lib \
--prefix=/usr/src/aarch64-linux-gnu --disable-shared
$ sudo make && sudo make install

交叉编译libFLAC

在编译此依赖时需要先安装libogg,否则会报错

fatal error: ogg/ogg.h: No such file or directory

$ cd ~/build
$ wget https://downloads.xiph.org/releases/ogg/libogg-1.3.4.tar.gz
$ tar -xvf libogg-1.3.4.tar.gz && cd libogg-1.3.4
$ ./configure --host=aarch64-linux-gnu --prefix=/usr/src/aarch64-linux-gnu --disable-shared
$ sudo make && sudo make install


$ cd ~/build
# 下载源码
$ wget https://ftp.osuosl.org/pub/xiph/releases/flac/flac-1.3.3.tar.xz
$ tar -xvf flac-1.3.3.tar.xz && cd flac-1.3.3
$ ./configure --host=aarch64-linux-gnu --with-ogg=/usr/src/aarch64-linux-gnu --prefix=/usr/src/aarch64-linux-gnu \
  --disable-shared
$ sudo make && sudo make install

交叉编译libvorbis

编译此依赖也需要先安装libogg,上面已经安装过了

$ cd ~/build
# 下载源码
$ wget https://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.7.tar.gz
$ tar -xvf libvorbis-1.3.7.tar.gz && cd libvorbis-1.3.7
$ ./configure --host=aarch64-linux-gnu --with-ogg=/usr/src/aarch64-linux-gnu \
CFLAGS=-I/usr/src/aarch64-linux-gnu/include \
CPPFLAGS=-I/usr/src/aarch64-linux-gnu/include \
--prefix=/usr/src/aarch64-linux-gnu --disable-shared
$ sudo make && sudo make install

交叉编译libsqlite3

$ cd ~/build
# 下载源码
$ wget https://www.sqlite.org/2020/sqlite-autoconf-3330000.tar.gz
$ tar -xvf sqlite-autoconf-3330000.tar.gz && cd sqlite-autoconf-3330000
$ ./configure --host=aarch64-linux-gnu --prefix=/usr/src/aarch64-linux-gnu --disable-shared
$ sudo make && sudo make install

交叉编译libavformat

警告 aarch64-linux-gnu-pkg-config not found, library detection may fail.

安装pkg-config解决警告, 我是arch系统,其他系统用自己的安装工具即可

$ sudo pacman -S pkg-config
$ cd ~/build
# 下载源码
$ wget https://ffmpeg.org/releases/ffmpeg-4.3.1.tar.gz
$ tar -xvf ffmpeg-4.3.1.tar.gz && cd ffmpeg-4.3.1
$ ./configure --enable-cross-compile --cross-prefix=aarch64-linux-gnu- --prefix=/usr/src/aarch64-linux-gnu \
--pkg-config=/usr/bin/pkg-config --arch=aarch64 --target-os=linux --disable-shared
$ sudo make && sudo install

修改代码支持dsd和交叉编译minidlna

修改代码支持dsd

metadata.c

在337到339行左右有一堆的if else if,在最后的else之前添加两个else if 以判断dsd歌曲的后缀名,dsf和dff

$ cd ~/build
# 下载minidlna
$ wget https://sourceforge.net/projects/minidlna/files/minidlna/1.2.1/minidlna-1.2.1.tar.gz
$ cd minidlna-1.2.1
$ vim metadata.c
    else if( ends_with(path, ".pcm") )
    {
        strcpy(type, "pcm");
        m.mime = strdup("audio/L16");
    }
+   else if( ends_with(path, ".dsf") )
+   {
+       strcpy(type, "dsf");
+       m.mime = strdup("audio/x-dsd");
+   }
+   else if( ends_with(path, ".dff") )
+   {
+       strcpy(type, "dff");
+       m.mime = strdup("audio/x-dsd");
+   }
    else
    {
        DPRINTF(E_WARN, L_METADATA, "Unhandled file extension on %s\n", path);
    }

tagutils/tagutils-dff.c

新建文件tagutils-dff.c,此文件是分析dsd歌曲的内容,标签,歌曲作者等等一些

vim tagutils/tagutils-dff.c

复制以下内容到文件中

//=========================================================================
// FILENAME     : tagutils-dff.c
// DESCRIPTION  : DFF metadata reader
//=========================================================================
// Copyright (c) 2014 Takeshich NAKAMURA
//=========================================================================

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#define GET_DFF_INT64(p) ((((uint64_t)((p)[0])) << 56) |   \
                          (((uint64_t)((p)[1])) << 48) |   \
                          (((uint64_t)((p)[2])) << 40) |   \
                          (((uint64_t)((p)[3])) << 32) |   \
                          (((uint64_t)((p)[4])) << 24) |   \
                          (((uint64_t)((p)[5])) << 16) |   \
                          (((uint64_t)((p)[6])) << 8) |    \
                          (((uint64_t)((p)[7])) ))

#define GET_DFF_INT32(p) ((((uint32_t)((p)[0])) << 24) |   \
                          (((uint32_t)((p)[1])) << 16) |   \
                          (((uint32_t)((p)[2])) << 8) |     \
                          (((uint32_t)((p)[3])) ))

#define GET_DFF_INT16(p) ((((uint16_t)((p)[0])) << 8) |   \
                          (((uint16_t)((p)[1])) )) 

static int
_get_dfffileinfo(char *file, struct song_metadata *psong)
{
    FILE *fp;
    uint32_t len;
    uint32_t rt;
    unsigned char hdr[32] = {0};

    uint64_t totalsize = 0;
    uint64_t propckDataSize = 0;
    uint64_t count = 0;
    uint32_t samplerate = 0;
    uint16_t channels = 0;
    //DST
    uint64_t dstickDataSize = 0;
    uint32_t numFrames = 0;
    uint16_t frameRate = 0;
    unsigned char frteckData[18] = {0};
    unsigned char dstickData[12] = {0};
    uint64_t totalcount = 0;
    unsigned char ckbuf[12] = {0};
    unsigned char compressionType[4] = {0};
    unsigned char dsdsdckData[12] = {0};
    uint64_t dsdsdckDataSize = 0;
    uint64_t cmprckDataSize = 0;
    uint64_t abssckDataSize = 0;
    uint64_t lscockDataSize = 0;
    uint64_t comtckDataSize = 0;
    uint64_t diinckDataSize = 0;
    uint64_t diarckDataSize = 0;
    uint64_t ditickDataSize = 0;
    uint64_t manfckDataSize = 0;

    //DPRINTF(E_DEBUG,L_SCANNER,"Getting DFF fileinfo =%s\n",file);    
    
    if((fp = fopen(file, "rb")) == NULL)
    {
        DPRINTF(E_WARN, L_SCANNER, "Could not create file handle\n");
        return -1;
    }
        
    len = 32;
    //Form DSD chunk
    if(!(rt = fread(hdr, len, 1,fp)))
    {
        DPRINTF(E_WARN, L_SCANNER, "Could not read Form DSD chunk from %s\n", file);
        fclose(fp);
        return -1;
    }
    
    if(strncmp((char*)hdr, "FRM8", 4))
    {
        DPRINTF(E_WARN, L_SCANNER, "Invalid Form DSD chunk in %s\n", file);
        fclose(fp);
        return -1;
    }
        
    totalsize = GET_DFF_INT64(hdr + 4) ;
       
    if(strncmp((char*)hdr+12, "DSD ", 4))
    {
        DPRINTF(E_WARN, L_SCANNER, "Invalid Form DSD chunk in %s\n", file);
        fclose(fp);
        return -1;
    }

    //FVER chunk
    if(strncmp((char*)hdr+16, "FVER", 4))
    {
        DPRINTF(E_WARN, L_SCANNER, "Invalid Format Version Chunk in %s\n", file);
        fclose(fp);
        return -1;
    }

    totalsize -= 16;
    while(totalcount < totalsize - 4)
    {
    
        if(!(rt = fread(ckbuf, sizeof(ckbuf), 1,fp)))
        {
            //DPRINTF(E_WARN, L_SCANNER, "Could not read chunk header from %s\n", file);
            //fclose(fp);
            //return -1;
            break;
        }
    
        //Property chunk
        if(strncmp((char*)ckbuf, "PROP", 4) == 0)
        {

            
            propckDataSize = GET_DFF_INT64(ckbuf + 4);
            totalcount += propckDataSize + 12;

            unsigned char propckData[propckDataSize];

            if(!(rt = fread(propckData, propckDataSize, 1,fp)))
            {
                DPRINTF(E_WARN, L_SCANNER, "Could not read Property chunk from %s\n", file);
                fclose(fp);
                return -1;
            }
                
            if(strncmp((char*)propckData, "SND ", 4))
            {
                DPRINTF(E_WARN, L_SCANNER, "Invalid Property chunk in %s\n", file);
                fclose(fp);
                return -1;
            }

            count += 4; 
            while(count < propckDataSize)
            {
                if(strncmp((char*)propckData+count, "FS  ", 4) == 0)
                {
                    //Sample Rate Chunk
                    count += 12;
                    samplerate = GET_DFF_INT32(propckData+count);
                    psong->samplerate = samplerate;
                    count += 4;
                    
                    //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "Sample Rate is %d\n", psong->samplerate);
                }else if(strncmp((char*)propckData+count, "CHNL", 4) == 0)
                {
                    //Channels Chunk
                    count += 12;
                    channels = GET_DFF_INT16(propckData+count);
                    psong->channels = channels;
                    count += channels * 4 + 2;
                    
                    //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "channels is %d\n", channels);
                }else if(strncmp((char*)propckData+count, "CMPR", 4) == 0)
                {
                    //Compression Type Chunk
                    count += 4;
                    cmprckDataSize = GET_DFF_INT64(propckData+count);
                    count += 8;
                    strncpy((char*)compressionType,(char*)propckData+count,4);
                    count += cmprckDataSize;
                    
                }else if(strncmp((char*)propckData+count, "ABSS", 4) == 0)
                {
                    //Absolute Start Time Chunk
                    count += 4;
                    abssckDataSize = GET_DFF_INT64(propckData+count);
                    count += abssckDataSize + 8;
                    
                }else if(strncmp((char*)propckData+count, "LSCO", 4) == 0)
                {
                    //Loudsperaker Configuration Chunk
                    count += 4;
                    lscockDataSize = GET_DFF_INT64(propckData+count);
                    count += lscockDataSize + 8;
                    
                }else{
                    break;
                }
            }

            //bitrate bitpersample is 1bit
            psong->bitrate = channels * samplerate * 1;

            //DSD/DST Sound Data Chunk
            len = 12;
            if(!(rt = fread(dsdsdckData, len, 1,fp)))
            {
                DPRINTF(E_WARN, L_SCANNER, "Could not read DSD/DST Sound Data chunk from %s\n", file);
                fclose(fp);
                return -1;
            }
    
            if(strncmp((char*)compressionType,(char*)dsdsdckData , 4))
            {
                DPRINTF(E_WARN, L_SCANNER, "Invalid DSD/DST Sound Data chunk in %s\n", file);
                fclose(fp);
                return -1;
            }

            if(strncmp((char*)dsdsdckData,"DSD " , 4) == 0)
            {
                //DSD
                dsdsdckDataSize = GET_DFF_INT64(dsdsdckData+4);
                totalcount += dsdsdckDataSize + 12;
                psong->song_length = (int)((double)dsdsdckDataSize / (double)samplerate / (double)channels * 8 * 1000);
                
                //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "songlength is %d\n", psong->song_length);
                
                fseeko(fp, dsdsdckDataSize,SEEK_CUR);
            }else if(strncmp((char*)dsdsdckData,"DST " , 4) == 0)
            {
                //DST
                dsdsdckDataSize = GET_DFF_INT64(dsdsdckData+4);
                totalcount += dsdsdckDataSize + 12;
    
                //DST Frame Information chunk   
                if(!(rt = fread(frteckData, 18, 1,fp)))
                {
                    DPRINTF(E_WARN, L_SCANNER, "Could not read DST Frame Information chunk from %s\n", file);
                    fclose(fp);
                    return -1;
                }

                if(strncmp((char*)frteckData ,"FRTE", 4) == 0)
                {
                    //uint64_t frteckDataSize = GET_DFF_INT64(frteckData+4);
                    numFrames = GET_DFF_INT32((char*)frteckData+12);
                    frameRate = GET_DFF_INT16((char*)frteckData+16);

                    psong->song_length = numFrames / frameRate * 1000;
    
                    fseeko(fp, dsdsdckDataSize-18,SEEK_CUR); 
                }else
                {
                    DPRINTF(E_WARN, L_SCANNER, "Invalid DST Frame Information chunk in %s\n", file);
                    fclose(fp);
                    return -1;
                }

                //DST Sound Index Chunk
                if(!(rt = fread(dstickData, 12, 1,fp)))
                {
                    if (ferror(fp))
                    {
                        DPRINTF(E_WARN, L_SCANNER, "Could not read DST Sound Index chunk from %s\n", file);
                        fclose(fp);
                        return -1;
                    }else
                    {
                        //EOF
                        break;
                    }
                }

                if(strncmp((char*)dstickData ,"DSTI", 4) == 0)
                {
                    dstickDataSize = GET_DFF_INT64(dstickData+4);
                    totalcount += dstickDataSize + 12;
                    fseeko(fp, dstickDataSize,SEEK_CUR);
                }else
                {
                    fseeko(fp, -12,SEEK_CUR);
                }
            }else
            {
                DPRINTF(E_WARN, L_SCANNER, "Invalid DSD/DST Sound Data chunk in %s\n", file);
                fclose(fp);
                return -1;
            }
        }else if(!strncmp((char*)ckbuf ,"COMT", 4))
        {
            //COMT Chunk
            comtckDataSize = GET_DFF_INT64(ckbuf+4);
            totalcount += comtckDataSize + 12;
            fseeko(fp, comtckDataSize,SEEK_CUR);

        }else if(!strncmp((char*)ckbuf ,"DIIN", 4))
        {
            //Edited Master Information chunk
            diinckDataSize = GET_DFF_INT64(ckbuf+4);
            unsigned char diinckData[diinckDataSize];
            totalcount += diinckDataSize + 12;
            
            if(!(rt = fread(diinckData, diinckDataSize, 1,fp)))
            {
                DPRINTF(E_WARN, L_SCANNER, "Could not read Edited Master Information chunk from %s\n", file);
                fclose(fp);
                return -1;
            }

            uint64_t icount = 0;
            while(icount < diinckDataSize)
            {
                if(!strncmp((char*)diinckData+icount ,"EMID", 4))
                {
                    //Edited Master ID chunk
                    icount += 4;    
                    icount += GET_DFF_INT64(diinckData+icount) + 8;

                }else if(!strncmp((char*)diinckData+icount ,"MARK", 4))
                {
                    //Master Chunk
                    icount += 4;    
                    icount += GET_DFF_INT64(diinckData+icount) + 8;

                }else if(!strncmp((char*)diinckData+icount ,"DIAR", 4))
                {
                    //Artist Chunk
                    icount += 4;
                    diarckDataSize = GET_DFF_INT64(diinckData+icount);
                    unsigned char arttext[diarckDataSize +1 - 4];
                    
                    icount += 12;   
                    
                    memset(arttext,0x00,sizeof(arttext));   
                    strncpy((char*)arttext,(char*)diinckData+icount,sizeof(arttext)-1);
                    psong->contributor[ROLE_ARTIST] = strdup((char*)&arttext[0]);

                    icount += diarckDataSize - 4;
                    
                }else if(!strncmp((char*)diinckData+icount ,"DITI", 4))
                {
                    //Title Chunk
                    icount += 4;
                    ditickDataSize = GET_DFF_INT64(diinckData+icount);
                    unsigned char titletext[ditickDataSize+1 - 4];
                    
                    icount += 12;
                    
                    memset(titletext,0x00,sizeof(titletext));   
                    strncpy((char*)titletext,(char*)diinckData+icount,sizeof(titletext)-1);
                    psong->title =  strdup((char*)&titletext[0]);
                    icount += ditickDataSize - 4;
                    
                }else
                {
                    break;
                }
            }
        }else if(!strncmp((char*)ckbuf ,"MANF", 4))
        {
            //Manufacturer Specific Chunk
            manfckDataSize = GET_DFF_INT64(ckbuf+4);
            totalcount += manfckDataSize + 12;
            fseeko(fp, manfckDataSize,SEEK_CUR);

        }
    }
    
    fclose(fp);

    xasprintf(&(psong->dlna_pn), "DFF");
    return 0;
}

tagutils/tagutils-dff.h

添加dff的头文件内容

$ vim tagutils/tagutils-dff.h
//=========================================================================
// FILENAME     : tagutils-dff.h
// DESCRIPTION  : DFF metadata reader
//=========================================================================
// Copyright (c) 2014 Takeshich NAKAMURA
//=========================================================================

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
static int _get_dfffileinfo(char *file, struct song_metadata *psong);

tagutils/tagutils-dsf.c

dsd歌曲还有dsf的后缀,新建tagutils-dsf.c文件支持分析dsf歌曲

$ vim tagutils/tagutils-dsf.c
//=========================================================================
// FILENAME : tagutils-dsf.c
// DESCRIPTION  : DSF metadata reader
//=========================================================================
// Copyright (c) 2014 Takeshich NAKAMURA 
// based on tagutils-mp3.c
//=========================================================================

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#define GET_DSF_INT64(p) ((((uint64_t)((p)[7])) << 56) |   \
              (((uint64_t)((p)[6])) << 48) |   \
              (((uint64_t)((p)[5])) << 40) |   \
              (((uint64_t)((p)[4])) << 32) |   \
              (((uint64_t)((p)[3])) << 24) |   \
              (((uint64_t)((p)[2])) << 16) |   \
              (((uint64_t)((p)[1])) << 8) |    \
              (((uint64_t)((p)[0]))))

#define GET_DSF_INT32(p) ((((uint8_t)((p)[3])) << 24) |   \
              (((uint8_t)((p)[2])) << 16) |   \
              (((uint8_t)((p)[1])) << 8) |     \
              (((uint8_t)((p)[0]))))
            
static int
_get_dsftags(char *file, struct song_metadata *psong)
{
    struct id3_tag *pid3tag;
    struct id3_frame *pid3frame;
    int err;
    int index;
    int used;
    unsigned char *utf8_text;
    int genre = WINAMP_GENRE_UNKNOWN;
    int have_utf8;
    int have_text;
    id3_ucs4_t const *native_text;
    char *tmp;
    int got_numeric_genre;
    id3_byte_t const *image;
    id3_length_t image_size = 0;
    
    FILE *fp;
    struct id3header *pid3;
    uint32_t len;
    unsigned char hdr[28] = {0};
    uint64_t total_size = 0;
    uint64_t pointer_to_metadata_chunk = 0;
    uint64_t metadata_chunk_size = 0;
    unsigned char *id3tagbuf = NULL;

    //DEBUG DPRINTF(E_DEBUG,L_SCANNER,"Getting DSF file info\n");

    if((fp = fopen(file, "rb")) == NULL)
    {
        DPRINTF(E_WARN, L_SCANNER, "Could not create file handle\n");
        return -1;
    }

    len = 28;
    if(!(len = fread(hdr, len, 1,fp)))
    {
        DPRINTF(E_WARN, L_SCANNER, "Could not read DSD Chunk from %s\n", file);
        fclose(fp);
        return -1;
    }

    if(strncmp((char*)hdr, "DSD ", 4))
    {
        DPRINTF(E_WARN, L_SCANNER, "Invalid DSD Chunk header in %s\n", file);
        fclose(fp);
        return -1;
    }

    total_size = GET_DSF_INT64(hdr + 12);
    pointer_to_metadata_chunk = GET_DSF_INT64(hdr + 20);
    metadata_chunk_size = total_size - pointer_to_metadata_chunk;
    
    //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "%llu\n", total_size);
    //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "%llu\n", pointer_to_metadata_chunk);
    //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "%llu\n", metadata_chunk_size);

    //check invalid metadata
    if(total_size == 0)
    {
        fclose(fp);
        DPRINTF(E_INFO, L_SCANNER, "Invalid TotalDataSize in %s\n", file);
        return 0;
    }

    if(pointer_to_metadata_chunk == 0)
    {
        fclose(fp);
        DPRINTF(E_INFO, L_SCANNER, "Metadata doesn't exist %s\n", file);
        return 0;
    }

    if(total_size > pointer_to_metadata_chunk)
    {
        metadata_chunk_size = total_size - pointer_to_metadata_chunk;
    }
    else
    {
        fclose(fp);
        DPRINTF(E_INFO, L_SCANNER, "Invalid PointerToMetadata in %s\n", file);
        return 0;
    }

    fseeko(fp, pointer_to_metadata_chunk,SEEK_SET);

    id3tagbuf = (unsigned char *)malloc(sizeof(unsigned char)*metadata_chunk_size);
    if(id3tagbuf == NULL)
    {
        fclose(fp);
        DPRINTF(E_WARN, L_SCANNER, "Out of memory.Big MetadataSize in %s\n",file);
        return -1;
    }
    memset(id3tagbuf, 0,sizeof(unsigned char)*metadata_chunk_size);
    
    if(!(len = fread(id3tagbuf,metadata_chunk_size,1,fp)))
    {
        fclose(fp);
        free(id3tagbuf);
        DPRINTF(E_WARN, L_SCANNER, "Could not read Metadata Chunk from %s\n", file);
        return -1;
    }
    
    pid3tag = id3_tag_parse(id3tagbuf,metadata_chunk_size);
    
    if(!pid3tag)
    {
        free(id3tagbuf);
        err = errno;
        fclose(fp);
        errno = err;
        DPRINTF(E_WARN, L_SCANNER, "Cannot get ID3 tag for %s\n", file);
        return -1;
    }

    pid3 = (struct id3header*)id3tagbuf;

    if(strncmp((char*)pid3->id, "ID3", 3) == 0)
    {
        char tagversion[16];

        /* found an ID3 header... */
        snprintf(tagversion, sizeof(tagversion), "ID3v2.%d.%d",
             pid3->version[0], pid3->version[1]);
        psong->tagversion = strdup(tagversion);
    }
    pid3 = NULL;
    
    index = 0;
    while((pid3frame = id3_tag_findframe(pid3tag, "", index)))
    {
        used = 0;
        utf8_text = NULL;
        native_text = NULL;
        have_utf8 = 0;
        have_text = 0;

        if(!strcmp(pid3frame->id, "YTCP"))   /* for id3v2.2 */
        {
            psong->compilation = 1;
            DPRINTF(E_DEBUG, L_SCANNER, "Compilation: %d [%s]\n", psong->compilation, basename(file));
        }
        else if(!strcmp(pid3frame->id, "APIC") && !image_size)
        {
            if( (strcmp((char*)id3_field_getlatin1(&pid3frame->fields[1]), "image/jpeg") == 0) ||
                (strcmp((char*)id3_field_getlatin1(&pid3frame->fields[1]), "image/jpg") == 0) ||
                (strcmp((char*)id3_field_getlatin1(&pid3frame->fields[1]), "jpeg") == 0) )
            {
                image = id3_field_getbinarydata(&pid3frame->fields[4], &image_size);
                if( image_size )
                {
                    psong->image = malloc(image_size);
                    memcpy(psong->image, image, image_size);
                    psong->image_size = image_size;
                    //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "Found thumbnail: %d\n", psong->image_size);
                }
            }
        }

        if(((pid3frame->id[0] == 'T') || (strcmp(pid3frame->id, "COMM") == 0)) &&
           (id3_field_getnstrings(&pid3frame->fields[1])))
            have_text = 1;

        if(have_text)
        {
            native_text = id3_field_getstrings(&pid3frame->fields[1], 0);

            if(native_text)
            {
                have_utf8 = 1;
                if(lang_index >= 0)
                    utf8_text = _get_utf8_text(native_text); // through iconv
                else
                    utf8_text = (unsigned char*)id3_ucs4_utf8duplicate(native_text);

                if(!strcmp(pid3frame->id, "TIT2"))
                {
                    used = 1;
                    psong->title = (char*)utf8_text;
                }
                else if(!strcmp(pid3frame->id, "TPE1"))
                {
                    used = 1;
                    psong->contributor[ROLE_ARTIST] = (char*)utf8_text;
                }
                else if(!strcmp(pid3frame->id, "TALB"))
                {
                    used = 1;
                    psong->album = (char*)utf8_text;
                }
                else if(!strcmp(pid3frame->id, "TCOM"))
                {
                    used = 1;
                    psong->contributor[ROLE_COMPOSER] = (char*)utf8_text;
                }
                else if(!strcmp(pid3frame->id, "TIT1"))
                {
                    used = 1;
                    psong->grouping = (char*)utf8_text;
                }
                else if(!strcmp(pid3frame->id, "TPE2"))
                {
                    used = 1;
                    psong->contributor[ROLE_BAND] = (char*)utf8_text;
                }
                else if(!strcmp(pid3frame->id, "TPE3"))
                {
                    used = 1;
                    psong->contributor[ROLE_CONDUCTOR] = (char*)utf8_text;
                }
                else if(!strcmp(pid3frame->id, "TCON"))
                {
                    used = 1;
                    psong->genre = (char*)utf8_text;
                    got_numeric_genre = 0;
                    if(psong->genre)
                    {
                        if(!strlen(psong->genre))
                        {
                            genre = WINAMP_GENRE_UNKNOWN;
                            got_numeric_genre = 1;
                        }
                        else if(isdigit(psong->genre[0]))
                        {
                            genre = atoi(psong->genre);
                            got_numeric_genre = 1;
                        }
                        else if((psong->genre[0] == '(') && (isdigit(psong->genre[1])))
                        {
                            genre = atoi((char*)&psong->genre[1]);
                            got_numeric_genre = 1;
                        }

                        if(got_numeric_genre)
                        {
                            if((genre < 0) || (genre > WINAMP_GENRE_UNKNOWN))
                                genre = WINAMP_GENRE_UNKNOWN;
                            free(psong->genre);
                            psong->genre = strdup(winamp_genre[genre]);
                        }
                    }
                }
                else if(!strcmp(pid3frame->id, "COMM"))
                {
                    used = 1;
                    psong->comment = (char*)utf8_text;
                }
                else if(!strcmp(pid3frame->id, "TPOS"))
                {
                    tmp = (char*)utf8_text;
                    strsep(&tmp, "/");
                    if(tmp)
                    {
                        psong->total_discs = atoi(tmp);
                    }
                    psong->disc = atoi((char*)utf8_text);
                }
                else if(!strcmp(pid3frame->id, "TRCK"))
                {
                    tmp = (char*)utf8_text;
                    strsep(&tmp, "/");
                    if(tmp)
                    {
                        psong->total_tracks = atoi(tmp);
                    }
                    psong->track = atoi((char*)utf8_text);
                }
                else if(!strcmp(pid3frame->id, "TDRC"))
                {
                    psong->year = atoi((char*)utf8_text);
                }
                else if(!strcmp(pid3frame->id, "TLEN"))
                {
                    psong->song_length = atoi((char*)utf8_text);
                }
                else if(!strcmp(pid3frame->id, "TBPM"))
                {
                    psong->bpm = atoi((char*)utf8_text);
                }
                else if(!strcmp(pid3frame->id, "TCMP"))
                {
                    psong->compilation = (char)atoi((char*)utf8_text);
                }
            }
        }

        // check if text tag
        if((!used) && (have_utf8) && (utf8_text))
            free(utf8_text);

        // v2 COMM
        if((!strcmp(pid3frame->id, "COMM")) && (pid3frame->nfields == 4))
        {
            native_text = id3_field_getstring(&pid3frame->fields[2]);
            if(native_text)
            {
                utf8_text = (unsigned char*)id3_ucs4_utf8duplicate(native_text);
                if((utf8_text) && (strncasecmp((char*)utf8_text, "iTun", 4) != 0))
                {
                    // read comment
                    free(utf8_text);

                    native_text = id3_field_getfullstring(&pid3frame->fields[3]);
                    if(native_text)
                    {
                        utf8_text = (unsigned char*)id3_ucs4_utf8duplicate(native_text);
                        if(utf8_text)
                        {
                            free(psong->comment);
                            psong->comment = (char*)utf8_text;
                        }
                    }
                }
                else
                {
                    free(utf8_text);
                }
            }
        }

        index++;
    }

    id3_tag_delete(pid3tag);
    free(id3tagbuf);
    fclose(fp);
    //DPRINTF(E_DEBUG, L_SCANNER, "Got id3tag successfully for file=%s\n", file);
    return 0;
}

static int
_get_dsffileinfo(char *file, struct song_metadata *psong)
{
    FILE *fp;
    int len = 80;
    unsigned char hdr[len];
    uint32_t channelnum;
    uint32_t samplingfrequency;
    uint32_t bitpersample;
    uint64_t samplecount;

    if((fp = fopen(file, "rb")) == NULL)
    {
        DPRINTF(E_WARN, L_SCANNER, "Could not create file handle\n");
        return -1;
    }

    if(!(len = fread(hdr, len, 1,fp)))
    {
        DPRINTF(E_WARN, L_SCANNER, "Could not read chunks from %s\n", file);
        fclose(fp);
        return -1;
    }
 
    if(strncmp((char*)hdr, "DSD ", 4))
    {
        DPRINTF(E_WARN, L_SCANNER, "Invalid DSD Chunk headerin %s\n", file);
        fclose(fp);
        return -1;
    }

    if(strncmp((char*)hdr+28, "fmt ", 4))
    {
        DPRINTF(E_WARN, L_SCANNER, "Invalid fmt Chunk header in %s\n", file);
        fclose(fp);
        return -1;
    }

    channelnum = GET_DSF_INT32(hdr + 52);
    samplingfrequency = GET_DSF_INT32(hdr + 56);
    bitpersample = GET_DSF_INT32(hdr + 60);
    samplecount = GET_DSF_INT64(hdr + 64);

    psong->bitrate = channelnum * samplingfrequency * bitpersample;
    psong->samplesize = bitpersample;
    psong->samplerate = samplingfrequency;
    psong->song_length = (samplecount / samplingfrequency) * 1000;
    psong->channels = channelnum;

    fclose(fp);

    xasprintf(&(psong->dlna_pn), "DSF");
    return 0;
}

tagutils/tagutils-dsf.h

添加头文件

$ vim tagutils/tagutils-dsf.h
//=========================================================================
// FILENAME : tagutils-dsf.h
// DESCRIPTION  : DSF metadata reader
//=========================================================================
// Copyright (c) 2014 Takeshich NAKAMURA
//=========================================================================

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
static int _get_dsffileinfo(char *file, struct song_metadata *psong);
static int _get_dsftags(char *file, struct song_metadata *psong);


tagutils/tagutils.c

修改tagutils/tagutils.c支持上面添加的两个对dsd歌曲文件的解析, +代表的是新添加的内容,添加时别把+号带上了, 行数跟版本的原因有可能不匹配,自行上下找一下代码

vim tagutils/tagutils.c

在107到109行左右引入新添加的两个头文件

#include "tagutils-asf.h"
#include "tagutils-wav.h"
#include "tagutils-pcm.h"
+#include "tagutils-dsf.h"
+#include "tagutils-dff.h"

在136行左右在数组中添加dsd歌曲的解析函数

static taghandler taghandlers[] = {
    { "aac", _get_aactags, _get_aacfileinfo                                  },
    { "mp3", _get_mp3tags, _get_mp3fileinfo                                  },
    { "flc", _get_flctags, _get_flcfileinfo                                  },
#ifdef HAVE_VORBISFILE
    { "ogg", 0,            _get_oggfileinfo                                  },
#endif
    { "asf", 0,            _get_asffileinfo                                  },
    { "wav", _get_wavtags, _get_wavfileinfo                                  },
    { "pcm", 0,            _get_pcmfileinfo                                  },
+   { "dsf", _get_dsftags, _get_dsffileinfo                                  },
+   { "dff", 0,            _get_dfffileinfo                                  },
    { NULL,  0 }
};

在155行左右添加新添加的两个c文件

#include "tagutils-asf.c"
#include "tagutils-wav.c"
#include "tagutils-pcm.c"
+#include "tagutils-dsf.c"
+#include "tagutils-dff.c"
#include "tagutils-plist.c"

upnpglobalvars.h

添加全局变量

vim upnpglobalvars.h
    "http-get:*:audio/mp4:*," \
    "http-get:*:audio/x-wav:*," \
    "http-get:*:audio/x-flac:*," \
+   "http-get:*:audio/x-dsd:*," \
    "http-get:*:application/ogg:*"

utils.c

vim utils.c

在345行左右添加

                return "3gp";
            else if( strcmp(mime, "application/ogg") == 0 )
                return "ogg";
+           else if( strcmp(mime+6, "x-dsd") == 0 )
+               return "dsd";
            break;
        case 'v':
            if( strcmp(mime+6, "avi") == 0 )

在418行左右添加

int
is_audio(const char * file)
{
    return (ends_with(file, ".mp3") || ends_with(file, ".flac") ||
        ends_with(file, ".wma") || ends_with(file, ".asf")  ||
        ends_with(file, ".fla") || ends_with(file, ".flc")  ||
        ends_with(file, ".m4a") || ends_with(file, ".aac")  ||
        ends_with(file, ".mp4") || ends_with(file, ".m4p")  ||
        ends_with(file, ".wav") || ends_with(file, ".ogg")  ||
+       ends_with(file, ".pcm") || ends_with(file, ".3gp")  ||
+       ends_with(file, ".dsf") || ends_with(file, ".dff"));
}

交叉编译minidlna

# 首先运行脚本文件进行检查,automake 和 autoconf 是否需要下载,需要下载自行下载就好
$ ./autogen.sh
$ CC=aarch64-linux-gnu-gcc \
CFLAGS=-I/usr/src/aarch64-linux-gnu/include \
CPPFLAGS=-I/usr/src/aarch64-linux-gnu/include \
LDFLAGS=-L/usr/src/aarch64-linux-gnu/lib \
LIBS="-lpthread -lexif -ljpeg -lsqlite3 -lavformat -lavcodec -lswresample -lavutil -lz -lid3tag -lFLAC -logg -lvorbis -ldl -lm" \
./configure --host=aarch64-linux-gnu --prefix=/home/ywh/build/minidlna/usr-lib
# 记得make之前通过下面的记录更改源码文件
$ sudo make && sudo make install
# make 以后会在当前目录生成minidlnad命令和minidlna.conf配置文件,如何配置可以参考官方文档
# 缩小静态编译文件
$ aarch64-linux-gnu-strip minidlnad