Hi,

I want to write a program to calculate the no. of bytes between the first binary pattern (110) to the next 110 binary pattern. i know i probably have to make use of:

cout<<sizeof(char);

Do i need to use a pointer or something to tell the program where to start counting? Or is there a better way?

Thanks.

Dani AI

Generated

already has a working detector for the 00 00 01 pattern and pointed out the simple “start counting after first match, stop at second” idea. The robust, maintainable approach is to record the file offsets of every start-code match and then compute differences — that avoids off-by-one ambiguity and deals correctly with 3- vs 4-byte start codes.

Key points to follow:

  • Open the file in binary mode and read bytes as uint8_t/unsigned char so comparisons are unambiguous.
  • Scan once and push each match offset into a vector. Prefer the 4-byte 00 00 00 01 match when present (treat it as one start code) to avoid double-counting overlapping sequences.
  • Compute the gap as bytes_between = next_offset - (current_offset + matched_length). That gives the number of payload bytes strictly between the two start codes; adjust if you want inclusive counts.
  • For very large files stream the data and keep a running byte counter rather than loading the entire file into memory.

Example (conceptual) scanning loop:

read file into a byte buffer or stream it
for i from 0 to size-1:
    if buffer[i..i+3] == 00 00 00 01:
        record i and advance i += 3
    else if buffer[i..i+2] == 00 00 01:
        record i and advance i += 2
compute gaps with next - (current + pattern_len)

Troubleshooting/caveats:

  • Many MP4 files do NOT contain Annex‑B start codes: inside MP4 NAL units are often length‑prefixed (avcC) rather than 00 00 01. If your code never finds start codes in MOV_0003.mp4, inspect the container format or parse the avcC box to read length-prefixed NALs instead. For large, performance‑sensitive tasks consider chunked scanning or mmap.

Recommended Answers

All 3 Replies

Unless you need something special, it would be easier to convert the bits to a string and do string searches. Then the problem is trivial.

Hi Narue,
I have already got the code of searching 001 byte pattern ready. So i was thinking how do i use this info to calculate the no. of bytes between two 001 byte pattern.

#include<iostream>
#include<fstream>

using namespace std;

bool isH264(ifstream& stream)
{
	int numZeros = 0;
	while(stream.good())
	{
		char c = stream.get();
		if(c == 0)
			numZeros++;
		else if(c == 1 && numZeros == 2)
		{
			return true;
		}
		else
			numZeros = 0;
	}
	return false;
}

int main(int argc, char *argv[])
{
	ifstream file("MOV_0003.mp4", ios::binary);
	if(isH264(file))
		cout << "This may be a H.264 file" << endl;
	else
		cout << "This is not a H.264 file" << endl;
	file.close();
	cin.ignore();
cin.get();
	return 0;
}

Nevermind, I was thinking bit patterns instead of bytes, but you asked about bytes. This is much easier, just begin incrementing a counter after you find the first matching byte and stop when you find the second. For example:

#include <iostream>
#include <cctype>

int main()
{
    bool found_start = false;
    int range = 0;
    char c;

    while (std::cin.get(c)) {
        if (std::isspace(c)) {
            if (!found_start)
                found_start = true;
            else {
                std::cout<< range <<'\n';
                found_start = false;
                range = 0;
            }
        }
        else if (found_start) {
            ++range;
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.