2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2014 Tampere University of Technology and others (see
|
2014-01-24 10:37:15 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as published
|
|
|
|
* by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* Kvazaar is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* \file
|
2012-06-05 12:38:54 +00:00
|
|
|
*/
|
|
|
|
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "nal.h"
|
2012-06-05 12:38:54 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-09-19 13:03:02 +00:00
|
|
|
#include <assert.h>
|
2013-09-18 09:16:03 +00:00
|
|
|
|
2012-06-05 12:38:54 +00:00
|
|
|
#include "config.h"
|
2012-06-06 13:20:29 +00:00
|
|
|
#include "bitstream.h"
|
|
|
|
#include "cabac.h"
|
|
|
|
#include "encoder.h"
|
|
|
|
|
2013-09-19 13:03:02 +00:00
|
|
|
/**
|
|
|
|
* \brief Write a Network Abstraction Layer (NAL) packet to the output.
|
|
|
|
*/
|
2014-04-03 10:40:16 +00:00
|
|
|
void nal_write(FILE *output, uint8_t nal_ref, uint8_t nal_type,
|
2014-03-24 13:31:15 +00:00
|
|
|
uint8_t temporal_id,int long_start_code)
|
2012-06-05 12:38:54 +00:00
|
|
|
{
|
|
|
|
uint8_t byte;
|
2012-06-11 15:43:29 +00:00
|
|
|
|
2013-09-19 13:03:02 +00:00
|
|
|
// Some useful constants
|
2012-06-06 10:42:02 +00:00
|
|
|
const uint8_t start_code_prefix_one_3bytes = 0x01;
|
|
|
|
const uint8_t zero = 0x00;
|
|
|
|
|
2014-02-03 13:31:58 +00:00
|
|
|
// zero_byte (0x00) shall be present in the byte stream NALU of VPS, SPS
|
|
|
|
// and PPS, or the first NALU of an access unit
|
|
|
|
if(long_start_code)
|
|
|
|
fwrite(&zero, 1, 1, output);
|
|
|
|
|
2013-09-19 13:03:02 +00:00
|
|
|
// start_code_prefix_one_3bytes
|
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-09-19 13:03:02 +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
|
|
|
|
byte = nal_type << 1;
|
2012-06-05 12:38:54 +00:00
|
|
|
fwrite(&byte, 1, 1, output);
|
|
|
|
|
2013-09-19 13:03:02 +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);
|
2013-09-05 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-19 13:03:02 +00:00
|
|
|
/**
|
|
|
|
* \brief Calculate checksum for one color of the picture.
|
|
|
|
* \param data Beginning of the pixel data for the picture.
|
|
|
|
* \param height Height of the picture.
|
|
|
|
* \param width Width of the picture.
|
|
|
|
* \param stride Width of one row in the pixel array.
|
|
|
|
*/
|
2014-02-21 13:00:20 +00:00
|
|
|
static void array_checksum(const pixel* data,
|
2013-09-19 13:03:02 +00:00
|
|
|
const int height, const int width,
|
2014-02-21 13:00:20 +00:00
|
|
|
const int stride,
|
2013-09-19 13:03:02 +00:00
|
|
|
unsigned char checksum_out[SEI_HASH_MAX_LENGTH])
|
2013-09-05 12:59:51 +00:00
|
|
|
{
|
2014-03-10 10:10:40 +00:00
|
|
|
uint8_t mask;
|
|
|
|
uint32_t checksum = 0;
|
|
|
|
int y, x;
|
2013-09-19 13:03:02 +00:00
|
|
|
|
|
|
|
assert(SEI_HASH_MAX_LENGTH >= 4);
|
|
|
|
|
2014-03-10 10:10:40 +00:00
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
mask = (uint8_t)((x & 0xff) ^ (y & 0xff) ^ (x >> 8) ^ (y >> 8));
|
|
|
|
checksum += (data[(y * stride) + x] & 0xff) ^ mask;
|
|
|
|
}
|
|
|
|
}
|
2013-09-05 12:59:51 +00:00
|
|
|
|
2013-09-19 13:03:02 +00:00
|
|
|
// Unpack uint into byte-array.
|
2014-03-10 10:10:40 +00:00
|
|
|
checksum_out[0] = (checksum >> 24) & 0xff;
|
|
|
|
checksum_out[1] = (checksum >> 16) & 0xff;
|
|
|
|
checksum_out[2] = (checksum >> 8) & 0xff;
|
|
|
|
checksum_out[3] = (checksum) & 0xff;
|
2013-09-05 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Calculate checksums for all colors of the picture.
|
|
|
|
\param pic The picture that checksum is calculated for.
|
|
|
|
\param checksum_out Result of the calculation.
|
|
|
|
\returns Void
|
|
|
|
*/
|
2013-09-19 13:03:02 +00:00
|
|
|
void picture_checksum(const picture* pic, unsigned char checksum_out[][SEI_HASH_MAX_LENGTH])
|
2013-09-05 12:59:51 +00:00
|
|
|
{
|
2014-03-10 10:10:40 +00:00
|
|
|
array_checksum(pic->y_recdata, pic->height, pic->width, pic->width, checksum_out[0]);
|
2013-09-05 12:59:51 +00:00
|
|
|
|
|
|
|
/* The number of chroma pixels is half that of luma. */
|
2014-03-10 10:10:40 +00:00
|
|
|
array_checksum(pic->u_recdata, pic->height >> 1, pic->width >> 1, pic->width >> 1, checksum_out[1]);
|
|
|
|
array_checksum(pic->v_recdata, pic->height >> 1, pic->width >> 1, pic->width >> 1, checksum_out[2]);
|
2014-02-03 14:32:54 +00:00
|
|
|
}
|