"Fossies" - the Fresh Open Source Software Archive

Member "minidlna-1.3.0/tagutils/tagutils-dff.c" (24 Nov 2020, 11955 Bytes) of package /linux/privat/minidlna-1.3.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "tagutils-dff.c" see the Fossies "Dox" file reference documentation.

    1 //=========================================================================
    2 // FILENAME     : tagutils-dff.c
    3 // DESCRIPTION  : DFF metadata reader
    4 //=========================================================================
    5 // Copyright (c) 2014 Takeshich NAKAMURA
    6 //=========================================================================
    7 
    8 /*
    9  * This program is free software; you can redistribute it and/or modify
   10  * it under the terms of the GNU General Public License as published by
   11  * the Free Software Foundation; either version 2 of the License, or
   12  * (at your option) any later version.
   13  *
   14  * This program is distributed in the hope that it will be useful,
   15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17  * GNU General Public License for more details.
   18  *
   19  * You should have received a copy of the GNU General Public License
   20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
   21  */
   22 #define GET_DFF_INT64(p) ((((uint64_t)((p)[0])) << 56) |   \
   23               (((uint64_t)((p)[1])) << 48) |   \
   24               (((uint64_t)((p)[2])) << 40) |   \
   25               (((uint64_t)((p)[3])) << 32) |   \
   26               (((uint64_t)((p)[4])) << 24) |   \
   27               (((uint64_t)((p)[5])) << 16) |   \
   28               (((uint64_t)((p)[6])) << 8) |    \
   29               (((uint64_t)((p)[7]))))
   30 
   31 #define GET_DFF_INT32(p) ((((uint32_t)((p)[0])) << 24) |   \
   32               (((uint32_t)((p)[1])) << 16) |   \
   33               (((uint32_t)((p)[2])) << 8) |     \
   34               (((uint32_t)((p)[3]))))
   35 
   36 #define GET_DFF_INT16(p) ((((uint16_t)((p)[0])) << 8) |   \
   37               (((uint16_t)((p)[1]))))
   38 
   39 static int
   40 _get_dfffileinfo(char *file, struct song_metadata *psong)
   41 {
   42     FILE *fp;
   43     uint32_t len;
   44     uint32_t rt;
   45     unsigned char hdr[32] = { 0 };
   46 
   47     uint64_t totalsize = 0;
   48     uint64_t propckDataSize = 0;
   49     uint64_t count = 0;
   50     uint32_t samplerate = 0;
   51     uint16_t channels = 0;
   52     //DST
   53     uint64_t dstickDataSize = 0;
   54     uint32_t numFrames = 0;
   55     uint16_t frameRate = 0;
   56     unsigned char frteckData[18] = { 0 };
   57     unsigned char dstickData[12] = { 0 };
   58     uint64_t totalcount = 0;
   59     unsigned char ckbuf[12] = { 0 };
   60     unsigned char compressionType[4] = { 0 };
   61     unsigned char dsdsdckData[12] = { 0 };
   62     uint64_t dsdsdckDataSize = 0;
   63     uint64_t cmprckDataSize = 0;
   64     uint64_t abssckDataSize = 0;
   65     uint64_t lscockDataSize = 0;
   66     uint64_t comtckDataSize = 0;
   67     uint64_t diinckDataSize = 0;
   68     uint64_t diarckDataSize = 0;
   69     uint64_t ditickDataSize = 0;
   70     uint64_t manfckDataSize = 0;
   71 
   72     //DPRINTF(E_DEBUG,L_SCANNER,"Getting DFF fileinfo =%s\n",file);
   73 
   74     if ((fp = fopen(file, "rb")) == NULL)
   75     {
   76         DPRINTF(E_WARN, L_SCANNER, "Could not create file handle\n");
   77         return -1;
   78     }
   79 
   80     len = 32;
   81     //Form DSD chunk
   82     if (!(rt = fread(hdr, len, 1, fp)))
   83     {
   84         DPRINTF(E_WARN, L_SCANNER, "Could not read Form DSD chunk from %s\n", file);
   85         fclose(fp);
   86         return -1;
   87     }
   88 
   89     if (strncmp((char*)hdr, "FRM8", 4))
   90     {
   91         DPRINTF(E_WARN, L_SCANNER, "Invalid Form DSD chunk in %s\n", file);
   92         fclose(fp);
   93         return -1;
   94     }
   95 
   96     totalsize = GET_DFF_INT64(hdr + 4);
   97 
   98     if (strncmp((char*)hdr + 12, "DSD ", 4))
   99     {
  100         DPRINTF(E_WARN, L_SCANNER, "Invalid Form DSD chunk in %s\n", file);
  101         fclose(fp);
  102         return -1;
  103     }
  104 
  105     //FVER chunk
  106     if (strncmp((char*)hdr + 16, "FVER", 4))
  107     {
  108         DPRINTF(E_WARN, L_SCANNER, "Invalid Format Version Chunk in %s\n", file);
  109         fclose(fp);
  110         return -1;
  111     }
  112 
  113     totalsize -= 16;
  114     while (totalcount < totalsize - 4)
  115     {
  116         if (!(rt = fread(ckbuf, sizeof(ckbuf), 1, fp)))
  117         {
  118             //DPRINTF(E_WARN, L_SCANNER, "Could not read chunk header from %s\n", file);
  119             //fclose(fp);
  120             //return -1;
  121             break;
  122         }
  123 
  124         //Property chunk
  125         if (strncmp((char*)ckbuf, "PROP", 4) == 0)
  126         {
  127             propckDataSize = GET_DFF_INT64(ckbuf + 4);
  128             totalcount += propckDataSize + 12;
  129 
  130             unsigned char propckData[propckDataSize];
  131 
  132             if (!(rt = fread(propckData, propckDataSize, 1, fp)))
  133             {
  134                 DPRINTF(E_WARN, L_SCANNER, "Could not read Property chunk from %s\n", file);
  135                 fclose(fp);
  136                 return -1;
  137             }
  138 
  139             if (strncmp((char*)propckData, "SND ", 4))
  140             {
  141                 DPRINTF(E_WARN, L_SCANNER, "Invalid Property chunk in %s\n", file);
  142                 fclose(fp);
  143                 return -1;
  144             }
  145 
  146             count += 4;
  147             while (count < propckDataSize)
  148             {
  149                 if (strncmp((char*)propckData + count, "FS  ", 4) == 0)
  150                 {
  151                     //Sample Rate Chunk
  152                     count += 12;
  153                     samplerate = GET_DFF_INT32(propckData + count);
  154                     psong->samplerate = samplerate;
  155                     count += 4;
  156 
  157                     //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "Sample Rate is %d\n", psong->samplerate);
  158                 }
  159                 else if (strncmp((char*)propckData + count, "CHNL", 4) == 0)
  160                 {
  161                     //Channels Chunk
  162                     count += 12;
  163                     channels = GET_DFF_INT16(propckData + count);
  164                     psong->channels = channels;
  165                     count += channels * 4 + 2;
  166 
  167                     //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "channels is %d\n", channels);
  168                 }
  169                 else if (strncmp((char*)propckData + count, "CMPR", 4) == 0)
  170                 {
  171                     //Compression Type Chunk
  172                     count += 4;
  173                     cmprckDataSize = GET_DFF_INT64(propckData + count);
  174                     count += 8;
  175                     memcpy(compressionType, propckData + count, 4);
  176                     count += cmprckDataSize;
  177                 }
  178                 else if (strncmp((char*)propckData + count, "ABSS", 4) == 0)
  179                 {
  180                     //Absolute Start Time Chunk
  181                     count += 4;
  182                     abssckDataSize = GET_DFF_INT64(propckData + count);
  183                     count += abssckDataSize + 8;
  184                 }
  185                 else if (strncmp((char*)propckData + count, "LSCO", 4) == 0)
  186                 {
  187                     //Loudsperaker Configuration Chunk
  188                     count += 4;
  189                     lscockDataSize = GET_DFF_INT64(propckData + count);
  190                     count += lscockDataSize + 8;
  191                 }
  192                 else
  193                 {
  194                     break;
  195                 }
  196             }
  197 
  198             //bitrate bitpersample is 1bit
  199             psong->bitrate = channels * samplerate * 1;
  200 
  201             //DSD/DST Sound Data Chunk
  202             len = 12;
  203             if (!(rt = fread(dsdsdckData, len, 1, fp)))
  204             {
  205                 DPRINTF(E_WARN, L_SCANNER, "Could not read DSD/DST Sound Data chunk from %s\n", file);
  206                 fclose(fp);
  207                 return -1;
  208             }
  209 
  210             if (strncmp((char*)compressionType, (char*)dsdsdckData, 4))
  211             {
  212                 DPRINTF(E_WARN, L_SCANNER, "Invalid DSD/DST Sound Data chunk in %s\n", file);
  213                 fclose(fp);
  214                 return -1;
  215             }
  216 
  217             if (strncmp((char*)dsdsdckData, "DSD ", 4) == 0)
  218             {
  219                 //DSD
  220                 dsdsdckDataSize = GET_DFF_INT64(dsdsdckData + 4);
  221                 totalcount += dsdsdckDataSize + 12;
  222                 psong->song_length = (int)((double)dsdsdckDataSize / (double)samplerate / (double)channels * 8 * 1000);
  223 
  224                 //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "songlength is %d\n", psong->song_length);
  225 
  226                 fseeko(fp, dsdsdckDataSize, SEEK_CUR);
  227             }
  228             else if (strncmp((char*)dsdsdckData, "DST ", 4) == 0)
  229             {
  230                 //DST
  231                 dsdsdckDataSize = GET_DFF_INT64(dsdsdckData + 4);
  232                 totalcount += dsdsdckDataSize + 12;
  233 
  234                 //DST Frame Information chunk
  235                 if (!(rt = fread(frteckData, 18, 1, fp)))
  236                 {
  237                     DPRINTF(E_WARN, L_SCANNER, "Could not read DST Frame Information chunk from %s\n", file);
  238                     fclose(fp);
  239                     return -1;
  240                 }
  241 
  242                 if (strncmp((char*)frteckData, "FRTE", 4) == 0)
  243                 {
  244                     //uint64_t frteckDataSize = GET_DFF_INT64(frteckData+4);
  245                     numFrames = GET_DFF_INT32((char*)frteckData + 12);
  246                     frameRate = GET_DFF_INT16((char*)frteckData + 16);
  247 
  248                     psong->song_length = numFrames / frameRate * 1000;
  249 
  250                     fseeko(fp, dsdsdckDataSize - 18, SEEK_CUR);
  251                 }
  252                 else
  253                 {
  254                     DPRINTF(E_WARN, L_SCANNER, "Invalid DST Frame Information chunk in %s\n", file);
  255                     fclose(fp);
  256                     return -1;
  257                 }
  258 
  259                 //DST Sound Index Chunk
  260                 if (!(rt = fread(dstickData, 12, 1, fp)))
  261                 {
  262                     if (ferror(fp))
  263                     {
  264                         DPRINTF(E_WARN, L_SCANNER, "Could not read DST Sound Index chunk from %s\n", file);
  265                         fclose(fp);
  266                         return -1;
  267                     }
  268                     else
  269                     {
  270                         //EOF
  271                         break;
  272                     }
  273                 }
  274 
  275                 if (strncmp((char*)dstickData, "DSTI", 4) == 0)
  276                 {
  277                     dstickDataSize = GET_DFF_INT64(dstickData + 4);
  278                     totalcount += dstickDataSize + 12;
  279                     fseeko(fp, dstickDataSize, SEEK_CUR);
  280                 }
  281                 else
  282                 {
  283                     fseeko(fp, -12, SEEK_CUR);
  284                 }
  285             }
  286             else
  287             {
  288                 DPRINTF(E_WARN, L_SCANNER, "Invalid DSD/DST Sound Data chunk in %s\n", file);
  289                 fclose(fp);
  290                 return -1;
  291             }
  292         }
  293         else if (!strncmp((char*)ckbuf, "COMT", 4))
  294         {
  295             //COMT Chunk
  296             comtckDataSize = GET_DFF_INT64(ckbuf + 4);
  297             totalcount += comtckDataSize + 12;
  298             fseeko(fp, comtckDataSize, SEEK_CUR);
  299         }
  300         else if (!strncmp((char*)ckbuf, "DIIN", 4))
  301         {
  302             //Edited Master Information chunk
  303             diinckDataSize = GET_DFF_INT64(ckbuf + 4);
  304             unsigned char diinckData[diinckDataSize];
  305             totalcount += diinckDataSize + 12;
  306 
  307             if (!(rt = fread(diinckData, diinckDataSize, 1, fp)))
  308             {
  309                 DPRINTF(E_WARN, L_SCANNER, "Could not read Edited Master Information chunk from %s\n", file);
  310                 fclose(fp);
  311                 return -1;
  312             }
  313 
  314             uint64_t icount = 0;
  315             while (icount < diinckDataSize)
  316             {
  317                 if (!strncmp((char*)diinckData + icount, "EMID", 4))
  318                 {
  319                     //Edited Master ID chunk
  320                     icount += 4;
  321                     icount += GET_DFF_INT64(diinckData + icount) + 8;
  322                 }
  323                 else if (!strncmp((char*)diinckData + icount, "MARK", 4))
  324                 {
  325                     //Master Chunk
  326                     icount += 4;
  327                     icount += GET_DFF_INT64(diinckData + icount) + 8;
  328                 }
  329                 else if (!strncmp((char*)diinckData + icount, "DIAR", 4))
  330                 {
  331                     //Artist Chunk
  332                     icount += 4;
  333                     diarckDataSize = GET_DFF_INT64(diinckData + icount);
  334                     unsigned char arttext[diarckDataSize + 1 - 4];
  335 
  336                     icount += 12;
  337 
  338                     memset(arttext, 0x00, sizeof(arttext));
  339                     strncpy((char*)arttext, (char*)diinckData + icount, sizeof(arttext) - 1);
  340                     psong->contributor[ROLE_ARTIST] = strdup((char*)&arttext[0]);
  341 
  342                     icount += diarckDataSize - 4;
  343                 }
  344                 else if (!strncmp((char*)diinckData + icount, "DITI", 4))
  345                 {
  346                     //Title Chunk
  347                     icount += 4;
  348                     ditickDataSize = GET_DFF_INT64(diinckData + icount);
  349                     unsigned char titletext[ditickDataSize + 1 - 4];
  350 
  351                     icount += 12;
  352 
  353                     memset(titletext, 0x00, sizeof(titletext));
  354                     strncpy((char*)titletext, (char*)diinckData + icount, sizeof(titletext) - 1);
  355                     psong->title = strdup((char*)&titletext[0]);
  356                     icount += ditickDataSize - 4;
  357                 }
  358                 else
  359                 {
  360                     break;
  361                 }
  362             }
  363         }
  364         else if (!strncmp((char*)ckbuf, "MANF", 4))
  365         {
  366             //Manufacturer Specific Chunk
  367             manfckDataSize = GET_DFF_INT64(ckbuf + 4);
  368             totalcount += manfckDataSize + 12;
  369             fseeko(fp, manfckDataSize, SEEK_CUR);
  370         }
  371     }
  372 
  373     fclose(fp);
  374 
  375     //DPRINTF(E_DEBUG, L_SCANNER, "totalsize is 0x%016lx\n", (long unsigned int)totalsize);
  376     //DPRINTF(E_DEBUG, L_SCANNER, "propckDataSize is 0x%016lx\n", (long unsigned int)propckDataSize);
  377     //DPRINTF(E_DEBUG, L_SCANNER, "cmprckDataSize is 0x%016lx\n", (long unsigned int)cmprckDataSize);
  378     //DPRINTF(E_DEBUG, L_SCANNER, "abssckDataSize is 0x%016lx\n", (long unsigned int)abssckDataSize);
  379     //DPRINTF(E_DEBUG, L_SCANNER, "lscockDataSize is 0x%016lx\n", (long unsigned int)lscockDataSize);
  380     //DPRINTF(E_DEBUG, L_SCANNER, "dsdsdckDataSize is 0x%016lx\n", (long unsigned int)dsdsdckDataSize);
  381     //DPRINTF(E_DEBUG, L_SCANNER, "dstickDataSize is 0x%016lx\n", (long unsigned int)dstickDataSize);
  382     //DPRINTF(E_DEBUG, L_SCANNER, "comtckDataSize is 0x%016lx\n", (long unsigned int)comtckDataSize);
  383     //DPRINTF(E_DEBUG, L_SCANNER, "diinckDataSize is 0x%016lx\n", (long unsigned int)diinckDataSize);
  384     //DPRINTF(E_DEBUG, L_SCANNER, "diarckDataSize is 0x%016lx\n", (long unsigned int)diarckDataSize);
  385     //DPRINTF(E_DEBUG, L_SCANNER, "ditickDataSize is 0x%016lx\n", (long unsigned int)ditickDataSize);
  386     //DPRINTF(E_DEBUG, L_SCANNER, "manfckDataSize is 0x%016lx\n", (long unsigned int)manfckDataSize);
  387 
  388 
  389     //DPRINTF(E_DEBUG, L_SCANNER, "Got dff fileinfo successfully=%s\n", file);
  390     //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "TITLE is %s\n",psong->title );
  391     //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "ARTIST is %s\n",psong->contributor[ROLE_ARTIST]);
  392     //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "samplerate is  %d\n", psong->samplerate);
  393     //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "song_length is  %d\n", psong->song_length);
  394     //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "channels are  %d\n", psong->channels);
  395     //DEBUG DPRINTF(E_DEBUG, L_SCANNER, "bitrate is  %d\n", psong->bitrate);
  396 
  397     xasprintf(&(psong->dlna_pn), "DFF");
  398     return 0;
  399 }