Skip to content

Commit

Permalink
modified: Makefile
Browse files Browse the repository at this point in the history
	modified:   README.md
	modified:   h264Splitter.c
  • Loading branch information
Christian Alvarado committed Dec 26, 2019
1 parent 8ae424e commit abc2cd3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
CFLAGS = -Wall -Werror
CFLAGS = -Wall
#-Werror

h264Splitter: h264Splitter.c
gcc $@.c $(CFLAGS) -o $@
h264Splitter4: h264Splitter.c
gcc $? $(CFLAGS) -o $@ -DSTARTER264NAL4

h264Splitter3: h264Splitter.c
gcc $? $(CFLAGS) -o $@

all: h264Splitter3 h264Splitter4
59 changes: 23 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,70 +1,57 @@
# h264-splitter
An h264 file is composed by several [NAL units](http://gentlelogic.blogspot.com/2011/11/exploring-h264-part-2-h264-bitstream.html), and with this utility you can split that file in independent file units.
An h264 file is composed by several [NAL units](http://gentlelogic.blogspot.com/2011/11/exploring-h264-part-2-h264-bitstream.html), and with this utility you can split that file into chunk files with one NAL each, with **001** and **0001** bytes header starter

## Getting Started
Once this repository is cloned, type the following on terminal to create the executable h264Splitter
Once this repository is cloned, type the following on terminal to create the executable h264Splitter3 and h264Splitter4

```
make
make all
```
### h264Splitter3
This is for **001** bytes header starter NAL unit
### h264Splitter4
This is for **0001** bytes header starter NAL unit

### Prerequisites

This has been developed to run under Linux.
This has been coded to run under Linux. (tested on pop-os!)

Also consider, this will split h264 files with a starter 0001.
#### For 001 header starters
comment line 6 and uncomment line 7. Currently it's like this:

```
int STARTER264NAL = 4;
// int STARTER264NAL = 3;
```
it should be like this
```
// int STARTER264NAL = 4;
int STARTER264NAL = 3;
```
Also modify the line 17, instead of this:

```
unsigned char starter[] = {0, 0, 0, 1};
```
it should be this:
```
unsigned char starter[] = {0, 0, 1};
```
##### Notes: I'm still working in automatize this section.
#### What Separator does my file have?
##### on Linux
For that,I recomend you to use a binary reader such as xxd. In terminal type the following
I recommend you to use a binary reader such as *xxd*. Open a terminal and type the following
```
xxd <you_h264_file> | head
xxd <you_h264_file> | head -n 3
```
this will give you the following result
this will give you a result like this:
```
00000000: 0000 0001 6742 c01f da03 204d f961 0000 ....gB.... M.a..
00000010: 0300 0100 0003 003c 8f18 32a0 0000 0001 .......<..2.....
00000020: 68ce 32c8 0000 0106 05ff ff62 dc45 e9bd h.2........b.E..
```
Observing the first line,
The important part is the first line
```
00000000: 0000 0001 6742 c01f da03 204d f961 0000 ....gB.... M.a..
```
we can conclude that that file is a **0001** header because it has *0000 0001*. On the other hand if your file has the following at the first line:
we can conclude that this file uses a **0001** header starter, because it has *0000 0001* at the beginning. On the other hand, if your file has the following at the first line:
```
00000000: 0000 0167 42c0 1fda 0320 4df9 61000003 ...gB.... M.a...
```
your file has a **001** header starters
your file has a **001** header starter.
##### on Mc or Windows
I recomend you to use a binary reader and check the first line

### What if my file is mp4?

Remember that mp4 is a container and not a codec, so you have to use ffmpeg. Try this:
```
ffmpeg -i <your_file_name>.mp4 -an -vcodec libx264 -crf 23 <your_output_file_name>.h264
```
## Running the tests
For running the splitter
```
./h264Splitter <your_h264_file>
./h264Splitter4 <your_h264_file>
```
this will generate one file per each NAL unit contained in your h264 file, from 0 until the last NAL unit, like this:
this will generate one file per each NAL unit contained in your h264 file, it will start at 0 until the last NAL unit, like this:

```
<your_h264_file>.0000
Expand All @@ -73,6 +60,6 @@ this will generate one file per each NAL unit contained in your h264 file, from
.
.
.
<your_h264_file>.<last_frame>
<your_h264_file>.<last_NAL_unit>
```
Remember that NAL units can be interpreted as "frames"
16 changes: 12 additions & 4 deletions h264Splitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
#include <stdio.h>
#include <string.h>

#ifdef STARTER264NAL4
int STARTER264NAL = 4;
unsigned char starter[] = {0, 0, 0, 1};
#else
int STARTER264NAL = 3;
unsigned char starter[] = {0, 0, 1};
#endif

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

int f = open(argv[1], O_RDONLY);
char fileName[50];
unsigned MAX_BUFFER_SIZE = 256;
unsigned char data[MAX_BUFFER_SIZE];
unsigned char tmp[MAX_BUFFER_SIZE];
unsigned char starter[] = {0, 0, 0, 1};
unsigned char *head;
unsigned char *tail;
unsigned char *DATA_TAIL;
Expand All @@ -32,11 +40,11 @@ int main(int argc, const char *argv[]) {
}

tail = data;
head -= 3;
head -= STARTER264NAL - 1;
sw = 0;

while(head < DATA_TAIL - 2) {
if(memcmp(head, starter, 4) == 0) {
while(head < DATA_TAIL - (STARTER264NAL - 2)) {
if(memcmp(head, starter, STARTER264NAL) == 0) {
sw = 1;
if (nalUnit > 0) {
write(nalUnit, tail, head - tail);
Expand Down

0 comments on commit abc2cd3

Please sign in to comment.