// File: /workspace/github_c_makefile_star2_O3/Joy-Division[P]tools-zoe/pss-demux.c
// Function: mpegDetectAndDumpPrivateStream
void mpegDetectAndDumpPrivateStream( FILE *f, unsigned int offset, FILE *aout, FILE *sout, FILE *pout, char *name ) {
	fseek( f, offset, SEEK_SET );
	uint32_t streamSize, headSize;
	char *outfilename = NULL;
	
	mpegPesHeader pesHead;
	mpegPrivate1ExtensionHead extHead;
	
	
	mpegPrivate1ExtensionKonami sonyExtHead;
	konamiPrivate1Header sonyPrivateHead;
	
	
	fread( &pesHead, sizeof(pesHead), 1, f );
	headSize = pesHead.size[0] << 8 | pesHead.size[1];
	fread( &extHead, sizeof(extHead), 1, f );
	fseek( f, extHead.size, SEEK_CUR );
	
	fread( &sonyExtHead, sizeof(sonyExtHead), 1, f );
	fread( &sonyPrivateHead, sizeof(sonyPrivateHead), 1, f );
	
	switch(sonyExtHead.streamId) {
		case ZOEPT_VAG: { // stream 0x01
			if( ftell(aout) == 0 ) {
				streamSize = headSize - 3 - extHead.size - 4 - 16 - (0x800-0x10); // subtract 3 for extHead size that is included in pesHead but not extHead, subtract 4 for sony extension, subtract 16 for sony header subtract 0x800-0x10 for alignment
				fseek( f, 0x800-0x10, SEEK_CUR ); // alignment stuffs
				audioSamplerate[2] = sonyPrivateHead.samplerate[0]; audioSamplerate[3] = sonyPrivateHead.samplerate[1];
				audioNumChannels = sonyPrivateHead.channels;
				unsigned char null[0x40] = {0};
				if( audioNumChannels == 1 ) fwrite( null, 1, 0x30, aout );
				else if( audioNumChannels == 2 ) fwrite( null, 1, 0x40, aout );
			}
			else {
				streamSize = headSize - 3 - extHead.size - 4;
				fseek( f, -0x10, SEEK_CUR ); // seek back size of sonyExtHead because only the first audio block contains it
			}
			writeFile( f, streamSize, aout );
			break;
		}
		case ZOEPT_DMX: { // stream 0x05
			fseek( f, -0x10, SEEK_CUR );
			writeFile( f, headSize, pout );
			break;
		}
		default: { // subtitle streams, always seem to fit into one PES
			streamSize = sonyPrivateHead.size; // for subs this is the actual size, so we can use it here
			outfilename = malloc( strlen( name ) + 7 + 2 + 4 + 1 );
			sprintf( outfilename, "%s-stream%02x.bin", name, sonyExtHead.streamId );
			if( !(sout = fopen( outfilename, "wb" ))) {
				printf("Couldnt open file %s\n", outfilename);
				return;
			}
			writeFile( f, streamSize, sout );
			fclose(sout);
			free(outfilename);
			outfilename = NULL;
			break;
		}
	}
	fseek( f, offset+headSize+6, SEEK_SET );
	return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, %rbp
movq %r8, %r14
movq %rdx, %r12
movq %rdi, %rbx
movq %rsi, 0x18(%rsp)
movl %esi, %esi
xorl %edx, %edx
callq 0x10d0
leaq 0x3a(%rsp), %r15
movl $0x6, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x1060
movzwl 0x4(%r15), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movq %rax, 0x8(%rsp)
leaq 0x15(%rsp), %r15
movl $0x3, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x1060
movzbl 0x2(%r15), %r13d
movq %rbx, %rdi
movq %r13, %rsi
movl $0x1, %edx
callq 0x10d0
leaq 0x36(%rsp), %r15
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rdi
movq %rbx, %rcx
callq 0x1060
leaq 0x26(%rsp), %rdi
movl $0x10, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x1060
movzbl 0x3(%r15), %r15d
cmpl $0x5, %r15d
je 0x147c
cmpl $0x1, %r15d
jne 0x14a3
movq %r12, %rdi
callq 0x10a0
testq %rax, %rax
je 0x1517
movq 0x8(%rsp), %r15
leal -0x7(%r15), %ebp
movq %rbx, %rdi
movq $-0x10, %rsi
movl $0x1, %edx
callq 0x10d0
jmp 0x15ad
movq %rbx, %rdi
movq $-0x10, %rsi
movl $0x1, %edx
callq 0x10d0
movq %rbx, %rdi
movq 0x8(%rsp), %r15
movl %r15d, %esi
movq %r14, %rdx
jmp 0x15b8
movzwl 0x2a(%rsp), %r13d
movq %rbp, %rdi
callq 0x1080
leaq 0xe(%rax), %rdi
callq 0x10b0
movq %rax, %r12
leaq 0xba6(%rip), %rsi       # 0x206a
movq %rax, %rdi
movq %rbp, %rdx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x1100
leaq 0xba1(%rip), %rsi       # 0x207c
movq %r12, %rdi
callq 0x10e0
testq %rax, %rax
je 0x156d
movq %rax, %r15
movq %rbx, %rdi
movl %r13d, %esi
movq %rax, %rdx
callq 0x121c
movq %r15, %rdi
callq 0x1070
movq %r12, %rdi
callq 0x1030
movq 0x8(%rsp), %r15
jmp 0x15bd
movl $0x7f0, %esi            # imm = 0x7F0
movq %rbx, %rdi
movl $0x1, %edx
callq 0x10d0
movzwl 0x2c(%rsp), %eax
movw %ax, 0x2b7e(%rip)       # 0x40b3
movzbl 0x2e(%rsp), %eax
movb %al, 0x2b75(%rip)       # 0x40b5
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x40(%rsp)
cmpl $0x1, %eax
movq 0x8(%rsp), %r15
je 0x158f
cmpl $0x2, %eax
jne 0x15a6
movl $0x40, %edx
jmp 0x1594
leaq 0xb0b(%rip), %rdi       # 0x207f
movq %r12, %rsi
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1090
movl $0x30, %edx
leaq 0x40(%rsp), %rdi
movl $0x1, %esi
movq %r12, %rcx
callq 0x1110
leal -0x807(%r15), %ebp
subl %r13d, %ebp
movq %rbx, %rdi
movl %ebp, %esi
movq %r12, %rdx
callq 0x121c
movq 0x18(%rsp), %rax
leal (%rax,%r15), %esi
addl $0x6, %esi
movq %rbx, %rdi
xorl %edx, %edx
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10d0