Refactor: nal.c/.h full cleanup.

- Move constant from the signature of calc_checksum into a macro.
This commit is contained in:
Ari Koivula 2013-09-19 16:03:02 +03:00
parent 4416d6ec36
commit f750c24948
3 changed files with 55 additions and 49 deletions

View file

@ -448,7 +448,7 @@ void read_one_frame(FILE* file, encoder_control* encoder)
*/ */
static void add_checksum(encoder_control* encoder) static void add_checksum(encoder_control* encoder)
{ {
unsigned char checksum[3][16]; unsigned char checksum[3][SEI_HASH_MAX_LENGTH];
uint32_t checksum_val; uint32_t checksum_val;
unsigned int i; unsigned int i;

View file

@ -13,89 +13,84 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include "config.h" #include "config.h"
#include "bitstream.h" #include "bitstream.h"
#include "cabac.h" #include "cabac.h"
#include "encoder.h" #include "encoder.h"
/**
* \brief Write a Network Abstraction Layer (NAL) packet to the output.
*/
void nal_write(FILE *output, uint8_t *buffer, uint32_t buffer_len, uint8_t nal_ref, uint8_t nal_type, uint8_t temporal_id) void nal_write(FILE *output, uint8_t *buffer, uint32_t buffer_len, uint8_t nal_ref, uint8_t nal_type, uint8_t temporal_id)
{ {
uint8_t byte; uint8_t byte;
uint32_t i; uint32_t i;
uint8_t zerocount = 0; uint8_t zerocount = 0;
/* Some useful constants */ // Some useful constants
const uint8_t emulation_prevention_three_byte = 0x03; const uint8_t emulation_prevention_three_byte = 0x03;
const uint8_t start_code_prefix_one_3bytes = 0x01; const uint8_t start_code_prefix_one_3bytes = 0x01;
const uint8_t zero = 0x00; const uint8_t zero = 0x00;
/*start_code_prefix_one_3bytes */ // start_code_prefix_one_3bytes
/*
if(temporal_id == 0)
{
fwrite(&zero, 1, 1, output);
}
*/
fwrite(&zero, 1, 1, output); fwrite(&zero, 1, 1, output);
fwrite(&zero, 1, 1, output); fwrite(&zero, 1, 1, output);
fwrite(&start_code_prefix_one_3bytes, 1, 1, output); fwrite(&start_code_prefix_one_3bytes, 1, 1, output);
/* Handle header bits with full bytes instead of using bitstream */ // Handle header bits with full bytes instead of using bitstream
/* forbidden_zero_flag(1) + nal_unit_type(6) + 1bit of nuh_layer_id*/ // forbidden_zero_flag(1) + nal_unit_type(6) + 1bit of nuh_layer_id
byte = nal_type << 1; byte = nal_type << 1;
fwrite(&byte, 1, 1, output); fwrite(&byte, 1, 1, output);
/* 5bits of nuh_layer_id + nuh_temporal_id_plus1(3) */ // 5bits of nuh_layer_id + nuh_temporal_id_plus1(3)
byte = (temporal_id + 1) & 7; byte = (temporal_id + 1) & 7;
fwrite(&byte, 1, 1, output); fwrite(&byte, 1, 1, output);
/* Write out bytes and add emulation_prevention_three_byte when needed */ // Write out bytes and add emulation_prevention_three_byte when needed
for(i = 0; i < buffer_len; i++) for (i = 0; i < buffer_len; ++i) {
{ // Prevent 0x0000 + 00/01/02 byte sequences from occurring by prefixing
if(zerocount == 2 && buffer[i] < 4) /* Prevent 0x0000 + 00/01/02/03 */ // the last byte with 0x03. Do the same for 0x03.
{ if (zerocount == 2 && buffer[i] < 4) {
/* Inserting 0x03 */
fwrite(&emulation_prevention_three_byte, 1, 1, output); fwrite(&emulation_prevention_three_byte, 1, 1, output);
zerocount = 0; zerocount = 0;
} }
if(buffer[i] == 0) if(buffer[i] == 0) {
{
zerocount++; zerocount++;
} } else {
else
{
zerocount = 0; zerocount = 0;
} }
/* Write the actual data */ // Write the actual data
fwrite(&buffer[i], 1, 1, output); fwrite(&buffer[i], 1, 1, output);
} }
/* If last byte was 0, add emulation_prevention_three_byte */ // If last byte was 0, add emulation_prevention_three_byte
if(buffer[buffer_len-1] == 0) if (buffer[buffer_len - 1] == 0) {
{
fwrite(&emulation_prevention_three_byte, 1, 1, output); fwrite(&emulation_prevention_three_byte, 1, 1, output);
} }
} }
/*! /**
\brief Calculate checksum for one color of the picture. * \brief Calculate checksum for one color of the picture.
\param data Beginning of the pixel data for the picture. * \param data Beginning of the pixel data for the picture.
\param height Height of the picture. * \param height Height of the picture.
\param width Width of the picture. * \param width Width of the picture.
\param stride Width of one row in the pixel array. * \param stride Width of one row in the pixel array.
\returns Void
*/ */
static void array_checksum(const uint8_t* data, const int height, const int width, const int stride, unsigned char checksum_out[]) static void array_checksum(const uint8_t* data,
const int height, const int width,
const int stride,
unsigned char checksum_out[SEI_HASH_MAX_LENGTH])
{ {
unsigned char mask; unsigned char mask;
unsigned int checksum = 0; unsigned int checksum = 0;
int y, x; int y, x;
assert(SEI_HASH_MAX_LENGTH >= 4);
for (y = 0; y < height; ++y) { for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) { for (x = 0; x < width; ++x) {
mask = (x & 0xff) ^ (y & 0xff) ^ (x >> 8) ^ (y >> 8); mask = (x & 0xff) ^ (y & 0xff) ^ (x >> 8) ^ (y >> 8);
@ -104,7 +99,7 @@ static void array_checksum(const uint8_t* data, const int height, const int widt
} }
} }
/* Unpack uint into byte-array.*/ // Unpack uint into byte-array.
checksum_out[0] = (checksum >> 24) & 0xff; checksum_out[0] = (checksum >> 24) & 0xff;
checksum_out[1] = (checksum >> 16) & 0xff; checksum_out[1] = (checksum >> 16) & 0xff;
checksum_out[2] = (checksum >> 8) & 0xff; checksum_out[2] = (checksum >> 8) & 0xff;
@ -118,7 +113,7 @@ static void array_checksum(const uint8_t* data, const int height, const int widt
\param checksum_out Result of the calculation. \param checksum_out Result of the calculation.
\returns Void \returns Void
*/ */
void picture_checksum(const picture* pic, unsigned char checksum_out[][16]) void picture_checksum(const picture* pic, unsigned char checksum_out[][SEI_HASH_MAX_LENGTH])
{ {
int stride = pic->width; /* TODO: != width, if there is a luma margin. */ int stride = pic->width; /* TODO: != width, if there is a luma margin. */
array_checksum(pic->y_recdata, pic->height, pic->width, pic->width, checksum_out[0]); array_checksum(pic->y_recdata, pic->height, pic->width, pic->width, checksum_out[0]);

View file

@ -19,8 +19,11 @@
#include "picture.h" #include "picture.h"
/*! //////////////////////////////////////////////////////////////////////////
* \brief NAL unit type codes // TYPES
/**
* \brief NAL unit type codes.
* *
* These are the nal_unit_type codes from Table 7-1 ITU-T H.265 v1.0. * These are the nal_unit_type codes from Table 7-1 ITU-T H.265 v1.0.
* The type codes have been prefixed with "NAL_". * The type codes have been prefixed with "NAL_".
@ -73,7 +76,15 @@ enum {
// Unspecified UNSPEC 48-63 // Unspecified UNSPEC 48-63
}; };
void nal_write(FILE* output, uint8_t* buffer, uint32_t buffer_len, uint8_t nal_ref, uint8_t nal_type, uint8_t temporal_id); #define SEI_HASH_MAX_LENGTH 4
void picture_checksum(const picture* pic, unsigned char checksum_out[][16]);
//////////////////////////////////////////////////////////////////////////
// FUNCTIONS
void nal_write(FILE *output, uint8_t *buffer, uint32_t buffer_len,
uint8_t nal_ref, uint8_t nal_type, uint8_t temporal_id);
void picture_checksum(const picture *pic,
unsigned char checksum_out[][SEI_HASH_MAX_LENGTH]);
#endif #endif