code

flv.cpp


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma pack(1)

#define TAG_TYPE_SCRIPT 18
#define TAG_TYPE_AUDIO  8
#define TAG_TYPE_VIDEO  9

typedef unsigned char byte;
typedef unsigned int uint;

typedef struct {
	byte Signature[3]; //F L V
	byte Version;
	byte Flags; //5 indicates audio and video
	uint DataOffset;// The header length is 4 bytes
} FLV_HEADER;

typedef struct {
	byte TagType;
	byte DataSize[3];
	byte Timestamp[3];
	uint Reserved;
} TAG_HEADER;


//reverse_bytes - turn a BigEndian byte array into a LittleEndian integer
uint reverse_bytes(byte *p, char c) {
	int r = 0;
	int i;
	for (i=0; i<c; i++) 
		r |= ( *(p+i) << (((c- 1) *8)- 8 -*i));
	return r;
}

/** * Analysis FLV file * @param url Location of input FLV file. */

int simplest_flv_parser(char *url){

	//whether output audio/video stream
	int output_a=1;
	int output_v=1;
	//-------------
	FILE *ifh=NULL,*vfh=NULL, *afh = NULL;

	//FILE *myout=fopen("output_log.txt","wb+");
	FILE *myout=stdout;

	FLV_HEADER flv;
	TAG_HEADER tagheader;
	uint previoustagsize, previoustagsize_z=0;
	uint ts=0, ts_new=0;

	ifh = fopen(url, "rb+");
	if ( ifh== NULL) {
		printf("Failed to open files!");
		return - 1;
	}

	//FLV file header
	fread((char *)&flv,1.sizeof(FLV_HEADER),ifh); // (memory pointer to hold data, number of bytes read, number of times read, file stream), sizeof in bytes

	fprintf(myout,"============== FLV Header ==============\n");
	fprintf(myout,"Signature: 0x %c %c %c\n",flv.Signature[0],flv.Signature[1],flv.Signature[2]);
	fprintf(myout,"Version: 0x %X\n",flv.Version);
	fprintf(myout,"Flags : 0x %X\n",flv.Flags); 
	fprintf(myout,"HeaderSize: 0x %X\n".reverse_bytes((byte *)&flv.DataOffset, sizeof(flv.DataOffset)));
	fprintf(myout,"========================================\n");

	//move the file pointer to the end of the header
	fseek(ifh, reverse_bytes((byte *)&flv.DataOffset, sizeof(flv.DataOffset)), SEEK_SET); // (file, offset byte, start point) SEEK_SET is the start of the file

	//process each tag
	do {
		// Read the first integer encountered (hexadecimal number greater than 0 and must not be a "letter"), each loop reads four bytes of the prev tag size in the FLV file format
		previoustagsize = getw(ifh); 

		/ / read the tag header
		fread((void *)&tagheader,sizeof(TAG_HEADER),1,ifh);

		//int temp_datasize1=reverse_bytes((byte *)&tagheader.DataSize, sizeof(tagheader.DataSize));
		int tagheader_datasize=tagheader.DataSize[0] *65536+tagheader.DataSize[1] *256+tagheader.DataSize[2]; / /?
		int tagheader_timestamp=tagheader.Timestamp[0] *65536+tagheader.Timestamp[1] *256+tagheader.Timestamp[2];

		char tagtype_str[10];
		switch(tagheader.TagType){
		case TAG_TYPE_AUDIO:sprintf(tagtype_str,"AUDIO");break;
		case TAG_TYPE_VIDEO:sprintf(tagtype_str,"VIDEO");break;
		case TAG_TYPE_SCRIPT:sprintf(tagtype_str,"SCRIPT");break;
		default:sprintf(tagtype_str,"UNKNOWN");break;
		}
		fprintf(myout,"[%6s] %6d %6d |",tagtype_str,tagheader_datasize,tagheader_timestamp);

		//if we are not past the end of file, process the tag
		if (feof(ifh)) {
			break;
		}

		//process tag by type
		switch (tagheader.TagType) {

		case TAG_TYPE_AUDIO:{ 
			char audiotag_str[100] = {0};
			strcat(audiotag_str,"|");
			// The first byte of the tag data section does not represent data
			char tagdata_first_byte;
			tagdata_first_byte=fgetc(ifh);// After reading a byte, the cursor position moves back one byte
			int x=tagdata_first_byte&0xF0; / / 0 xf0:1111 0000
			x=x>>4;// The first byte of the tag Data section, the first four bits, represents the soundformat
			switch (x)
			{
			case 0:strcat(audiotag_str,"Linear PCM, platform endian");break;
			case 1:strcat(audiotag_str,"ADPCM");break;
			case 2:strcat(audiotag_str,"MP3");break;
			case 3:strcat(audiotag_str,"Linear PCM, little endian");break;
			case 4:strcat(audiotag_str,"Nellymoser 16-kHz mono");break;
			case 5:strcat(audiotag_str,"Nellymoser 8-kHz mono");break;
			case 6:strcat(audiotag_str,"Nellymoser");break;
			case 7:strcat(audiotag_str,"G.711 A-law logarithmic PCM");break;
			case 8:strcat(audiotag_str,"G.711 mu-law logarithmic PCM");break;
			case 9:strcat(audiotag_str,"reserved");break;
			case 10:strcat(audiotag_str,"AAC");break;
			case 11:strcat(audiotag_str,"Speex");break;
			case 14:strcat(audiotag_str,"MP3 8-Khz");break;
			case 15:strcat(audiotag_str,"Device-specific sound");break;
			default:strcat(audiotag_str,"UNKNOWN");break;
			}
			strcat(audiotag_str,"|");
			x=tagdata_first_byte&0x0C; // 0000 1100 take the middle two digits
			x=x>>2;
			switch (x)
			{
			case 0:strcat(audiotag_str,"5.5-kHz");break;
			case 1:strcat(audiotag_str,"1-kHz");break;
			case 2:strcat(audiotag_str,"22-kHz");break;
			case 3:strcat(audiotag_str,"44-kHz");break;
			default:strcat(audiotag_str,"UNKNOWN");break;
			}
			strcat(audiotag_str,"|");
			x=tagdata_first_byte&0x02; // 0000 0010 is the second to last
			x=x>>1;
			switch (x)
			{
			case 0:strcat(audiotag_str,"8Bit");break;
			case 1:strcat(audiotag_str,"16Bit");break;
			default:strcat(audiotag_str,"UNKNOWN");break;
			}
			strcat(audiotag_str,"|");
			x=tagdata_first_byte&0x01; // 0000 0001 takes the last digit
			switch (x)
			{
			case 0:strcat(audiotag_str,"Mono");break;
			case 1:strcat(audiotag_str,"Stereo");break;
			default:strcat(audiotag_str,"UNKNOWN");break;
			}
			fprintf(myout,"%s",audiotag_str);

			//if the output file hasn't been opened, open it.
			if(output_a! =0&&afh == NULL){ 
				afh = fopen("output.mp3"."wb");
			}

			//TagData - First Byte Data
			int data_size=reverse_bytes((byte *)&tagheader.DataSize, sizeof(tagheader.DataSize))- 1;//-1 is because the first byte of tag data is not data
			if(output_a! =0) {//TagData+1
				for (int i=0; i<data_size; i++)
					fputc(fgetc(ifh),afh);// Write to output, ifH and AFH move backwards

			}else{
				for (int i=0; i<data_size; i++)
					fgetc(ifh);// No output is required
			}
			break;
		}
		case TAG_TYPE_VIDEO:{
			char videotag_str[100] = {0};
			strcat(videotag_str,"|");
			char tagdata_first_byte;
			tagdata_first_byte=fgetc(ifh);
			int x=tagdata_first_byte&0xF0;// Take the first four bits, frame type
			x=x>>4;
			switch (x)
			{
			case 1:strcat(videotag_str,"key frame ");break;
			case 2:strcat(videotag_str,"inter frame");break;
			case 3:strcat(videotag_str,"disposable inter frame");break;
			case 4:strcat(videotag_str,"generated keyframe");break;
			case 5:strcat(videotag_str,"video info/command frame");break;
			default:strcat(videotag_str,"UNKNOWN");break;
			}
			strcat(videotag_str,"|");
			x=tagdata_first_byte&0x0F;// Take the last four digits, the encoding type
			switch (x)
			{
			case 1:strcat(videotag_str,"JPEG (currently unused)");break;
			case 2:strcat(videotag_str,"Sorenson H.263");break;
			case 3:strcat(videotag_str,"Screen video");break;
			case 4:strcat(videotag_str,"On2 VP6");break;
			case 5:strcat(videotag_str,"On2 VP6 with alpha channel");break;
			case 6:strcat(videotag_str,"Screen video version 2");break;
			case 7:strcat(videotag_str,"AVC");break;
			default:strcat(videotag_str,"UNKNOWN");break;
			}
			fprintf(myout,"%s",videotag_str);

			fseek(ifh, - 1, SEEK_CUR); // Go one step ahead
			//if the output file hasn't been opened, open it.
			if (vfh == NULL&&output_v! =0) {
				//write the flv header (reuse the original file's hdr) and first previoustagsize
				// Separate video FLV must have FLV header, prevTagSize, tagHeader, and tagData. Isolated audio is not required
					vfh = fopen("output.flv"."wb");
					fwrite((char *)&flv,1.sizeof(flv),vfh);
					fwrite((char *)&previoustagsize_z,1.sizeof(previoustagsize_z),vfh);
			}
#if 0
			//Change Timestamp
			//Get Timestamp
			ts = reverse_bytes((byte *)&tagheader.Timestamp, sizeof(tagheader.Timestamp));
			ts=ts*2;
			//Writeback Timestamp
			ts_new = reverse_bytes((byte *)&ts, sizeof(ts));
			memcpy(&tagheader.Timestamp, ((char *)&ts_new) + 1.sizeof(tagheader.Timestamp));
#endif


			//TagData + Previous Tag Size
			// Write a tag containing prev tag size(4 bytes), tagheader, and tagData
			int data_size=reverse_bytes((byte *)&tagheader.DataSize, sizeof(tagheader.DataSize))+4;// tagData + prevTagSize Length
			if(output_v! =0) {//TagHeader
				fwrite((char *)&tagheader,1.sizeof(tagheader),vfh);
				//TagData
				for (int i=0; i<data_size; i++)
					fputc(fgetc(ifh),vfh);
			}else{
				for (int i=0; i<data_size; i++)
					fgetc(ifh);
			}
			//rewind 4 bytes, because we need to read the previoustagsize again for the loop's sake
			// The beginning of each loop reads off 4 bytes of pretagsize
			fseek(ifh, 4 -, SEEK_CUR);

			break;
		}
		default:
			//skip the data of this tag
			fseek(ifh, reverse_bytes((byte *)&tagheader.DataSize, sizeof(tagheader.DataSize)), SEEK_CUR);
		}

		fprintf(myout,"\n");

	} while (!feof(ifh));

	fcloseall(a);return 0;
}

Copy the code

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/** * Analysis H.264 Bitstream * @param url Location of input H.264 bitstream file. */
int simplest_h264_parser(char *url);

/** * Analysis FLV file * @param url Location of input FLV file. */
int simplest_flv_parser(char *url);

/** * Analysis AAC file * @param url Location of input AAC file. */
int simplest_aac_parser(char *url);

/** * Analysis RTP stream * @param url RTP URL */
int simplest_udp_parser(int port);

/** * Generate RGB24 colorbar. * @param width Width of Output RGB file. * @param height Height of Output RGB file. * @param url_out Location of Output RGB file. */
int simplest_rgb24_colorbar(int width, int height,char *url_out);
/** * Convert RGB24 file to BMP file * @param rgb24path Location of input RGB file. * @param width Width of input RGB file. * @param height Height of input RGB file. * @param url_out Location of Output BMP file. */
int simplest_rgb24_to_bmp(const char *rgb24path,int width,int height,const char *bmppath);

/** * Split R, G, B planes in RGB24 file. * @param url Location of Input RGB file. * @param w Width of Input RGB file. * @param h Height of Input RGB file. * @param num Number of frames to process. * */
int simplest_rgb24_split(char *url, int w, int h,int num);

/** * Convert RGB24 file to YUV420P file * @param url_in Location of Input RGB file. * @param w Width of Input RGB file.  * @param h Height of Input RGB file. * @param num Number of frames to process. * @param url_out Location of Output YUV file. */
int simplest_rgb24_to_yuv420(char *url_in, int w, int h,int num,char *url_out);

