FFmpeg Library decoding HiSilicon G726 Library encoded audio data

Source: Internet
Author: User
Tags fread

Decoding process:

1, read HiSilicon g726 audio data, HiSilicon g726 audio will be more than 4 bytes of HiSilicon header information.

2. Select FFmpeg g726 encoder for decoding. FFmpeg g726 decoder includes: av_codec_id_adpcm_g726, Av_codec_id_adpcm_g726le. Select the av_codec_id_adpcm_g726 decoder type if the HiSilicon g726 stream type is ASF, and select the g726 decoder type if the HiSilicon RFC3551 stream type is Av_codec_id_adpcm_g726le standard.

Instance code:

#define HISI_AUDIO_HERDER_LEN 4//hisi audio data header extern "C" {#include "libavcodec/avcodec.h" #include "libavformat/avf

Ormat.h "#include" libavutil/frame.h "#include" libswscale/swscale.h "#include" libavutil/imgutils.h "}//Link ffmpeg Lib Library
	Avcodec *codec;
	Avcodeccontext *c= NULL;
	Avpacket avpkt;
	Avframe *decoded_frame = NULL;
	Avcodec_register_all ();
	Av_init_packet (&AVPKT); /* Find the MPEG Audio decoder */* ffmpeg g726 Encoder: av_codec_id_adpcm_g726 ffmpeg g726 decoder includes: av_codec_id_adpcm_g726, Av_c Odec_id_adpcm_g726le if the HiSilicon g726 stream type is ASF, select the av_codec_id_adpcm_g726 decoder type if the HiSilicon g726 stream type is RFC3551 standard, select Av_codec_id_
	Adpcm_g726le Decoder Type */codec = Avcodec_find_decoder (Av_codec_id_adpcm_g726le);
		if (!codec) {fprintf (stderr, "codec not found\n");
	Return
	} C = Avcodec_alloc_context3 (codec);
	Sample rate = 8000 Number of bits per sample = 16 Channels = 1/* Bits_per_coded_sample: Represents the ratio of the encoded bit value to the bit value of the sample rate. If the audio is g726, the ratio of g726 stream compression to the sample rate is indicated.
	For example, the Kbps code stream compression ratio is: k/8k = 5,kbps Stream Compression ratio is k/8k = 2. */c->bits_per_coded_sample = 5;
	C->channels = 1;
	C->SAMPLE_FMT = AV_SAMPLE_FMT_S16;
	C->sample_rate = 8000;
	C->codec_type = Avmedia_type_audio;
	C->bit_rate = 16000;
	int iRet = Avcodec_open2 (c, codec,null);
		if (IRet < 0) {fprintf (stderr, "could not open codec\n");
	Return
	} CString FilePath = "";
	CString Newlfilepath = "";
	Char szfilter[] = {"g726 Files (*.g726_hisi) |*.g726_hisi| |"}; CFileDialog Dlg (true,null,null,ofn_hidereadonly |
	Ofn_overwriteprompt,szfilter,null); if (dlg. DoModal () = = IDOK) {FilePath = dlg.
		GetPathName ();
		Newlfilepath = FilePath;
		Newlfilepath.replace (". G726_hisi", ". HISI2FF.PCM");
		BOOL bRet = 0;
		FILE * fpsrc = fopen (Filepath.getbuffer (Filepath.getlength ()), "RB");
		FILE * fpdst = fopen (Newlfilepath.getbuffer (Newlfilepath.getlength ()), "wb+");
		Char szdata[100] = {0};
		Char szoutdata[320] = {0};
		int ndatalen = 100;
		int noutdatalen = 320;
		int nreadedsize = 0;
		unsigned short ushisiheader[2] = {0};
if (fpsrc! = NULL) {while (TRUE)			{//Read header tag Ndatalen = Hisi_audio_herder_len;
				Nreadedsize = Fread (szdata,sizeof (char), NDATALEN,FPSRC);
				if (Nreadedsize < Ndatalen) {break;
				} memcpy (Ushisiheader,szdata,hisi_audio_herder_len);
				int naudioframedatalen = (ushisiheader[1] & 0x00ff) * sizeof (unsigned short);
				Ndatalen = Naudioframedatalen;
				Read Audio frame data nreadedsize = Fread (szdata,sizeof (char), NDATALEN,FPSRC);
				if (Nreadedsize < Ndatalen) {break;
				} Avpkt.data = (uint8_t *) szdata;
				Avpkt.size = nreadedsize;
				int got_frame = 0; if (!decoded_frame) {if (!) (
					Decoded_frame = Avcodec_alloc_frame ())) {return;
				}} else {avcodec_get_frame_defaults (decoded_frame);
				} int len = Avcodec_decode_audio4 (c, Decoded_frame, &got_frame, &AMP;AVPKT);
				if (Len < 0) {return; if (got_frame) {/* If a frame has been decoded, output it */int data_size = av_samples_get_Buffer_size (NULL, C->channels, Decoded_frame->nb_samples, C-&GT;SAMPLE_FMT, 1);
					
				Fwrite (Decoded_frame->data[0], 1, data_size, FPDST);
			}} fclose (FPSRC);
			Fclose (FPDST);
			Avcodec_close (c);
			Av_free (c);
		Av_free (Decoded_frame);
 }

	}

Code download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.