2012-06-05 12:38:54 +00:00
|
|
|
/**
|
|
|
|
* HEVC Encoder
|
2013-04-16 08:23:03 +00:00
|
|
|
* - Marko Viitanen ( fador at iki.fi ), Tampere University of Technology, Department of Pervasive Computing.
|
2012-06-05 12:38:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file nal.c
|
|
|
|
\brief NAL
|
|
|
|
\author Marko Viitanen
|
2013-08-02 13:35:30 +00:00
|
|
|
\date 2013-06
|
2012-06-05 12:38:54 +00:00
|
|
|
|
|
|
|
NAL functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "global.h"
|
|
|
|
#include "config.h"
|
2012-06-06 13:20:29 +00:00
|
|
|
#include "bitstream.h"
|
2012-06-05 12:38:54 +00:00
|
|
|
#include "picture.h"
|
2012-06-06 13:20:29 +00:00
|
|
|
#include "cabac.h"
|
|
|
|
#include "encoder.h"
|
|
|
|
|
|
|
|
|
2012-06-05 12:38:54 +00:00
|
|
|
#include "nal.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
uint32_t i;
|
|
|
|
uint8_t zerocount=0;
|
2012-06-11 15:43:29 +00:00
|
|
|
|
|
|
|
/* Some useful constants */
|
2012-06-06 10:42:02 +00:00
|
|
|
const uint8_t emulation_prevention_three_byte = 0x03;
|
|
|
|
const uint8_t start_code_prefix_one_3bytes = 0x01;
|
|
|
|
const uint8_t zero = 0x00;
|
|
|
|
|
2012-06-11 15:43:29 +00:00
|
|
|
/*start_code_prefix_one_3bytes */
|
2013-08-02 13:35:30 +00:00
|
|
|
|
2013-02-24 14:03:40 +00:00
|
|
|
/*
|
2013-02-21 14:45:22 +00:00
|
|
|
if(temporal_id == 0)
|
|
|
|
{
|
|
|
|
fwrite(&zero, 1, 1, output);
|
2013-08-02 13:35:30 +00:00
|
|
|
}
|
|
|
|
*/
|
2012-06-05 12:38:54 +00:00
|
|
|
fwrite(&zero, 1, 1, output);
|
|
|
|
fwrite(&zero, 1, 1, output);
|
|
|
|
fwrite(&start_code_prefix_one_3bytes, 1, 1, output);
|
|
|
|
|
2013-02-21 14:45:22 +00:00
|
|
|
/* Handle header bits with full bytes instead of using bitstream */
|
|
|
|
/* forbidden_zero_flag(1) + nal_unit_type(6) + 1bit of nuh_layer_id*/
|
2012-08-14 08:02:08 +00:00
|
|
|
byte = nal_type<<1;
|
2012-06-05 12:38:54 +00:00
|
|
|
fwrite(&byte, 1, 1, output);
|
|
|
|
|
2013-02-21 14:45:22 +00:00
|
|
|
/* 5bits of nuh_layer_id + nuh_temporal_id_plus1(3) */
|
|
|
|
byte = (temporal_id+1)&7;
|
2012-06-05 12:38:54 +00:00
|
|
|
fwrite(&byte, 1, 1, output);
|
|
|
|
|
|
|
|
/* Write out bytes and add emulation_prevention_three_byte when needed */
|
|
|
|
for(i = 0; i < buffer_len; i++)
|
|
|
|
{
|
|
|
|
if(zerocount == 2 && buffer[i] < 4) /* Prevent 0x0000 + 00/01/02/03 */
|
|
|
|
{
|
|
|
|
/* Inserting 0x03 */
|
|
|
|
fwrite(&emulation_prevention_three_byte, 1, 1, output);
|
|
|
|
zerocount = 0;
|
|
|
|
}
|
|
|
|
if(buffer[i] == 0)
|
2012-06-11 15:43:29 +00:00
|
|
|
{
|
2012-06-05 12:38:54 +00:00
|
|
|
zerocount++;
|
2012-06-11 15:43:29 +00:00
|
|
|
}
|
2012-06-05 12:38:54 +00:00
|
|
|
else
|
2012-06-11 15:43:29 +00:00
|
|
|
{
|
2012-06-05 12:38:54 +00:00
|
|
|
zerocount = 0;
|
2012-06-11 15:43:29 +00:00
|
|
|
}
|
2012-06-05 12:38:54 +00:00
|
|
|
|
2012-06-06 10:42:02 +00:00
|
|
|
/* Write the actual data */
|
2012-06-05 12:38:54 +00:00
|
|
|
fwrite(&buffer[i], 1, 1, output);
|
|
|
|
}
|
2012-06-06 10:42:02 +00:00
|
|
|
|
2012-06-11 15:43:29 +00:00
|
|
|
/* If last byte was 0, add emulation_prevention_three_byte */
|
2012-06-06 10:42:02 +00:00
|
|
|
if(buffer[buffer_len-1] == 0)
|
2012-06-11 15:43:29 +00:00
|
|
|
{
|
2012-06-06 10:42:02 +00:00
|
|
|
fwrite(&emulation_prevention_three_byte, 1, 1, output);
|
2012-06-11 15:43:29 +00:00
|
|
|
}
|
2012-06-05 12:38:54 +00:00
|
|
|
|
|
|
|
}
|