/** * Generate YUV420P gray scale bar. * @param width Width of Output YUV file. * @param height Height of Output YUV file. * @param ymin Max value of Y * @param ymax Min value of Y * @param barnum Number of bars * @param url_out Location  of Output YUV file. */
int simplest_yuv420_graybar(int width, int height,int ymin,int ymax,int barnum,char *url_out);

/** * Convert YUV420P file to gray picture * @param url Location of Input YUV file. * @param w Width of Input YUV file. * @param h Height of Input YUV file. * @param num Number of frames to process. */
int simplest_yuv420_gray(char *url, int w, int h,int num);

/** * Split Y, U, V planes in YUV420P file. * @param url Location of Input YUV file. * @param w Width of Input YUV file. * @param h Height  of Input YUV file. * @param num Number of frames to process. * */
int simplest_yuv420_split(char *url, int w, int h,int num);

/** * Halve Y value of YUV420P file * @param url Location of Input YUV file. * @param w Width of Input YUV file. * @param h Height of Input YUV file. * @param num Number of frames to process. */
int simplest_yuv420_halfy(char *url, int w, int h,int num);

/** * Add border for YUV420P file * @param url Location of Input YUV file. * @param w Width of Input YUV file. * @param h Height of Input YUV file. * @param border Width of Border. * @param num Number of frames to process. */
int simplest_yuv420_border(char *url, int w, int h,int border,int num);


