The first three articles have explained in detail how to convert the WAV audio format to the FLV format. This article describes how to convert the last part, first, the AMR is converted into WAV format, and then the WAV format is encapsulated
FLV format. The key part of this article is the part in the while loop. The other part can be skipped. The function d_if_decode (St, serial, synth16, _ good_frame ); the function is to encode a frame of AMR data into a corresponding frame of WAV format data. To call this function, you need some relevant AMR audio codec libraries. You can search for them online.
Int amrtowav (const char * amrdata, int amrdatalen, char * wavdata ){
/*
* Convert the AMR audio data of amrdatalen bytes into WAV and write the data into wavdata.
* If the number of Wav data bytes is returned successfully,-1 is returned if the data fails to be returned.
*/
Uword8 serial [nb_serial_max];
Word8 synth8 [l_frame16k * 2];
Word16 synth16 [l_frame16k];
Word16 mode = 0;
Word16 readlen = 0;
Word16 writelen = l_frame16k * 2;
Word32 readbytesum = 0;
Word32 writebytesum = 0;
Word16 I, J;
Char * temppt = synth8;
//////////////////////
Unsigned char flvprevioustagsizetemp [10];
Unsigned char * flvprevioustagnum = flvprevioustagsizetemp;
Unsigned char flvtagdatafirstbytetemp [10];
Unsigned char * flvtagdatafirstbyte = flvtagdatafirstbytetemp;
Unsigned char flvheadertemp [50];
Unsigned char * flvheader = flvheadertemp;
Unsigned char flvtagheadertemp [50];
Unsigned char * flvtagheader = flvtagheadertemp;
Word32 flvheadersize = 0;
Word32 flvtagheadersize = 0;
Word32 flvtagdatafirstbytesize = 0;
Word32 flvprevioustagsize = 0;
//////////////////////
While (readbytesum <amrdatalen ){
Serial [0] = * (amrdata + readbytesum );
# Ifdef if2
Mode = (word16) (Serial [0]> 4 );
# Else
Mode = (word16) (Serial [0]> 3) & 0x0f );
# Endif
If (mode> = 0 & mode <= 8 ){
Readlen = block_size [mode]-1;
For (I = 1; I <= readlen; I ++ ){
Serial [I] = * (amrdata + I + readbytesum );
}
Frame ++;
Readbytesum + = (readlen + 1 );
// Fprintf (stderr, "Decoding Frame: % d, % d \ r", frame, writebytesum );
D_if_decode (St, serial, synth16, _ good_frame );
Temppt = reinterpret_cast <char *> (synth16 );
/**
* Todo
* Modify Timestamp
*/
If (isfirsttaghdr ){
Isfirsttaghdr = false;
} Else {
Calculatetimestamp (timestamp );
}
Flvtagheadersize = getflvtagheader (flvtagheader, 0); // get the tag Header
For (j = 0; j <flvtagheadersize; j ++ ){
* (Wavdata + J + writebytesum) = * (flvtagheader + J); // write the tag header to the FLV File
}
Writebytesum + = flvtagheadersize;
Flvtagdatafirstbytesize = getflvtagdatafirstbyte (flvtagdatafirstbyte, 0); // firstbyte of tagdata
For (j = 0; j <flvtagdatafirstbytesize; j ++ ){
* (Wavdata + J + writebytesum) = * (flvtagdatafirstbyte + J); // write the firstbyte of tagdata to FLV
}
Writebytesum + = flvtagdatafirstbytesize;
For (j = 0; j <writelen; j ++ ){
* (Wavdata + J + writebytesum) = * (temppt + J );
}
Writebytesum + = writelen;
Flvprevioustagsize = getprevioustagsize (flvprevioustagnum, 0); // previoustagsize
For (j = 0; j <flvprevioustagsize; j ++ ){
* (Wavdata + J + writebytesum) = * (flvprevioustagnum + J );
}
Writebytesum + = flvprevioustagsize;
Fprintf (stderr, "previous tag size: % d \ r", writebytesum );
}
}
Return writebytesum;
}
PS: I wrote an article at the beginning. Please forgive me. If you have any questions or communication, you can add your YY: 301558660
Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/7542679