]> git.sesse.net Git - vlc/blob - modules/packetizer/h264.c
* h264: fixed nal decoding.
[vlc] / modules / packetizer / h264.c
1 /*****************************************************************************
2  * h264.c: h264/avc video packetizer
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/decoder.h>
33 #include <vlc/sout.h>
34
35 #include "vlc_block_helper.h"
36 #include "vlc_bits.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 static int  Open ( vlc_object_t * );
42 static void Close( vlc_object_t * );
43
44 vlc_module_begin();
45     set_description( _("H264 video packetizer") );
46     set_capability( "packetizer", 50 );
47     set_callbacks( Open, Close );
48 vlc_module_end();
49
50
51 /****************************************************************************
52  * Local prototypes
53  ****************************************************************************/
54 static block_t *Packetize( decoder_t *, block_t ** );
55
56 struct decoder_sys_t
57 {
58     block_bytestream_t bytestream;
59
60     int     i_state;
61     int     i_offset;
62     uint8_t startcode[4];
63
64     vlc_bool_t b_slice;
65     block_t    *p_frame;
66
67     int64_t      i_dts;
68     int64_t      i_pts;
69     unsigned int i_flags;
70
71     vlc_bool_t   b_sps;
72 };
73
74 enum
75 {
76     STATE_NOSYNC,
77     STATE_NEXT_SYNC,
78 };
79
80 enum nal_unit_type_e
81 {
82     NAL_UNKNOWN = 0,
83     NAL_SLICE   = 1,
84     NAL_SLICE_DPA   = 2,
85     NAL_SLICE_DPB   = 3,
86     NAL_SLICE_DPC   = 4,
87     NAL_SLICE_IDR   = 5,    /* ref_idc != 0 */
88     NAL_SEI         = 6,    /* ref_idc == 0 */
89     NAL_SPS         = 7,
90     NAL_PPS         = 8
91     /* ref_idc == 0 for 6,9,10,11,12 */
92 };
93
94 enum nal_priority_e
95 {
96     NAL_PRIORITY_DISPOSABLE = 0,
97     NAL_PRIORITY_LOW        = 1,
98     NAL_PRIORITY_HIGH       = 2,
99     NAL_PRIORITY_HIGHEST    = 3,
100 };
101
102 static block_t *ParseNALBlock( decoder_t *, block_t * );
103
104 /*****************************************************************************
105  * Open: probe the packetizer and return score
106  *****************************************************************************/
107 static int Open( vlc_object_t *p_this )
108 {
109     decoder_t     *p_dec = (decoder_t*)p_this;
110     decoder_sys_t *p_sys;
111
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') )
114     {
115         return VLC_EGENERIC;
116     }
117
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 )
120     {
121         msg_Err( p_dec, "out of memory" );
122         return VLC_EGENERIC;
123     }
124     p_sys->i_state = STATE_NOSYNC;
125     p_sys->i_offset = 0;
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;
133     p_sys->i_dts   = 0;
134     p_sys->i_pts   = 0;
135     p_sys->i_flags = 0;
136     p_sys->b_sps   = VLC_FALSE;
137
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' );
141
142 #if 0
143     if( p_dec->fmt_in.i_extra )
144     {
145         /* We have a vol */
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 );
150
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 );
154     }
155     else
156     {
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;
160     }
161 #endif
162
163     /* Set callback */
164     p_dec->pf_packetize = Packetize;
165
166     return VLC_SUCCESS;
167 }
168
169 /*****************************************************************************
170  * Close: clean up the packetizer
171  *****************************************************************************/
172 static void Close( vlc_object_t *p_this )
173 {
174     decoder_t *p_dec = (decoder_t*)p_this;
175     decoder_sys_t *p_sys = p_dec->p_sys;
176
177     block_BytestreamRelease( &p_sys->bytestream );
178     free( p_sys );
179 }
180
181 /****************************************************************************
182  * Packetize: the whole thing
183  ****************************************************************************/
184 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
185 {
186     decoder_sys_t *p_sys = p_dec->p_sys;
187     block_t       *p_pic;
188
189     if( !pp_block || !*pp_block ) return NULL;
190
191     block_BytestreamPush( &p_sys->bytestream, *pp_block );
192
193     for( ;; )
194     {
195         switch( p_sys->i_state )
196         {
197             case STATE_NOSYNC:
198                 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
199                         &p_sys->i_offset, p_sys->startcode, 4 ) == VLC_SUCCESS )
200                 {
201                     p_sys->i_state = STATE_NEXT_SYNC;
202                 }
203
204                 if( p_sys->i_offset )
205                 {
206                     block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
207                     p_sys->i_offset = 0;
208                     block_BytestreamFlush( &p_sys->bytestream );
209                 }
210
211                 if( p_sys->i_state != STATE_NEXT_SYNC )
212                 {
213                     /* Need more data */
214                     return NULL;
215                 }
216
217                 p_sys->i_offset = 1; /* To find next startcode */
218
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 )
223                 {
224                     /* Need more data */
225                     return NULL;
226                 }
227
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;
232
233                 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
234                                 p_pic->i_buffer );
235
236                 p_sys->i_offset = 0;
237
238                 /* Parse the NAL */
239                 if( !( p_pic = ParseNALBlock( p_dec, p_pic ) ) )
240                 {
241                     p_sys->i_state = STATE_NOSYNC;
242                     break;
243                 }
244
245                 /* So p_block doesn't get re-added several times */
246                 *pp_block = block_BytestreamPop( &p_sys->bytestream );
247
248                 p_sys->i_state = STATE_NOSYNC;
249
250                 return p_pic;
251         }
252     }
253 }
254
255 static void nal_get_decoded( uint8_t **pp_ret, int *pi_ret, uint8_t *src, int i_src )
256 {
257     uint8_t *end = &src[i_src];
258     uint8_t *dst = malloc( i_src );
259
260     *pp_ret = dst;
261
262     while( src < end )
263     {
264         if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00  && src[2] == 0x03 )
265         {
266             *dst++ = 0x00;
267             *dst++ = 0x00;
268
269             src += 3;
270             continue;
271         }
272         *dst++ = *src++;
273     }
274
275     *pi_ret = dst - *pp_ret;
276 }
277
278 static inline int bs_read_ue( bs_t *s )
279 {
280     int i = 0;
281
282     while( bs_read1( s ) == 0 && s->p < s->p_end && i < 32 )
283     {
284         i++;
285     }
286     return( ( 1 << i) - 1 + bs_read( s, i ) );
287 }
288 static inline int bs_read_se( bs_t *s ) 
289 {
290     int val = bs_read_ue( s );
291
292     return val&0x01 ? (val+1)/2 : -(val/2);
293 }
294
295
296 static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
297 {
298     decoder_sys_t *p_sys = p_dec->p_sys;
299     block_t *p_pic = NULL;
300
301     const int i_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
302     const int i_nal_type= p_frag->p_buffer[4]&0x1f;
303
304     if( p_sys->b_slice &&
305         ( i_nal_type == NAL_SLICE || i_nal_type == NAL_SLICE_IDR ||
306           i_nal_type == NAL_SLICE_DPC || i_nal_type == NAL_SPS || i_nal_type == NAL_PPS ) )
307     {
308         if( p_sys->b_sps )
309         {
310             p_pic = block_ChainGather( p_sys->p_frame );
311             p_pic->i_dts = p_sys->i_dts;
312             p_pic->i_pts = p_sys->i_pts;
313             p_pic->i_length = 0;    /* FIXME */
314             p_pic->i_flags = p_sys->i_flags;
315         }
316         else
317         {
318             block_ChainRelease( p_sys->p_frame );
319         }
320
321         /* reset context */
322         p_sys->p_frame = NULL;
323         p_sys->b_slice = VLC_FALSE;
324         //p_sys->i_dts += 40000;
325     }
326
327     if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
328     {
329         uint8_t *dec;
330         int     i_dec;
331         bs_t s;
332
333         p_sys->b_slice = VLC_TRUE;
334         p_sys->i_dts   = p_frag->i_dts;
335         p_sys->i_pts   = p_frag->i_pts;
336
337         /* do not convert the whole frame */
338         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5], __MIN( p_frag->i_buffer - 5, 60 ) );
339         bs_init( &s, dec, i_dec );
340
341         /* i_first_mb */
342         bs_read_ue( &s );
343         /* picture type */
344         switch( bs_read_ue( &s ) )
345         {
346             case 0: case 5:
347                 p_sys->i_flags = BLOCK_FLAG_TYPE_P;
348                 break;
349             case 1: case 6:
350                 p_sys->i_flags =BLOCK_FLAG_TYPE_B;
351                 break;
352             case 2: case 7:
353                 p_sys->i_flags = BLOCK_FLAG_TYPE_I;
354                 break;
355             case 3: case 8: /* SP */
356                 p_sys->i_flags = BLOCK_FLAG_TYPE_P;
357                 break;
358             case 4: case 9:
359                 p_sys->i_flags = BLOCK_FLAG_TYPE_I;
360                 break;
361         }
362
363         free( dec );
364     }
365     else if( i_nal_type == NAL_SPS )
366     {
367         uint8_t *dec;
368         int     i_dec;
369         bs_t s;
370         int i_tmp;
371
372         p_sys->b_sps = VLC_TRUE;
373
374         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
375
376         bs_init( &s, dec, i_dec );
377         /* Skip profile(8), constraint_set012, reserver(5), level(8) */
378         bs_skip( &s, 8 + 1+1+1 + 5 + 8 );
379         /* sps id */
380         bs_read_ue( &s );
381         /* Skip i_log2_max_frame_num */
382         bs_read_ue( &s );
383         /* Read poc_type */
384         i_tmp = bs_read_ue( &s );
385         if( i_tmp == 0 )
386         {
387             /* skip i_log2_max_poc_lsb */
388             bs_read_ue( &s );
389         }
390         else if( i_tmp == 1 )
391         {
392             int i_cycle;
393             /* skip b_delta_pic_order_always_zero */
394             bs_skip( &s, 1 );
395             /* skip i_offset_for_non_ref_pic */
396             bs_read_se( &s );
397             /* skip i_offset_for_top_to_bottom_field */
398             bs_read_se( &s );
399             /* read i_num_ref_frames_in_poc_cycle */
400             i_cycle = bs_read_ue( &s );
401             if( i_cycle > 256 ) i_cycle = 256;
402             while( i_cycle > 0 )
403             {
404                 /* skip i_offset_for_ref_frame */
405                 bs_read_se(&s );
406             }
407         }
408         /* i_num_ref_frames */
409         bs_read_ue( &s );
410         /* b_gaps_in_frame_num_value_allowed */
411         bs_skip( &s, 1 );
412
413         /* Read size */
414         p_dec->fmt_out.video.i_width  = 16 * ( bs_read_ue( &s ) + 1 );
415         p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
416
417         /* b_frame_mbs_only */
418         i_tmp = bs_read( &s, 1 );
419         if( i_tmp == 0 )
420         {
421             bs_skip( &s, 1 );
422         }
423         /* b_direct8x8_inference */
424         bs_skip( &s, 1 );
425
426         /* crop ? */
427         i_tmp = bs_read( &s, 1 );
428         if( i_tmp )
429         {
430             /* left */
431             p_dec->fmt_out.video.i_width -= 2 * bs_read_ue( &s );
432             /* right */
433             p_dec->fmt_out.video.i_width -= 2 * bs_read_ue( &s );
434             /* top */
435             p_dec->fmt_out.video.i_height -= 2 * bs_read_ue( &s );
436             /* bottom */
437             p_dec->fmt_out.video.i_height -= 2 * bs_read_ue( &s );
438         }
439
440         /* vui */
441         i_tmp = bs_read( &s, 1 );
442         if( i_tmp )
443         {
444             /* read the aspect ratio part if any FIXME check it */
445             i_tmp = bs_read( &s, 1 );
446             if( i_tmp )
447             {
448                 static const struct { int w, h; } sar[14] =
449                 {
450                     { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
451                     { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
452                     { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
453                     { 64, 33 }, { 160,99 },
454                 };
455                 int i_sar = bs_read( &s, 8 );
456                 int w, h;
457
458                 if( i_sar < 14 )
459                 {
460                     w = sar[i_sar].w;
461                     h = sar[i_sar].h;
462                 }
463                 else
464                 {
465                     w = bs_read( &s, 16 );
466                     h = bs_read( &s, 16 );
467                 }
468                 p_dec->fmt_out.video.i_aspect =
469                     VOUT_ASPECT_FACTOR *
470                     w / h *
471                     p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
472             }
473         }
474
475         free( dec );
476     }
477     else if( i_nal_type == NAL_PPS )
478     {
479         bs_t s;
480         bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
481
482         /* TODO */
483     }
484
485
486     /* Append the block */
487     block_ChainAppend( &p_sys->p_frame, p_frag );
488
489     return p_pic;
490 }
491