Advertisement
schorman

wav2dts (Updated for LD)

Aug 12th, 2018
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.95 KB | None | 0 0
  1. /*
  2. wav2dts try to convert a dtswav file (dts 14 le with a wav header) to a standard dts 16 be.
  3.  
  4. Must work in CLI mode.
  5.  
  6. Copyright (C) 2017 by Tebasuna
  7.  
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation; either
  11. version 2.1 of the License, or (at your option) any later version.
  12.  
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. Lesser General Public License for more details.
  17.  
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  
  22. Updated 2018 by schorman  
  23.         -Removed search restrictions for frame headers.  It's slow, but I don't really want to try to make it faster.  
  24.         -Now truncates file after the last valid DTS frame.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29.  
  30. // Global variables
  31. FILE *fp;                               // Input file pointer
  32. unsigned long FileSize;
  33. unsigned int FSIZE, AMODE, SFREQ, RATE;
  34. unsigned char bufi[8], bufo[7];         // buffers to transfer data
  35. ////////////////////////////////////////////////////////////////////////
  36. //      SearchFirstFrame
  37. ////////////////////////////////////////////////////////////////////////
  38. static unsigned int SearchFirstFrame(int offset) {
  39.     unsigned int i;
  40.     unsigned int j = FileSize;
  41.     unsigned char ch1, ch2, ch3, ch4, ch5;                   // To read only 1 byte
  42.     for(i=0; i<(FileSize-1); i++) {
  43.         if (FileSize > (long)(i + offset + 9)) {
  44.             fseek(fp, (long)(i + offset), SEEK_SET);
  45.             if ((fgetc(fp)== 0xFF)) {                        // First header byte
  46.                 if ((fgetc(fp)== 0x1F)) {                    // and next
  47.                     if ((fgetc(fp)== 0x00)) {
  48.                         if ((fgetc(fp)== 0xE8)) {
  49.                             if ((fgetc(fp) & 0xF0)== 0xF0) {
  50.                                 if ((fgetc(fp)== 0x07)) {    // header found
  51.                                     ch1 = fgetc(fp);
  52.                                     ch2 = fgetc(fp);
  53.                                     ch3 = fgetc(fp);
  54.                                     ch4 = fgetc(fp);
  55.                                     ch5 = fgetc(fp);
  56.                                     ch5 = fgetc(fp);
  57.                                     FSIZE = ((ch1)<<4) + ((ch2 & 0x03)<<12) + ((ch4 & 0x7C)>>2);
  58.                                     AMODE = ((ch3 & 0xF0)>>4) + ((ch4 & 0x03)<<4);
  59.                                     SFREQ = (ch3 & 0x0F);
  60.                                     RATE  = ((ch5 & 0x3E)>>1);
  61.                                     j = i;
  62.                                     i = (FileSize-1);
  63.                                 }
  64.                             }
  65.                         }
  66.                     }
  67.                 }
  68.             }
  69.         } else i = (FileSize-1);
  70.     }
  71.     return j;
  72. }
  73. ////////////////////////////////////////////////////////////////////////
  74. //      SearchLastFrame
  75. ////////////////////////////////////////////////////////////////////////
  76. static unsigned int SearchLastFrame(int offset) {
  77.     unsigned int m;
  78.     unsigned int n = FileSize;
  79.     for(m=0; m<(FileSize-1); m++) {
  80.         fseek(fp, (long)(m*(-1) + offset), SEEK_END);
  81.         if ((fgetc(fp)== 0xFF)) {                        // First header byte
  82.             if ((fgetc(fp)== 0x1F)) {                    // and next
  83.                 if ((fgetc(fp)== 0x00)) {
  84.                     if ((fgetc(fp)== 0xE8)) {
  85.                         if ((fgetc(fp) & 0xF0)== 0xF0) {
  86.                             if ((fgetc(fp)== 0x07)) {    // header found
  87.                                 n = (m - 4094);
  88.                                 m = (FileSize-1);
  89.                             }
  90.                         }
  91.                     }
  92.                 }
  93.             }
  94.         }
  95.     }
  96.     return n;
  97. }
  98. ////////////////////////////////////////////////////////////////////////
  99. //      Convert 14le to 16be
  100. ////////////////////////////////////////////////////////////////////////
  101. static void Convert14to16(void) {
  102.     bufo[0] = ((bufi[1]  & 0x3F)<<2) + ((bufi[0]  & 0xC0)>>6);
  103.     bufo[1] = ((bufi[0]  & 0x3F)<<2) + ((bufi[3]  & 0x30)>>4);
  104.     bufo[2] = ((bufi[3]  & 0x0F)<<4) + ((bufi[2]  & 0xF0)>>4);
  105.     bufo[3] = ((bufi[2]  & 0x0F)<<4) + ((bufi[5]  & 0x3C)>>2);
  106.     bufo[4] = ((bufi[5]  & 0x03)<<6) + ((bufi[4]  & 0xFC)>>2);
  107.     bufo[5] = ((bufi[4]  & 0x03)<<6) +  (bufi[7]  & 0x3f);
  108.     bufo[6] =   bufi[6];
  109. }
  110. ////////////////////////////////////////////////////////////////////////
  111. //      Main
  112. ////////////////////////////////////////////////////////////////////////
  113.  
  114. int main(int argc, char** argv) {
  115.   // Local variables
  116.     unsigned int IniDelay, FrameSize14, EndDelay, FrameSize16, NumFrames, NumBlocks;
  117.     unsigned char ch5, ch6, ch7;
  118.     int i, j, k;
  119.     FILE *fo;                          // output file
  120.     char s2[256];                      // Output file name
  121.     char s3[] = ".dts";                // to append
  122.  
  123. // see if correct number of command line arguments.
  124.     if (argc < 2) {
  125.         printf("Convert a DTSWAV 14le to a standard DTS 16be.\n");
  126.         printf("\n");
  127.         printf("Usage: wav2dts <dtswav_filename>\n");
  128.         printf("\n");
  129.         exit(1);
  130.     }
  131. // open file for input
  132.     if ((fp = fopen(argv[1], "rb"))==NULL)  {
  133.         printf("Cannot open input file\n");
  134.         exit(2);
  135.     }
  136. // Gets FileSize
  137.     fseek(fp, (long)0, SEEK_END);               // SEEK_END  final
  138.     FileSize = ftell(fp);
  139.  
  140. // Search for first valid frame. Initial 'delay' accepted and reported
  141.     if ((IniDelay = SearchFirstFrame(0))==FileSize) {
  142.         printf("Didn't find any valid dts headers.\n");
  143.         exit(3);
  144.     }
  145.     if ((FrameSize14 = SearchFirstFrame(IniDelay + 1))==FileSize) {
  146.         printf("Didn't find any valid dts headers.\n");
  147.         exit(4);
  148.     }
  149. // Search for last valid frame. End 'delay' accepted and reported
  150.     if ((EndDelay = SearchLastFrame(-2))==FileSize) {
  151.         printf("Didn't find any valid dts headers.\n");
  152.         exit(3);
  153.     }
  154.     FrameSize14 += 1;
  155.     FrameSize16 = FrameSize14 * 14 / 16;
  156.     NumFrames = (FileSize - IniDelay - EndDelay) / FrameSize14;
  157. // Show info
  158.     printf("FileSize : %u bytes\r\n", FileSize);
  159.     printf("Initial bytes rejected : %u\r\n", IniDelay);
  160.     printf("Last bytes rejected : %u\r\n", EndDelay);
  161.     printf("-------------   Valid Header\r\n");
  162.     printf("FrameSizeRead   %u bytes.\r\n", FSIZE + 1);
  163.     printf("FrameSize14le : %u bytes.\r\n", FrameSize14);
  164.     printf("FrameSize16be : %u bytes.\r\n", FrameSize16);
  165.     printf("NumFrames     : %u.\r\n", NumFrames);
  166.     if (AMODE == 9) printf("Channels mode : Surround.\r\n");
  167.     if (SFREQ == 8) printf("SampleRate    : 44.1 KHz,\r\n");
  168.  
  169.     if ((FrameSize14 % 8) == 0) {
  170. // Name for dts output file
  171.         strcpy(s2, argv[1]);               // copy input file name
  172.         k = strlen(s2) - 4;                // - ".wav"
  173.         for (j=0; j<4; j++) s2[j+k]=s3[j];   // append ".dts"
  174.         if ((fo = fopen(s2, "wb"))==NULL)  {
  175.            printf("Cannot open output file\n");
  176.            exit(5);
  177.         }
  178.  
  179.         fseek(fp, (long)IniDelay, SEEK_SET);                 // Begin of input DTS 14le stream
  180.         NumBlocks = FrameSize14 / 8;
  181.         FrameSize16 -= 1;                                    // to correct
  182.         ch5 = (unsigned char)(FrameSize16 / 4096);
  183.         FrameSize16 = (FrameSize16 % 4096);
  184.         ch6 = (unsigned char)(FrameSize16 / 16);
  185.         ch7 = ((unsigned char)(FrameSize16 % 16)<<4);
  186.  
  187.         for (i=0; i<NumFrames; i++) {
  188.             fread(&bufi, sizeof (unsigned char), 8, fp);     // read 8 bytes
  189.             Convert14to16();
  190.             bufo[5] = (bufo[5] & 0xFC) + ch5;                // modify header FrameSize
  191.             bufo[6] = ch6;
  192.             fwrite(bufo, sizeof (unsigned char), 7, fo);     // write 7 bytes
  193.             fread(&bufi, sizeof (unsigned char), 8, fp);     // read 8 bytes
  194.             Convert14to16();
  195.             bufo[0] = (bufo[0] & 0x0F) + ch7;
  196.             if (RATE == 29) {                                // modify header RATE
  197.                 bufo[1] = (bufo[1] & 0xFC) + 0x02;           // by 22 (1411,2 Kb/s)
  198.                 bufo[2] = (bufo[2] & 0x1F) + 0xC0;
  199.             }
  200.             fwrite(bufo, sizeof (unsigned char), 7, fo);     // write 7 bytes
  201.  
  202.             for (j=2; j<NumBlocks; j++) {
  203.                 fread(&bufi, sizeof (unsigned char), 8, fp); // read 8 bytes
  204.                 Convert14to16();
  205.                 fwrite(bufo, sizeof (unsigned char), 7, fo); // write 7 bytes
  206.             }
  207.         }
  208.         fclose(fo);
  209.         printf("Done, dts created.\r\n");
  210.     } else printf("Sorry, only work with FrameSize14le divisible by 8.\r\n");
  211. //
  212.     fclose(fp);
  213.     return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement