1 /*****************************************************************************
2 * h264.c: h264/avc video packetizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: mpeg4video.c 7338 2004-04-13 10:52:29Z gbazin $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
9 * Gildas Bazin <gbazin@netcourrier.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/decoder.h>
35 #include "vlc_block_helper.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
45 set_description( _("H264 video packetizer") );
46 set_capability( "packetizer", 50 );
47 set_callbacks( Open, Close );
51 /****************************************************************************
53 ****************************************************************************/
54 static block_t *Packetize( decoder_t *, block_t ** );
58 block_bytestream_t bytestream;
87 NAL_SLICE_IDR = 5, /* ref_idc != 0 */
88 NAL_SEI = 6, /* ref_idc == 0 */
91 /* ref_idc == 0 for 6,9,10,11,12 */
96 NAL_PRIORITY_DISPOSABLE = 0,
98 NAL_PRIORITY_HIGH = 2,
99 NAL_PRIORITY_HIGHEST = 3,
102 static block_t *ParseNALBlock( decoder_t *, block_t * );
104 /*****************************************************************************
105 * Open: probe the packetizer and return score
106 *****************************************************************************/
107 static int Open( vlc_object_t *p_this )
109 decoder_t *p_dec = (decoder_t*)p_this;
110 decoder_sys_t *p_sys;
112 if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') &&
113 p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') )
118 /* Allocate the memory needed to store the decoder's structure */
119 if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
121 msg_Err( p_dec, "out of memory" );
124 p_sys->i_state = STATE_NOSYNC;
126 p_sys->startcode[0] = 0;
127 p_sys->startcode[1] = 0;
128 p_sys->startcode[2] = 0;
129 p_sys->startcode[3] = 1;
130 p_sys->bytestream = block_BytestreamInit( p_dec );
131 p_sys->b_slice = VLC_FALSE;
132 p_sys->p_frame = NULL;
136 p_sys->b_sps = VLC_FALSE;
138 /* Setup properties */
139 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
140 p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
143 if( p_dec->fmt_in.i_extra )
146 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
147 p_dec->fmt_out.p_extra = malloc( p_dec->fmt_in.i_extra );
148 memcpy( p_dec->fmt_out.p_extra, p_dec->fmt_in.p_extra,
149 p_dec->fmt_in.i_extra );
151 msg_Dbg( p_dec, "opening with vol size:%d", p_dec->fmt_in.i_extra );
152 m4v_VOLParse( &p_dec->fmt_out,
153 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
157 /* No vol, we'll have to look for one later on */
158 p_dec->fmt_out.i_extra = 0;
159 p_dec->fmt_out.p_extra = 0;
164 p_dec->pf_packetize = Packetize;
169 /*****************************************************************************
170 * Close: clean up the packetizer
171 *****************************************************************************/
172 static void Close( vlc_object_t *p_this )
174 decoder_t *p_dec = (decoder_t*)p_this;
175 decoder_sys_t *p_sys = p_dec->p_sys;
177 block_BytestreamRelease( &p_sys->bytestream );
181 /****************************************************************************
182 * Packetize: the whole thing
183 ****************************************************************************/
184 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
186 decoder_sys_t *p_sys = p_dec->p_sys;
189 if( !pp_block || !*pp_block ) return NULL;
191 block_BytestreamPush( &p_sys->bytestream, *pp_block );
195 switch( p_sys->i_state )
198 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
199 &p_sys->i_offset, p_sys->startcode, 4 ) == VLC_SUCCESS )
201 p_sys->i_state = STATE_NEXT_SYNC;
204 if( p_sys->i_offset )
206 block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
208 block_BytestreamFlush( &p_sys->bytestream );
211 if( p_sys->i_state != STATE_NEXT_SYNC )
217 p_sys->i_offset = 1; /* To find next startcode */
219 case STATE_NEXT_SYNC:
220 /* Find the next startcode */
221 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
222 &p_sys->i_offset, p_sys->startcode, 4 ) != VLC_SUCCESS )
228 /* Get the new fragment and set the pts/dts */
229 p_pic = block_New( p_dec, p_sys->i_offset );
230 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
231 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
233 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
239 if( !( p_pic = ParseNALBlock( p_dec, p_pic ) ) )
241 p_sys->i_state = STATE_NOSYNC;
245 /* So p_block doesn't get re-added several times */
246 *pp_block = block_BytestreamPop( &p_sys->bytestream );
248 p_sys->i_state = STATE_NOSYNC;
255 static void nal_get_decoded( uint8_t **pp_ret, int *pi_ret, uint8_t *src, int i_src )
257 uint8_t *end = &src[i_src];
258 uint8_t *dst = malloc( i_src );
264 if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 && src[2] == 0x03 )
274 *pi_ret = dst - *pp_ret;
277 static inline int bs_read_ue( bs_t *s )
281 while( bs_read1( s ) == 0 && s->p < s->p_end && i < 32 )
285 return( ( 1 << i) - 1 + bs_read( s, i ) );
287 static inline int bs_read_se( bs_t *s )
289 int val = bs_read_ue( s );
291 return val&0x01 ? (val+1)/2 : -(val/2);
295 static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
297 decoder_sys_t *p_sys = p_dec->p_sys;
298 block_t *p_pic = NULL;
300 const int i_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
301 const int i_nal_type= p_frag->p_buffer[4]&0x1f;
303 if( p_sys->b_slice &&
304 ( i_nal_type == NAL_SLICE || i_nal_type == NAL_SLICE_IDR ||
305 i_nal_type == NAL_SLICE_DPC || i_nal_type == NAL_SPS || i_nal_type == NAL_PPS ) )
309 p_pic = block_ChainGather( p_sys->p_frame );
310 p_pic->i_dts = p_sys->i_dts;
311 p_pic->i_pts = p_sys->i_pts;
312 p_pic->i_length = 0; /* FIXME */
313 p_pic->i_flags = p_sys->i_flags;
317 block_ChainRelease( p_sys->p_frame );
321 p_sys->p_frame = NULL;
322 p_sys->b_slice = VLC_FALSE;
323 //p_sys->i_dts += 40000;
326 if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
332 p_sys->b_slice = VLC_TRUE;
333 p_sys->i_dts = p_frag->i_dts;
334 p_sys->i_pts = p_frag->i_pts;
336 /* do not convert the whole frame */
337 nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5], __MIN( p_frag->i_buffer - 5, 60 ) );
338 bs_init( &s, dec, i_dec );
343 switch( bs_read_ue( &s ) )
346 p_sys->i_flags = BLOCK_FLAG_TYPE_P;
349 p_sys->i_flags =BLOCK_FLAG_TYPE_B;
352 p_sys->i_flags = BLOCK_FLAG_TYPE_I;
354 case 3: case 8: /* SP */
355 p_sys->i_flags = BLOCK_FLAG_TYPE_P;
358 p_sys->i_flags = BLOCK_FLAG_TYPE_I;
364 else if( i_nal_type == NAL_SPS )
371 p_sys->b_sps = VLC_TRUE;
373 nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
375 bs_init( &s, dec, i_dec );
376 /* Skip profile(8), constraint_set012, reserver(5), level(8) */
377 bs_skip( &s, 8 + 1+1+1 + 5 + 8 );
380 /* Skip i_log2_max_frame_num */
383 i_tmp = bs_read_ue( &s );
386 /* skip i_log2_max_poc_lsb */
389 else if( i_tmp == 1 )
392 /* skip b_delta_pic_order_always_zero */
394 /* skip i_offset_for_non_ref_pic */
396 /* skip i_offset_for_top_to_bottom_field */
398 /* read i_num_ref_frames_in_poc_cycle */
399 i_cycle = bs_read_ue( &s );
400 if( i_cycle > 256 ) i_cycle = 256;
403 /* skip i_offset_for_ref_frame */
407 /* i_num_ref_frames */
409 /* b_gaps_in_frame_num_value_allowed */
413 p_dec->fmt_out.video.i_width = 16 * ( bs_read_ue( &s ) + 1 );
414 p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
416 /* b_frame_mbs_only */
417 i_tmp = bs_read( &s, 1 );
422 /* b_direct8x8_inference */
426 i_tmp = bs_read( &s, 1 );
430 p_dec->fmt_out.video.i_width -= 2 * bs_read_ue( &s );
432 p_dec->fmt_out.video.i_width -= 2 * bs_read_ue( &s );
434 p_dec->fmt_out.video.i_height -= 2 * bs_read_ue( &s );
436 p_dec->fmt_out.video.i_height -= 2 * bs_read_ue( &s );
440 i_tmp = bs_read( &s, 1 );
443 /* read the aspect ratio part if any FIXME check it */
444 i_tmp = bs_read( &s, 1 );
447 static const struct { int w, h; } sar[14] =
449 { 0, 0 }, { 1, 1 }, { 12, 11 }, { 10, 11 },
450 { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
451 { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
452 { 64, 33 }, { 160,99 },
454 int i_sar = bs_read( &s, 8 );
464 w = bs_read( &s, 16 );
465 h = bs_read( &s, 16 );
467 p_dec->fmt_out.video.i_aspect =
470 p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
476 else if( i_nal_type == NAL_PPS )
479 bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
485 /* Append the block */
486 block_ChainAppend( &p_sys->p_frame, p_frag );