Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- wav2dts try to convert a dtswav file (dts 14 le with a wav header) to a standard dts 16 be.
- Must work in CLI mode.
- Copyright (C) 2017 by Tebasuna
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
- This library 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
- Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- Updated 2018 by schorman
- -Removed search restrictions for frame headers. It's slow, but I don't really want to try to make it faster.
- -Now truncates file after the last valid DTS frame.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // Global variables
- FILE *fp; // Input file pointer
- unsigned long FileSize;
- unsigned int FSIZE, AMODE, SFREQ, RATE;
- unsigned char bufi[8], bufo[7]; // buffers to transfer data
- ////////////////////////////////////////////////////////////////////////
- // SearchFirstFrame
- ////////////////////////////////////////////////////////////////////////
- static unsigned int SearchFirstFrame(int offset) {
- unsigned int i;
- unsigned int j = FileSize;
- unsigned char ch1, ch2, ch3, ch4, ch5; // To read only 1 byte
- for(i=0; i<(FileSize-1); i++) {
- if (FileSize > (long)(i + offset + 9)) {
- fseek(fp, (long)(i + offset), SEEK_SET);
- if ((fgetc(fp)== 0xFF)) { // First header byte
- if ((fgetc(fp)== 0x1F)) { // and next
- if ((fgetc(fp)== 0x00)) {
- if ((fgetc(fp)== 0xE8)) {
- if ((fgetc(fp) & 0xF0)== 0xF0) {
- if ((fgetc(fp)== 0x07)) { // header found
- ch1 = fgetc(fp);
- ch2 = fgetc(fp);
- ch3 = fgetc(fp);
- ch4 = fgetc(fp);
- ch5 = fgetc(fp);
- ch5 = fgetc(fp);
- FSIZE = ((ch1)<<4) + ((ch2 & 0x03)<<12) + ((ch4 & 0x7C)>>2);
- AMODE = ((ch3 & 0xF0)>>4) + ((ch4 & 0x03)<<4);
- SFREQ = (ch3 & 0x0F);
- RATE = ((ch5 & 0x3E)>>1);
- j = i;
- i = (FileSize-1);
- }
- }
- }
- }
- }
- }
- } else i = (FileSize-1);
- }
- return j;
- }
- ////////////////////////////////////////////////////////////////////////
- // SearchLastFrame
- ////////////////////////////////////////////////////////////////////////
- static unsigned int SearchLastFrame(int offset) {
- unsigned int m;
- unsigned int n = FileSize;
- for(m=0; m<(FileSize-1); m++) {
- fseek(fp, (long)(m*(-1) + offset), SEEK_END);
- if ((fgetc(fp)== 0xFF)) { // First header byte
- if ((fgetc(fp)== 0x1F)) { // and next
- if ((fgetc(fp)== 0x00)) {
- if ((fgetc(fp)== 0xE8)) {
- if ((fgetc(fp) & 0xF0)== 0xF0) {
- if ((fgetc(fp)== 0x07)) { // header found
- n = (m - 4094);
- m = (FileSize-1);
- }
- }
- }
- }
- }
- }
- }
- return n;
- }
- ////////////////////////////////////////////////////////////////////////
- // Convert 14le to 16be
- ////////////////////////////////////////////////////////////////////////
- static void Convert14to16(void) {
- bufo[0] = ((bufi[1] & 0x3F)<<2) + ((bufi[0] & 0xC0)>>6);
- bufo[1] = ((bufi[0] & 0x3F)<<2) + ((bufi[3] & 0x30)>>4);
- bufo[2] = ((bufi[3] & 0x0F)<<4) + ((bufi[2] & 0xF0)>>4);
- bufo[3] = ((bufi[2] & 0x0F)<<4) + ((bufi[5] & 0x3C)>>2);
- bufo[4] = ((bufi[5] & 0x03)<<6) + ((bufi[4] & 0xFC)>>2);
- bufo[5] = ((bufi[4] & 0x03)<<6) + (bufi[7] & 0x3f);
- bufo[6] = bufi[6];
- }
- ////////////////////////////////////////////////////////////////////////
- // Main
- ////////////////////////////////////////////////////////////////////////
- int main(int argc, char** argv) {
- // Local variables
- unsigned int IniDelay, FrameSize14, EndDelay, FrameSize16, NumFrames, NumBlocks;
- unsigned char ch5, ch6, ch7;
- int i, j, k;
- FILE *fo; // output file
- char s2[256]; // Output file name
- char s3[] = ".dts"; // to append
- // see if correct number of command line arguments.
- if (argc < 2) {
- printf("Convert a DTSWAV 14le to a standard DTS 16be.\n");
- printf("\n");
- printf("Usage: wav2dts <dtswav_filename>\n");
- printf("\n");
- exit(1);
- }
- // open file for input
- if ((fp = fopen(argv[1], "rb"))==NULL) {
- printf("Cannot open input file\n");
- exit(2);
- }
- // Gets FileSize
- fseek(fp, (long)0, SEEK_END); // SEEK_END final
- FileSize = ftell(fp);
- // Search for first valid frame. Initial 'delay' accepted and reported
- if ((IniDelay = SearchFirstFrame(0))==FileSize) {
- printf("Didn't find any valid dts headers.\n");
- exit(3);
- }
- if ((FrameSize14 = SearchFirstFrame(IniDelay + 1))==FileSize) {
- printf("Didn't find any valid dts headers.\n");
- exit(4);
- }
- // Search for last valid frame. End 'delay' accepted and reported
- if ((EndDelay = SearchLastFrame(-2))==FileSize) {
- printf("Didn't find any valid dts headers.\n");
- exit(3);
- }
- FrameSize14 += 1;
- FrameSize16 = FrameSize14 * 14 / 16;
- NumFrames = (FileSize - IniDelay - EndDelay) / FrameSize14;
- // Show info
- printf("FileSize : %u bytes\r\n", FileSize);
- printf("Initial bytes rejected : %u\r\n", IniDelay);
- printf("Last bytes rejected : %u\r\n", EndDelay);
- printf("------------- Valid Header\r\n");
- printf("FrameSizeRead %u bytes.\r\n", FSIZE + 1);
- printf("FrameSize14le : %u bytes.\r\n", FrameSize14);
- printf("FrameSize16be : %u bytes.\r\n", FrameSize16);
- printf("NumFrames : %u.\r\n", NumFrames);
- if (AMODE == 9) printf("Channels mode : Surround.\r\n");
- if (SFREQ == 8) printf("SampleRate : 44.1 KHz,\r\n");
- if ((FrameSize14 % 8) == 0) {
- // Name for dts output file
- strcpy(s2, argv[1]); // copy input file name
- k = strlen(s2) - 4; // - ".wav"
- for (j=0; j<4; j++) s2[j+k]=s3[j]; // append ".dts"
- if ((fo = fopen(s2, "wb"))==NULL) {
- printf("Cannot open output file\n");
- exit(5);
- }
- fseek(fp, (long)IniDelay, SEEK_SET); // Begin of input DTS 14le stream
- NumBlocks = FrameSize14 / 8;
- FrameSize16 -= 1; // to correct
- ch5 = (unsigned char)(FrameSize16 / 4096);
- FrameSize16 = (FrameSize16 % 4096);
- ch6 = (unsigned char)(FrameSize16 / 16);
- ch7 = ((unsigned char)(FrameSize16 % 16)<<4);
- for (i=0; i<NumFrames; i++) {
- fread(&bufi, sizeof (unsigned char), 8, fp); // read 8 bytes
- Convert14to16();
- bufo[5] = (bufo[5] & 0xFC) + ch5; // modify header FrameSize
- bufo[6] = ch6;
- fwrite(bufo, sizeof (unsigned char), 7, fo); // write 7 bytes
- fread(&bufi, sizeof (unsigned char), 8, fp); // read 8 bytes
- Convert14to16();
- bufo[0] = (bufo[0] & 0x0F) + ch7;
- if (RATE == 29) { // modify header RATE
- bufo[1] = (bufo[1] & 0xFC) + 0x02; // by 22 (1411,2 Kb/s)
- bufo[2] = (bufo[2] & 0x1F) + 0xC0;
- }
- fwrite(bufo, sizeof (unsigned char), 7, fo); // write 7 bytes
- for (j=2; j<NumBlocks; j++) {
- fread(&bufi, sizeof (unsigned char), 8, fp); // read 8 bytes
- Convert14to16();
- fwrite(bufo, sizeof (unsigned char), 7, fo); // write 7 bytes
- }
- }
- fclose(fo);
- printf("Done, dts created.\r\n");
- } else printf("Sorry, only work with FrameSize14le divisible by 8.\r\n");
- //
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement