qt: playlist: use item title if available
[vlc.git] / modules / mux / mpeg / repack.c
blob6afc5178b72ff94246130df86f1ca09ba08c7120
1 /*****************************************************************************
2 * repack.c: Codec specific formatting for AnnexB multiplexers
3 *****************************************************************************
4 * Copyright (C) 2021 VideoLabs, VLC authors and VideoLAN
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 *****************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <vlc_common.h>
25 #include <vlc_block.h>
27 #include "repack.h"
28 #include "../../packetizer/hevc_nal.h"
29 #include "../../packetizer/h264_nal.h"
30 #include "../../packetizer/hxxx_nal.h"
32 #include <assert.h>
34 static void AnnexBInject(block_t **pp_pes,
35 const uint8_t *p_extra, size_t i_extra,
36 const uint8_t *p_au, size_t i_au)
38 if(!i_extra && !i_au)
39 return;
41 *pp_pes = block_Realloc(*pp_pes,
42 i_extra + i_au,
43 (*pp_pes)->i_buffer);
44 if(!*pp_pes)
45 return;
46 if(i_au)
47 memcpy(&(*pp_pes)->p_buffer[0], p_au, i_au);
48 if(i_extra)
49 memcpy(&(*pp_pes)->p_buffer[i_au], p_extra, i_extra);
53 static void PES_RepackHEVC(block_t **pp_pes,
54 const uint8_t *p_extra, size_t i_extra)
56 size_t i_au = 6;
57 size_t i_aucurrent = 0;
58 const uint8_t audata[] = { 0x00, 0x00, 0x00, 0x01, 0x46, 0x01 };
59 hxxx_iterator_ctx_t ctx;
60 hxxx_iterator_init(&ctx, (*pp_pes)->p_buffer, (*pp_pes)->i_buffer, 0);
61 const uint8_t *p_nal; size_t i_nal;
62 while(hxxx_annexb_iterate_next(&ctx, &p_nal, &i_nal))
64 if(i_nal < 2)
65 return;
66 uint8_t type = hevc_getNALType(p_nal);
67 if(type < HEVC_NAL_VPS)
68 break;
69 switch(type)
71 case HEVC_NAL_AUD:
72 i_au = 0;
73 i_aucurrent = 2 + (p_nal - (*pp_pes)->p_buffer);
74 break;
75 case HEVC_NAL_VPS:
76 case HEVC_NAL_PPS:
77 case HEVC_NAL_SPS:
78 i_extra = 0;
79 break;
80 default:
81 break;
85 if(i_extra && i_aucurrent) /* strip existing AU for now */
87 (*pp_pes)->p_buffer += i_aucurrent;
88 (*pp_pes)->i_buffer -= i_aucurrent;
89 i_au = 6;
92 AnnexBInject(pp_pes, p_extra, i_extra, audata, i_au);
95 static void PES_RepackH264(block_t **pp_pes,
96 const uint8_t *p_extra, size_t i_extra)
98 size_t i_au = 6;
99 size_t i_aucurrent = 0;
100 const uint8_t audata[] = { 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0 };
101 hxxx_iterator_ctx_t ctx;
102 hxxx_iterator_init(&ctx, (*pp_pes)->p_buffer, (*pp_pes)->i_buffer, 0);
103 const uint8_t *p_nal; size_t i_nal;
104 while(hxxx_annexb_iterate_next(&ctx, &p_nal, &i_nal))
106 if(i_nal < 2)
107 return;
108 uint8_t type = p_nal[0]&0x1f;
109 if(type < H264_NAL_SEI)
110 break;
111 switch(type)
113 case H264_NAL_AU_DELIMITER:
114 i_au = 0;
115 i_aucurrent = 2 + (p_nal - (*pp_pes)->p_buffer);
116 break;
117 case H264_NAL_SPS:
118 case H264_NAL_PPS:
119 i_extra = 0;
120 break;
121 default:
122 break;
126 if(i_extra && i_aucurrent) /* strip existing AU for now */
128 (*pp_pes)->p_buffer += i_aucurrent;
129 (*pp_pes)->i_buffer -= i_aucurrent;
130 i_au = 6;
133 AnnexBInject(pp_pes, p_extra, i_extra, audata, i_au);
136 static void PES_RepackMP4V(block_t **pp_pes,
137 const uint8_t *p_extra, size_t i_extra)
139 hxxx_iterator_ctx_t ctx;
140 hxxx_iterator_init(&ctx, (*pp_pes)->p_buffer, (*pp_pes)->i_buffer, 0);
141 const uint8_t *p_nal; size_t i_nal;
142 while(hxxx_annexb_iterate_next(&ctx, &p_nal, &i_nal))
144 if(i_nal < 2)
145 return;
146 if(p_nal[0] >= 0x30) /* > VOLS */
147 break;
148 if(p_nal[0] >= 0x20 && p_nal[0] == p_extra[3]) /* same VOL */
149 i_extra = 0;
152 AnnexBInject(pp_pes, p_extra, i_extra, NULL, 0);
155 block_t * PES_Repack(vlc_fourcc_t i_codec,
156 const uint8_t *p_extra, size_t i_extra,
157 block_t **pp_pes)
159 /* safety check for annexb extra */
160 if(i_extra < 4 ||
161 (memcmp(p_extra, annexb_startcode4, 4) &&
162 memcmp(&p_extra[1], annexb_startcode3, 3)))
163 i_extra = 0;
165 switch(i_codec)
167 case VLC_CODEC_HEVC:
168 PES_RepackHEVC(pp_pes, p_extra, i_extra);
169 break;
170 case VLC_CODEC_H264:
171 PES_RepackH264(pp_pes, p_extra, i_extra);
172 break;
173 case VLC_CODEC_MP4V:
174 PES_RepackMP4V(pp_pes, p_extra, i_extra);
175 break;
176 default:
177 break;
179 return *pp_pes;