Last active
December 17, 2015 05:09
-
-
Save legnaleurc/5555755 to your computer and use it in GitHub Desktop.
h.264
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _BSD_SOURCE | |
#include <endian.h> | |
// #include "bs.h" | |
#include <stdio.h> | |
#include <stdint.h> | |
#include <stdlib.h> | |
#include <x264.h> | |
int main () { | |
FILE * fout = fopen("/tmp/t1.264", "wb"); | |
uint8_t sps[] = {0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x80, 0x29, 0x95, 0xA0, 0x1E, 0x00, 0x89, 0x79, 0x50}; | |
uint8_t pps[] = {0x00, 0x00, 0x00, 0x01, 0x68, 0xCE, 0x3C, 0x80}; | |
#if 0 | |
x264_param_t p; | |
x264_t * g; | |
x264_nal_t * nals; | |
int nHeader; | |
x264_param_default(&p); | |
// x264_param_default_preset(&p, "ultrafast", "zerolatency"); | |
p.i_width = 1920; | |
p.i_height = 1080; | |
p.i_fps_num = 30; | |
p.i_fps_den = 1; | |
// p.i_bitrate = 14000000; | |
g = x264_encoder_open(&p); | |
x264_encoder_headers(g, &nals, &nHeader); | |
for (int i = 0; i < nHeader; ++i) { | |
fwrite(nals[i].p_payload, 1, nals[i].i_payload, fout); | |
} | |
#endif | |
#if 0 | |
uint8_t sps[] = {0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x0a, 0xf8, 0x41, 0xa2}; | |
uint8_t pps[] = {0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x38, 0x80}; | |
fwrite(sps, 1, 11, fout); | |
fwrite(pps, 1, 8, fout); | |
#endif | |
#if 0 | |
uint8_t c[] = {'\x00', '\x00', '\x00', '\x01', '\x67', '\x42', '\xC0', '\x33'}; | |
fwrite(c, 1, 8, fout); | |
int a[] = {0, 1, 0, 2, 1}; | |
int b[] = {119, 67}; | |
uint8_t bitstream[1024] = {0}; | |
bs_t * bs = bs_new(bitstream, 1024); | |
for (int i = 0; i < 5; ++i) { | |
bs_write_ue(bs, a[i]); | |
} | |
bs_write_u1(bs, 0); | |
for (int i = 0; i < 2; ++i) { | |
bs_write_ue(bs, b[i]); | |
} | |
bs_write_u1(bs, 1); | |
bs_write_u1(bs, 1); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 1); | |
fwrite(bitstream, 1, bs_pos(bs), fout); | |
if (!bs_byte_aligned(bs)) { | |
fwrite(bitstream + bs_pos(bs), 1, 1, fout); | |
} | |
uint8_t d[] = {'\x00', '\x00', '\x00', '\x01', '\x68'}; | |
fwrite(d, 1, 5, fout); | |
memset(bitstream, 0, 1024); | |
bs_init(bs, bitstream, 1024); | |
int e[] = {0, 0}; | |
for (int i = 0; i < 2; ++i) { | |
bs_write_ue(bs, e[i]); | |
} | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_ue(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 0); | |
bs_write_u1(bs, 1); | |
bs_print_state(bs); | |
fwrite(bitstream, 1, bs_pos(bs), fout); | |
if (!bs_byte_aligned(bs)) { | |
fwrite(bitstream + bs_pos(bs), 1, 1, fout); | |
} | |
bs_free(bs); | |
#endif | |
FILE * fin = fopen("/tmp/xxx.264", "rb"); | |
uint8_t nal[] = {'\x00', '\x00', '\x00', '\x01'}; | |
uint32_t size_ = 0; | |
uint8_t * f = NULL; | |
for (int i = 0; i < 32; ++i) { | |
fread(&size_, 4, 1, fin); | |
size_ = be32toh(size_); | |
f = malloc(size_); | |
fread(f, 1, size_, fin); | |
fwrite(nal, 1, 4, fout); | |
fwrite(f, 1, size_, fout); | |
free(f); | |
} | |
fclose(fin); | |
fclose(fout); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment