#include <stdio.h> #include <stdlib.h> extern "C"{// #include "avcodec.h" #include "avformat.h" }
Int main (char Arg, char * argv []) { Char * filename = "02.swf ";
Av_register_all (); // register all decoded types Avformatcontext * pinfmtctx = NULL; // File Format Avcodeccontext * pincodecctx = NULL; // encoding format If (av_open_input_file (& pinfmtctx, filename, null, 0, null )! = 0) // obtain the file format Printf ("av_open_input_file error/N "); If (av_find_stream_info (pinfmtctx) <0) // obtain audio and video stream information in the file Printf ("av_find_stream_info error/N ");
Unsigned Int J; // Find the first audio stream
Int audiostream =-1; For (j = 0; j <pinfmtctx-> nb_streams; j ++) // find the stream corresponding to the audio If (pinfmtctx-> streams [J]-> codec-> codec_type = codec_type_audio) { Audiostream = J; Break; } If (audiostream =-1) { Printf ("input file has no audio stream/N "); Return 0; // didn't find a audio stream
} Printf ("audio stream num: % d/N", audiostream ); Pincodecctx = pinfmtctx-> streams [audiostream]-> codec; // Audio Encoding Context Avcodec * pincodec = NULL; /* File * file3 = fopen ("codec_private_data_size.txt", "W "); For (INT I = 0; I <200000; I ++) { Pincodec = avcodec_find_decoder (codecid) I ); If (pincodec! = NULL) { Fprintf (file3, "% S % d/N", pincodec-> name, pincodec-> priv_data_size ); } } Fclose (file3 ); System ("pause "); */ Pincodec = avcodec_find_decoder (pincodecctx-> codec_id); // locate the structure used for Decoding Based on the encoding ID If (pincodec = NULL) { Printf ("error no codec found/N "); Return-1; // codec not found }
// Decoding can also be completed by replacing pincodecctx with test. It can be seen that decoding and re-sampling can be realized as long as the following important information is obtained. Avcodeccontext * test = avcodec_alloc_context (); Test-> bit_rate = pincodecctx-> bit_rate; // used for re-sampling Test-> sample_rate = pincodecctx-> sample_rate; // used for re-sampling Test-> channels = pincodecctx-> channels; // used for re-sampling Test-> extradata = pincodecctx-> extradata; // If yes Test-> extradata_size = pincodecctx-> extradata_size; // if necessary Test-> codec_type = codec_type_audio; // unnecessary Test-> block_align = pincodecctx-> block_align; // required
If (avcodec_open (test, pincodec) <0) // combine the two to call the corresponding decoding function in pincodec in the following decoding function { Printf ("error avcodec_open failed./N "); Return-1; // cocould not open Codec
}
If (avcodec_open (pincodecctx, pincodec) <0) { Printf ("error avcodec_open failed./N "); Return-1; // cocould not open Codec
}
Static avpacket packet;
Printf ("bit_rate = % d/R/N", pincodecctx-> bit_rate ); Printf ("sample_rate = % d/R/N", pincodecctx-> sample_rate ); Printf ("channels = % d/R/N", pincodecctx-> channels ); Printf ("code_name = % S/R/N", pincodecctx-> codec-> name ); // Printf ("extra data size: % d: Data % x/N", pincodecctx-> extradata_size, pincodecctx-> extradata [0] //, Pincodecctx-> extradata [1], pincodecctx-> extradata [2], pincodecctx-> extradata [3]); Printf ("block_align = % d/N", pincodecctx-> block_align );
// System ("pause "); //////////////////////////////////////// //////////////////////////////////
Uint8_t * pktdata; Int pktsize; Int out_size = avcodec_max_audio_frame_size * 100; Uint8_t * inbuf = (uint8_t *) malloc (out_size ); File * PCM, * packetinfo; Packetinfo = fopen ("packetinfo.txt", "W "); PCM = fopen ("result. PCM", "WB "); Long start = clock (); While (av_read_frame (pinfmtctx, & Packet)> = 0) // call the corresponding packet format in pinfmtctx to obtain the Function { // Fprintf (packetinfo, "packet {PTS = % d; DTS = % d; % x, % x; size = % d; stream_index = % d; Pos = % d;}/N ",
// Packet. PTS, packet. DTS, packet. data [0], packet. data [1], packet. data [2], packet. data [3], packet. size, packet. stream_index, packet. pos );
If (packet. stream_index = audiostream) // If the audio { Pktdata = packet. Data; Pktsize = packet. size; While (pktsize> 0) { // Fprintf (packetinfo, "packet data: % x/N", pktdata [0], pktdata [1], pktdata [2], pktdata [3], pktdata [4]);
// Fprintf (packetinfo, "packet size: % d/n", pktsize );
Out_size = avcodec_max_audio_frame_size * 100; // Decoding Int Len = avcodec_decode_audio2 (pincodecctx, (short *) inbuf, & out_size, pktdata, pktsize ); If (LEN <0) { Printf ("error while decoding./N "); Break; } If (out_size> 0) { Fwrite (inbuf, 1, out_size, PCM); // PCM record Fflush (PCM ); } Pktsize-= Len; Pktdata + = Len; } } Av_free_packet (& Packet ); } Long end = clock (); Printf ("cost time: % F/N", double (end-Start)/(double) clocks_per_sec ); Free (inbuf ); Fclose (PCM ); Fclose (packetinfo ); If (pincodecctx! = NULL) { Avcodec_close (pincodecctx ); } If (test! = NULL) { Avcodec_close (test ); } Av_free (test ); Av_close_input_file (pinfmtctx ); Return 0; }
|