1 /*****************************************************************************
2 * Copyright © 2010-2014 VideoLAN
4 * Authors: Thomas Guillem <thomas.guillem@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 /* Inspired by libavcodec/hevc.c */
26 int convert_hevc_nal_units(decoder_t *p_dec, const uint8_t *p_buf,
27 uint32_t i_buf_size, uint8_t *p_out_buf,
28 uint32_t i_out_buf_size, uint32_t *p_sps_pps_size,
32 const uint8_t *p_end = p_buf + i_buf_size;
33 uint32_t i_sps_pps_size = 0;
35 if( i_buf_size <= 3 || ( !p_buf[0] && !p_buf[1] && p_buf[2] <= 1 ) )
38 if( p_end - p_buf < 23 )
40 msg_Err( p_dec, "Input Metadata too small" );
47 *p_nal_size = (*p_buf & 0x03) + 1;
50 num_arrays = *p_buf++;
52 for( i = 0; i < num_arrays; i++ )
56 if( p_end - p_buf < 3 )
58 msg_Err( p_dec, "Input Metadata too small" );
61 type = *(p_buf++) & 0x3f;
64 cnt = p_buf[0] << 8 | p_buf[1];
67 for( j = 0; j < cnt; j++ )
71 if( p_end - p_buf < 2 )
73 msg_Err( p_dec, "Input Metadata too small" );
77 i_nal_size = p_buf[0] << 8 | p_buf[1];
80 if( i_nal_size < 0 || p_end - p_buf < i_nal_size )
82 msg_Err( p_dec, "NAL unit size does not match Input Metadata size" );
86 if( i_sps_pps_size + 4 + i_nal_size > i_out_buf_size )
88 msg_Err( p_dec, "Output buffer too small" );
92 p_out_buf[i_sps_pps_size++] = 0;
93 p_out_buf[i_sps_pps_size++] = 0;
94 p_out_buf[i_sps_pps_size++] = 0;
95 p_out_buf[i_sps_pps_size++] = 1;
97 memcpy(p_out_buf + i_sps_pps_size, p_buf, i_nal_size);
100 i_sps_pps_size += i_nal_size;
104 *p_sps_pps_size = i_sps_pps_size;