1 /*****************************************************************************
2 * Copyright © 2010-2014 VideoLAN
4 * Authors: Jean-Baptiste Kempf <jb@videolan.org>
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 int convert_sps_pps( decoder_t
*p_dec
, const uint8_t *p_buf
,
26 uint32_t i_buf_size
, uint8_t *p_out_buf
,
27 uint32_t i_out_buf_size
, uint32_t *p_sps_pps_size
,
31 uint32_t i_data_size
= i_buf_size
, i_nal_size
, i_sps_pps_size
= 0;
32 unsigned int i_loop_end
;
37 msg_Err( p_dec
, "Input Metadata too small" );
41 /* Read infos in first 6 bytes */
42 i_profile
= (p_buf
[1] << 16) | (p_buf
[2] << 8) | p_buf
[3];
44 *p_nal_size
= (p_buf
[4] & 0x03) + 1;
48 for ( unsigned int j
= 0; j
< 2; j
++ )
50 /* First time is SPS, Second is PPS */
53 msg_Err( p_dec
, "PPS too small after processing SPS/PPS %u",
57 i_loop_end
= p_buf
[0] & (j
== 0 ? 0x1f : 0xff);
58 p_buf
++; i_data_size
--;
60 for ( unsigned int i
= 0; i
< i_loop_end
; i
++)
64 msg_Err( p_dec
, "SPS is too small %u", i_data_size
);
68 i_nal_size
= (p_buf
[0] << 8) | p_buf
[1];
72 if( i_data_size
< i_nal_size
)
74 msg_Err( p_dec
, "SPS size does not match NAL specified size %u",
78 if( i_sps_pps_size
+ 4 + i_nal_size
> i_out_buf_size
)
80 msg_Err( p_dec
, "Output SPS/PPS buffer too small" );
84 p_out_buf
[i_sps_pps_size
++] = 0;
85 p_out_buf
[i_sps_pps_size
++] = 0;
86 p_out_buf
[i_sps_pps_size
++] = 0;
87 p_out_buf
[i_sps_pps_size
++] = 1;
89 memcpy( p_out_buf
+ i_sps_pps_size
, p_buf
, i_nal_size
);
90 i_sps_pps_size
+= i_nal_size
;
93 i_data_size
-= i_nal_size
;
97 *p_sps_pps_size
= i_sps_pps_size
;
102 void convert_h264_to_annexb( uint8_t *p_buf
, uint32_t i_len
,
104 struct H264ConvertState
*state
)
106 if( i_nal_size
< 3 || i_nal_size
> 4 )
109 /* This only works for NAL sizes 3-4 */
112 if( state
->nal_pos
< i_nal_size
) {
114 for( i
= 0; state
->nal_pos
< i_nal_size
&& i
< i_len
; i
++, state
->nal_pos
++ ) {
115 state
->nal_len
= (state
->nal_len
<< 8) | p_buf
[i
];
118 if( state
->nal_pos
< i_nal_size
)
124 if( state
->nal_len
> INT_MAX
)
126 if( state
->nal_len
> i_len
)
128 state
->nal_len
-= i_len
;
133 p_buf
+= state
->nal_len
;
134 i_len
-= state
->nal_len
;
141 bool h264_get_profile_level(const es_format_t
*p_fmt
, size_t *p_profile
,
142 size_t *p_level
, size_t *p_nal_size
)
144 uint8_t *p
= (uint8_t*)p_fmt
->p_extra
;
145 if(!p
|| !p_fmt
->p_extra
) return false;
147 /* Check the profile / level */
148 if (p_fmt
->i_original_fourcc
== VLC_FOURCC('a','v','c','1') && p
[0] == 1)
150 if (p_fmt
->i_extra
< 12) return false;
151 if (p_nal_size
) *p_nal_size
= 1 + (p
[4]&0x03);
152 if (!(p
[5]&0x1f)) return false;
157 if (p_fmt
->i_extra
< 8) return false;
158 if (!p
[0] && !p
[1] && !p
[2] && p
[3] == 1) p
+= 4;
159 else if (!p
[0] && !p
[1] && p
[2] == 1) p
+= 3;
163 if ( ((*p
++)&0x1f) != 7) return false;
165 /* Get profile/level out of first SPS */
166 if (p_profile
) *p_profile
= p
[0];
167 if (p_level
) *p_level
= p
[2];