// DumpTS audio hevc to aac
// Audio source only
// by 293
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
FILE *fp_src, *fp_dst;
int i;
long input_file_size;
int data[4] = {0, 0, 0, 0};
long int pos, pos_prev = 0, bytes;
unsigned int total_frame = 0;
if(argc < 3){
printf("dt2aac input output\r\n");
return -1;
}
if((fp_src = fopen(argv[1], "rb")) == NULL ) {
perror("input file");
exit(EXIT_FAILURE);
}
fseek(fp_src, 0L, SEEK_END);
input_file_size = ftell(fp_src);
fseek(fp_src, 0L, SEEK_SET);
if((fp_dst = fopen(argv[2], "wb")) == NULL ) {
perror(NULL);
exit(EXIT_FAILURE);
}
fseek(fp_dst, 0L, SEEK_SET);
printf("dt2aac processing...\r\n0%%");
while(!feof(fp_src)){
if(ferror(fp_src)){
perror(NULL);
exit(EXIT_FAILURE);
}
if(ferror(fp_dst)){
perror(NULL);
exit(EXIT_FAILURE);
}
data[0] = fgetc(fp_src);
if((data[0] == 0x90) && (data[1] == 0x11) && (data[2] == 0x00) && (data[3] == 0x20)){
pos = ftell(fp_src);
bytes = pos - pos_prev;
if(bytes > 8){
fputc(0x56, fp_dst);
fputc((((int)bytes >> 8) & 0x1F) | 0xE0, fp_dst);
fputc((int)bytes & 0xFF, fp_dst);
fputc(0x20, fp_dst);
fputc(0x00, fp_dst);
fputc(0x11, fp_dst);
fputc(0x90, fp_dst);
fseek(fp_src, pos_prev, SEEK_SET);
for(i = 0;i < (bytes - 4);i++){
fputc(fgetc(fp_src), fp_dst);
}
// printf("offset:%08ld bytes:%04lX\r\n", pos, bytes);
printf("\r%d%%", (int)((float)ftell(fp_src) / (float)input_file_size * 100));
total_frame++;
}
pos_prev = pos;
memset(data, 0, sizeof(data));
} else {
data[3] = data[2];
data[2] = data[1];
data[1] = data[0];
}
}
pos = ftell(fp_src);
bytes = pos - pos_prev + 4;
if((bytes > 8) && (total_frame > 0)){
fputc(0x56, fp_dst);
fputc((((int)bytes >> 8) & 0x1F) | 0xE0, fp_dst);
fputc((int)bytes & 0xFF, fp_dst);
fputc(0x20, fp_dst);
fputc(0x00, fp_dst);
fputc(0x11, fp_dst);
fputc(0x90, fp_dst);
fseek(fp_src, pos_prev, SEEK_SET);
for(i = 0;i < (bytes - 4);i++){
fputc(fgetc(fp_src), fp_dst);
}
// printf("offset:%08ld bytes:%04lX\r\n", pos, bytes);
printf("\r%d%%", (int)((float)ftell(fp_src) / (float)input_file_size * 100));
total_frame++;
}
printf("\r\nTotal AAC Frames:%d\r\n", total_frame);
fclose(fp_src);
fclose(fp_dst);
return 0;
}