1 /*****************************************************************************
2 * hevc.c: h.265/hevc video packetizer
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
7 * Authors: Denis Charmet <typx@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
35 #include <vlc_block.h>
38 #include <vlc_block_helper.h>
39 #include "packetizer_helper.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open (vlc_object_t *);
45 static void Close(vlc_object_t *);
48 set_category(CAT_SOUT)
49 set_subcategory(SUBCAT_SOUT_PACKETIZER)
50 set_description(N_("HEVC/H.265 video packetizer"))
51 set_capability("packetizer", 50)
52 set_callbacks(Open, Close)
56 /****************************************************************************
58 ****************************************************************************/
59 static block_t *Packetize(decoder_t *, block_t **);
60 static void PacketizeReset(void *p_private, bool b_broken);
61 static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *);
62 static int PacketizeValidate(void *p_private, block_t *);
67 packetizer_t packetizer;
74 /* NAL types from https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-H.265-201304-I!!PDF-E&type=items */
87 /* 10 to 15 reserved */
95 /* 22 to 31 reserved */
100 AUD = 35, /* Access unit delimiter */
101 EOS = 36, /* End of sequence */
102 EOB = 37, /* End of bitstream */
103 FD = 38, /* Filler data*/
104 PREF_SEI = 39, /* Prefix SEI */
105 SUFF_SEI = 40, /* Suffix SEI */
109 static const uint8_t p_hevc_startcode[3] = {0x00, 0x00, 0x01};
111 /*****************************************************************************
113 *****************************************************************************/
114 static int Open(vlc_object_t *p_this)
116 decoder_t *p_dec = (decoder_t*)p_this;
118 if (p_dec->fmt_in.i_codec != VLC_CODEC_HEVC)
121 p_dec->p_sys = calloc(1, sizeof(decoder_sys_t));
125 packetizer_Init(&p_dec->p_sys->packetizer,
126 p_hevc_startcode, sizeof(p_hevc_startcode),
127 p_hevc_startcode, 1, 5,
128 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec);
130 /* Copy properties */
131 es_format_Copy(&p_dec->fmt_out, &p_dec->fmt_in);
134 p_dec->pf_packetize = Packetize;
140 /*****************************************************************************
142 *****************************************************************************/
143 static void Close(vlc_object_t *p_this)
145 decoder_t *p_dec = (decoder_t*)p_this;
146 decoder_sys_t *p_sys = p_dec->p_sys;
147 packetizer_Clean(&p_sys->packetizer);
152 /****************************************************************************
154 ****************************************************************************/
155 static block_t *Packetize(decoder_t *p_dec, block_t **pp_block)
157 decoder_sys_t *p_sys = p_dec->p_sys;
159 return packetizer_Packetize(&p_sys->packetizer, pp_block);
162 /****************************************************************************
164 ****************************************************************************/
165 static void PacketizeReset(void *p_private, bool b_broken)
167 VLC_UNUSED(b_broken);
169 decoder_t *p_dec = p_private;
170 decoder_sys_t *p_sys = p_dec->p_sys;
171 block_ChainRelease(p_sys->p_frame);
173 p_sys->p_frame = NULL;
174 p_sys->b_vcl = false;
177 static block_t *PacketizeParse(void *p_private, bool *pb_ts_used, block_t *p_block)
179 decoder_t *p_dec = p_private;
180 decoder_sys_t *p_sys = p_dec->p_sys;
182 block_t * p_nal = NULL;
184 while (p_block->i_buffer > 5 && p_block->p_buffer[p_block->i_buffer-1] == 0x00 )
188 bs_init(&bs, p_block->p_buffer+4, p_block->i_buffer-4);
191 uint32_t forbidden_zero_bit = bs_read1(&bs);
193 if (forbidden_zero_bit)
195 msg_Err(p_dec,"Forbidden zero bit not null, corrupted NAL");
196 p_sys->p_frame = NULL;
197 p_sys->b_vcl = false;
200 uint32_t nalu_type = bs_read(&bs,6);
205 /* NAL is a VCL NAL */
208 uint32_t first_slice_in_pic = bs_read1(&bs);
210 if (first_slice_in_pic && p_sys->p_frame)
212 p_nal = block_ChainGather(p_sys->p_frame);
213 p_sys->p_frame = NULL;
216 block_ChainAppend(&p_sys->p_frame, p_block);
222 p_nal = block_ChainGather(p_sys->p_frame);
223 p_nal->p_next = p_block;
224 p_sys->p_frame = NULL;
235 static int PacketizeValidate( void *p_private, block_t *p_au )
237 VLC_UNUSED(p_private);