/** * Calculate PSNR between 2 YUV420P file * @param url1 Location of first Input YUV file. * @param url2 Location of another Input YUV file. * @param w Width of Input YUV file. * @param h Height of Input YUV file. * @param num Number of frames to process. */
int simplest_yuv420_psnr(char *url1,char *url2,int w,int h,int num);

/** * Split Y, U, V planes in YUV444P file. * @param url Location of YUV file. * @param w Width of Input YUV file. * @param h Height of Input YUV file. * @param num Number of frames to process. * */
int simplest_yuv444_split(char *url, int w, int h,int num);

/** * Cut a 16LE PCM single channel file. * @param url Location of PCM file. * @param start_num start point * @param dur_num how much point to cut */
int simplest_pcm16le_cut_singlechannel(char *url,int start_num,int dur_num);

/** * Split Left and Right channel of 16LE PCM file. * @param url Location of PCM file. * */
int simplest_pcm16le_split(char *url);

/** * Halve volume of Left channel of 16LE PCM file * @param url Location of PCM file. */
int simplest_pcm16le_halfvolumeleft(char *url);

/** * Re-sample to double the speed of 16LE PCM file * @param url Location of PCM file. */
int simplest_pcm16le_doublespeed(char *url);

/** * Convert PCM-16 data to PCM-8 data. * @param url Location of PCM file. */
int simplest_pcm16le_to_pcm8(char *url);

/** * Convert PCM16LE raw data to WAVE format * @param pcmpath Input PCM file. * @param channels Channel number of PCM file. * @param sample_rate Sample rate of PCM file. * @param wavepath Output WAVE file. */
int simplest_pcm16le_to_wave(const char *pcmpath,int channels,int sample_rate,const char *wavepath);



int main(int argc, char* argv[]){

	//Test
	/ / simplest_yuv420_split (" lena_256x256_yuv420p. Yuv ", 256256, 1);

	/ / simplest_yuv444_split (" lena_256x256_yuv444p. Yuv ", 256256, 1);
	
	/ / simplest_yuv420_halfy (" lena_256x256_yuv420p. Yuv ", 256256, 1);

	/ / simplest_yuv420_gray (" lena_256x256_yuv420p. Yuv ", 256256, 1);

	/ / simplest_yuv420_border (256256,20,1 "lena_256x256_yuv420p. Yuv,");

	/ / simplest_yuv420_graybar (640, 360,0,255,10, "graybar_640x360. Yuv");

	/ / simplest_yuv420_psnr (" lena_256x256_yuv420p. Yuv ", "lena_distort_256x256_yuv420p. Yuv", 256256, 1);

	/ / simplest_rgb24_split (" cie1931_500x500. RGB ", 500, 500, 1);

	/ / simplest_rgb24_to_bmp (" lena_256x256_rgb24. RGB ", 256256, "output_lena. BMP");

	/ / simplest_rgb24_to_yuv420 (" lena_256x256_rgb24. RGB ", 256256, 1, "output_lena. Yuv");

	// simplest_rgb24_colorbar(640, 360,"colorbar_640x360.rgb");

	/ / simplest_pcm16le_split (" NocturneNo2inEflat_44. 1 k_s16le. PCM ");

	/ / simplest_pcm16le_halfvolumeleft (" NocturneNo2inEflat_44. 1 k_s16le. PCM ");

	/ / simplest_pcm16le_doublespeed (" NocturneNo2inEflat_44. 1 k_s16le. PCM ");

	/ / simplest_pcm16le_to_pcm8 (" NocturneNo2inEflat_44. 1 k_s16le. PCM ");

	/ / simplest_pcm16le_cut_singlechannel (" drum. PCM ", 2360120);

	/ / simplest_pcm16le_to_wave (" NocturneNo2inEflat_44. 1 k_s16le. PCM ", 2441, 00 "output_nocturne. Wav");

	// simplest_h264_parser("sintel.h264");
	
	simplest_flv_parser("cuc_ieschool.flv");

	// simplest_aac_parser("nocturne.aac");

	// simplest_udp_parser(8880);

	return 0;
}
Copy the code

Compile operation

g++ -c flv.cpp
g++ -c main.cpp
g++ flv.o main.o -o test
./test
Copy the code

The results of

Blog.csdn.net/leixiaohua1…