2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
2014-01-24 10:37:15 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
2014-01-24 10:37:15 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* 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 Lesser General Public License for
|
|
|
|
* more details.
|
2014-01-24 10:37:15 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
2014-01-24 10:37:15 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "nal.h"
|
|
|
|
|
2012-06-06 13:20:29 +00:00
|
|
|
#include "bitstream.h"
|
2016-04-01 14:14:23 +00:00
|
|
|
#include "strategies/strategies-nal.h"
|
2019-05-21 09:28:05 +00:00
|
|
|
#include <stdio.h>
|
2016-04-01 14:14:23 +00:00
|
|
|
|
2012-06-06 13:20:29 +00:00
|
|
|
|
2013-09-19 13:03:02 +00:00
|
|
|
/**
|
|
|
|
* \brief Write a Network Abstraction Layer (NAL) packet to the output.
|
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_nal_write(bitstream_t * const bitstream, const uint8_t nal_type,
|
2014-04-17 04:55:35 +00:00
|
|
|
const uint8_t temporal_id, const 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;
|
|
|
|
|
2019-05-21 09:28:05 +00:00
|
|
|
#ifdef KVZ_DEBUG
|
|
|
|
printf("=========== NAL unit ===========\n");
|
|
|
|
#endif
|
|
|
|
|
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)
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_writebyte(bitstream, zero);
|
2014-02-03 13:31:58 +00:00
|
|
|
|
2013-09-19 13:03:02 +00:00
|
|
|
// start_code_prefix_one_3bytes
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_writebyte(bitstream, zero);
|
|
|
|
kvz_bitstream_writebyte(bitstream, zero);
|
|
|
|
kvz_bitstream_writebyte(bitstream, start_code_prefix_one_3bytes);
|
2012-06-05 12:38:54 +00:00
|
|
|
|
2013-09-19 13:03:02 +00:00
|
|
|
// Handle header bits with full bytes instead of using bitstream
|
2019-05-29 13:17:02 +00:00
|
|
|
// forbidden_zero_flag(1) + nuh_temporal_id_plus1(3) + nal_unit_type(4)
|
|
|
|
uint8_t zero_tid_required_flag = 0;
|
2019-11-14 07:21:11 +00:00
|
|
|
|
2019-05-29 13:17:02 +00:00
|
|
|
if ((nal_type >= 16) && (nal_type <= 31)) {
|
|
|
|
zero_tid_required_flag = 1;
|
|
|
|
}
|
|
|
|
uint8_t nal_type_lsb = nal_type - (zero_tid_required_flag << 4);
|
2019-11-14 07:21:11 +00:00
|
|
|
|
|
|
|
// forbidden zero (1bit) + reserver zero (1bit) layer_id (6 bits)
|
2020-10-01 08:10:40 +00:00
|
|
|
byte = 0;
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_writebyte(bitstream, byte);
|
2012-06-05 12:38:54 +00:00
|
|
|
|
2019-11-14 07:21:11 +00:00
|
|
|
// nal_unit_type (5bits) + temporal_id_plus1 (3 bits)
|
|
|
|
byte = (nal_type<<3)+(temporal_id + 1);
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_writebyte(bitstream, byte);
|
2019-05-21 09:28:05 +00:00
|
|
|
|
2019-11-14 07:21:11 +00:00
|
|
|
|
2019-05-21 09:28:05 +00:00
|
|
|
#if VERBOSE
|
2019-11-14 07:21:11 +00:00
|
|
|
// ToDo: Match with the actual bits
|
2020-09-28 11:58:07 +00:00
|
|
|
printf("%-50s u(%d) : %d\n", "zero_tid_required_flag", 1, zero_tid_required_flag);
|
|
|
|
printf("%-50s u(%d) : %d\n", "nuh_temporal_id_plus1", 3, temporal_id + 1);
|
|
|
|
printf("%-50s u(%d) : %d\n", "nal_unit_type_lsb", 4, nal_type_lsb);
|
2020-10-01 08:10:40 +00:00
|
|
|
printf("%-50s u(%d) : %d\n", "nuh_layer_id_plus1", 7, 0);
|
2020-09-28 11:58:07 +00:00
|
|
|
printf("%-50s u(%d) : %d\n", "nuh_reserved_zero_bit", 1, 0);
|
2019-05-21 09:28:05 +00:00
|
|
|
#endif
|
2013-09-05 12:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Calculate checksums for all colors of the picture.
|
2014-06-05 12:54:58 +00:00
|
|
|
\param im The image that checksum is calculated for.
|
2013-09-05 12:59:51 +00:00
|
|
|
\param checksum_out Result of the calculation.
|
|
|
|
\returns Void
|
|
|
|
*/
|
2015-08-26 08:50:27 +00:00
|
|
|
void kvz_image_checksum(const kvz_picture *im, unsigned char checksum_out[][SEI_HASH_MAX_LENGTH], const uint8_t bitdepth)
|
2013-09-05 12:59:51 +00:00
|
|
|
{
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_array_checksum(im->y, im->height, im->width, im->width, checksum_out[0], bitdepth);
|
2013-09-05 12:59:51 +00:00
|
|
|
|
|
|
|
/* The number of chroma pixels is half that of luma. */
|
2016-08-25 13:05:46 +00:00
|
|
|
if (im->chroma_format != KVZ_CSP_400) {
|
|
|
|
kvz_array_checksum(im->u, im->height >> 1, im->width >> 1, im->width >> 1, checksum_out[1], bitdepth);
|
|
|
|
kvz_array_checksum(im->v, im->height >> 1, im->width >> 1, im->width >> 1, checksum_out[2], bitdepth);
|
|
|
|
}
|
2014-02-03 14:32:54 +00:00
|
|
|
}
|
2016-03-17 16:12:43 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Calculate md5 for all colors of the picture.
|
|
|
|
\param im The image that md5 is calculated for.
|
|
|
|
\param checksum_out Result of the calculation.
|
|
|
|
\returns Void
|
|
|
|
*/
|
|
|
|
void kvz_image_md5(const kvz_picture *im, unsigned char checksum_out[][SEI_HASH_MAX_LENGTH], const uint8_t bitdepth)
|
|
|
|
{
|
|
|
|
kvz_array_md5(im->y, im->height, im->width, im->width, checksum_out[0], bitdepth);
|
|
|
|
|
|
|
|
/* The number of chroma pixels is half that of luma. */
|
2016-08-25 13:05:46 +00:00
|
|
|
if (im->chroma_format != KVZ_CSP_400) {
|
|
|
|
kvz_array_md5(im->u, im->height >> 1, im->width >> 1, im->width >> 1, checksum_out[1], bitdepth);
|
|
|
|
kvz_array_md5(im->v, im->height >> 1, im->width >> 1, im->width >> 1, checksum_out[2], bitdepth);
|
|
|
|
}
|
2016-03-17 16:12:43 +00:00
|
|
|
}
|