]> git.sesse.net Git - vlc/blob - modules/packetizer/h264.c
revert 24090 and add fix for including sps/pps in every keyframe without
[vlc] / modules / packetizer / h264.c
1 /*****************************************************************************
2  * h264.c: h264/avc video packetizer
3  *****************************************************************************
4  * Copyright (C) 2001, 2002, 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          Derk-Jan Hartman <hartman at videolan dot org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #include <vlc/vlc.h>
32 #include <vlc_sout.h>
33 #include <vlc_codec.h>
34 #include <vlc_block.h>
35
36 #include "vlc_block_helper.h"
37 #include "vlc_bits.h"
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
44
45 vlc_module_begin();
46     set_category( CAT_SOUT );
47     set_subcategory( SUBCAT_SOUT_PACKETIZER );
48     set_description( _("H.264 video packetizer") );
49     set_capability( "packetizer", 50 );
50     set_callbacks( Open, Close );
51 vlc_module_end();
52
53
54 /****************************************************************************
55  * Local prototypes
56  ****************************************************************************/
57 static block_t *Packetize( decoder_t *, block_t ** );
58 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
59
60 typedef struct
61 {
62     int i_nal_type;
63     int i_nal_ref_idc;
64
65     int i_frame_type;
66     int i_pic_parameter_set_id;
67     int i_frame_num;
68
69     int i_field_pic_flag;
70     int i_bottom_field_flag;
71
72     int i_idr_pic_id;
73
74     int i_pic_order_cnt_lsb;
75     int i_delta_pic_order_cnt_bottom;
76
77     int i_delta_pic_order_cnt0;
78     int i_delta_pic_order_cnt1;
79 } slice_t;
80
81 struct decoder_sys_t
82 {
83     block_bytestream_t bytestream;
84
85     int     i_state;
86     int     i_offset;
87     uint8_t startcode[4];
88
89     vlc_bool_t b_slice;
90     block_t    *p_frame;
91
92     vlc_bool_t   b_sps;
93     vlc_bool_t   b_pps;
94
95     /* avcC data */
96     int i_avcC_length_size;
97     block_t *p_sps;
98     block_t *p_pps;
99
100     /* Useful values of the Sequence Parameter Set */
101     int i_log2_max_frame_num;
102     int b_frame_mbs_only;
103     int i_pic_order_cnt_type;
104     int i_delta_pic_order_always_zero_flag;
105     int i_log2_max_pic_order_cnt_lsb;
106
107     /* Value from Picture Parameter Set */
108     int i_pic_order_present_flag;
109
110     /* Useful values of the Slice Header */
111     slice_t slice;
112 };
113
114 enum
115 {
116     STATE_NOSYNC,
117     STATE_NEXT_SYNC,
118 };
119
120 enum nal_unit_type_e
121 {
122     NAL_UNKNOWN = 0,
123     NAL_SLICE   = 1,
124     NAL_SLICE_DPA   = 2,
125     NAL_SLICE_DPB   = 3,
126     NAL_SLICE_DPC   = 4,
127     NAL_SLICE_IDR   = 5,    /* ref_idc != 0 */
128     NAL_SEI         = 6,    /* ref_idc == 0 */
129     NAL_SPS         = 7,
130     NAL_PPS         = 8,
131     NAL_AU_DELIMITER= 9
132     /* ref_idc == 0 for 6,9,10,11,12 */
133 };
134
135 enum nal_priority_e
136 {
137     NAL_PRIORITY_DISPOSABLE = 0,
138     NAL_PRIORITY_LOW        = 1,
139     NAL_PRIORITY_HIGH       = 2,
140     NAL_PRIORITY_HIGHEST    = 3,
141 };
142
143 static block_t *ParseNALBlock( decoder_t *, block_t * );
144
145 static block_t *nal_get_annexeb( decoder_t *, uint8_t *p, int );
146
147 /*****************************************************************************
148  * Open: probe the packetizer and return score
149  * When opening after demux, the packetizer is only loaded AFTER the decoder
150  * That means that what you set in fmt_out is ignored by the decoder in this special case
151  *****************************************************************************/
152 static int Open( vlc_object_t *p_this )
153 {
154     decoder_t     *p_dec = (decoder_t*)p_this;
155     decoder_sys_t *p_sys;
156
157     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') &&
158         p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') &&
159         p_dec->fmt_in.i_codec != VLC_FOURCC( 'V', 'S', 'S', 'H') &&
160         p_dec->fmt_in.i_codec != VLC_FOURCC( 'v', 's', 's', 'h') &&
161         p_dec->fmt_in.i_codec != VLC_FOURCC( 'D', 'A', 'V', 'C') &&
162         ( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1') ||
163           p_dec->fmt_in.i_extra < 7 ) )
164     {
165         return VLC_EGENERIC;
166     }
167
168     /* Allocate the memory needed to store the decoder's structure */
169     if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
170     {
171         msg_Err( p_dec, "out of memory" );
172         return VLC_EGENERIC;
173     }
174     p_sys->i_state = STATE_NOSYNC;
175     p_sys->i_offset = 0;
176     p_sys->startcode[0] = 0;
177     p_sys->startcode[1] = 0;
178     p_sys->startcode[2] = 0;
179     p_sys->startcode[3] = 1;
180     p_sys->bytestream = block_BytestreamInit( p_dec );
181     p_sys->b_slice = VLC_FALSE;
182     p_sys->p_frame = NULL;
183     p_sys->b_sps   = VLC_FALSE;
184     p_sys->b_pps   = VLC_FALSE;
185     p_sys->p_sps   = 0;
186     p_sys->p_pps   = 0;
187
188     p_sys->slice.i_nal_type = -1;
189     p_sys->slice.i_nal_ref_idc = -1;
190     p_sys->slice.i_idr_pic_id = -1;
191     p_sys->slice.i_frame_num = -1;
192     p_sys->slice.i_frame_type = 0;
193     p_sys->slice.i_pic_parameter_set_id = -1;
194     p_sys->slice.i_field_pic_flag = 0;
195     p_sys->slice.i_bottom_field_flag = -1;
196     p_sys->slice.i_pic_order_cnt_lsb = -1;
197     p_sys->slice.i_delta_pic_order_cnt_bottom = -1;
198
199     /* Setup properties */
200     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
201     p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
202
203     if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'v', 'c', '1' ) )
204     {
205         /* This type of stream is produced by mp4 and matroska
206          * when we want to store it in another streamformat, you need to convert
207          * The fmt_in.p_extra should ALWAYS contain the avcC
208          * The fmt_out.p_extra should contain all the SPS and PPS with 4 byte startcodes */
209         uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4];
210         int i_sps, i_pps;
211         int i;
212
213         /* Parse avcC */
214         p_sys->i_avcC_length_size = 1 + ((*p++)&0x03);
215
216         /* Read SPS */
217         i_sps = (*p++)&0x1f;
218         for( i = 0; i < i_sps; i++ )
219         {
220             int i_length = GetWBE( p );
221             block_t *p_sps = nal_get_annexeb( p_dec, p + 2, i_length );
222
223             p_sys->p_sps = block_Duplicate( p_sps );
224             p_sps->i_pts = p_sps->i_dts = mdate();
225             ParseNALBlock( p_dec, p_sps );
226             p += 2 + i_length;
227         }
228         /* Read PPS */
229         i_pps = *p++;
230         for( i = 0; i < i_pps; i++ )
231         {
232             int i_length = GetWBE( p );
233             block_t *p_pps = nal_get_annexeb( p_dec, p + 2, i_length );
234
235             p_sys->p_pps = block_Duplicate( p_pps );
236             p_pps->i_pts = p_pps->i_dts = mdate();
237             ParseNALBlock( p_dec, p_pps );
238             p += 2 + i_length;
239         }
240         msg_Dbg( p_dec, "avcC length size=%d, sps=%d, pps=%d",
241                  p_sys->i_avcC_length_size, i_sps, i_pps );
242
243         /* FIXME: FFMPEG isn't happy at all if you leave this */
244         if( p_dec->fmt_out.i_extra ) free( p_dec->fmt_out.p_extra );
245         p_dec->fmt_out.i_extra = 0; p_dec->fmt_out.p_extra = NULL;
246  
247         /* Set the new extradata */
248         p_dec->fmt_out.i_extra = p_sys->p_pps->i_buffer + p_sys->p_sps->i_buffer;
249         p_dec->fmt_out.p_extra = (uint8_t*)malloc( p_dec->fmt_out.i_extra );
250         memcpy( (uint8_t*)p_dec->fmt_out.p_extra, p_sys->p_sps->p_buffer, p_sys->p_sps->i_buffer);
251         memcpy( (uint8_t*)p_dec->fmt_out.p_extra+p_sys->p_sps->i_buffer, p_sys->p_pps->p_buffer, p_sys->p_pps->i_buffer);
252
253         /* Set callback */
254         p_dec->pf_packetize = PacketizeAVC1;
255     }
256     else
257     {
258         /* This type of stream contains data with 3 of 4 byte startcodes
259          * The fmt_in.p_extra MAY contain SPS/PPS with 4 byte startcodes
260          * The fmt_out.p_extra should be the same */
261  
262         /* Set callback */
263         p_dec->pf_packetize = Packetize;
264
265         /* */
266         if( p_dec->fmt_in.i_extra > 0 )
267         {
268             block_t *p_init = block_New( p_dec, p_dec->fmt_in.i_extra );
269             block_t *p_pic;
270
271             memcpy( p_init->p_buffer, p_dec->fmt_in.p_extra,
272                     p_dec->fmt_in.i_extra );
273
274             while( ( p_pic = Packetize( p_dec, &p_init ) ) )
275             {
276                 /* Should not occur because we should only receive SPS/PPS */
277                 block_Release( p_pic );
278             }
279         }
280     }
281
282     return VLC_SUCCESS;
283 }
284
285 /*****************************************************************************
286  * Close: clean up the packetizer
287  *****************************************************************************/
288 static void Close( vlc_object_t *p_this )
289 {
290     decoder_t *p_dec = (decoder_t*)p_this;
291     decoder_sys_t *p_sys = p_dec->p_sys;
292
293     if( p_sys->p_frame ) block_ChainRelease( p_sys->p_frame );
294     if( p_sys->p_sps ) block_Release( p_sys->p_sps );
295     if( p_sys->p_pps ) block_Release( p_sys->p_pps );
296     block_BytestreamRelease( &p_sys->bytestream );
297     free( p_sys );
298 }
299
300 /****************************************************************************
301  * Packetize: the whole thing
302  * Search for the startcodes 3 or more bytes
303  * Feed ParseNALBlock ALWAYS with 4 byte startcode prepended NALs
304  ****************************************************************************/
305 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
306 {
307     decoder_sys_t *p_sys = p_dec->p_sys;
308     block_t       *p_pic;
309
310     if( !pp_block || !*pp_block )
311         return NULL;
312
313     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
314     {
315         if( (*pp_block)->i_flags&BLOCK_FLAG_CORRUPTED )
316         {
317             p_sys->i_state = STATE_NOSYNC;
318             block_BytestreamFlush( &p_sys->bytestream );
319
320             if( p_sys->p_frame )
321                 block_ChainRelease( p_sys->p_frame );
322             p_sys->p_frame = NULL;
323             p_sys->slice.i_frame_type = 0;
324             p_sys->b_slice = VLC_FALSE;
325         }
326         block_Release( *pp_block );
327         return NULL;
328     }
329
330     block_BytestreamPush( &p_sys->bytestream, *pp_block );
331
332     for( ;; )
333     {
334         switch( p_sys->i_state )
335         {
336             case STATE_NOSYNC:
337                 /* Skip until 3 byte startcode 0 0 1 */
338                 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
339                       &p_sys->i_offset, p_sys->startcode+1, 3 ) == VLC_SUCCESS)
340                 {
341                     p_sys->i_state = STATE_NEXT_SYNC;
342                 }
343
344                 if( p_sys->i_offset )
345                 {
346                     /* skip the data */
347                     block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
348                     p_sys->i_offset = 0;
349                     block_BytestreamFlush( &p_sys->bytestream );
350                 }
351
352                 if( p_sys->i_state != STATE_NEXT_SYNC )
353                 {
354                     /* Need more data */
355                     return NULL;
356                 }
357
358                 p_sys->i_offset = 1; /* To find next startcode */
359
360             case STATE_NEXT_SYNC:
361                 /* Find the next 3 byte startcode 0 0 1*/
362                 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
363                       &p_sys->i_offset, p_sys->startcode+1, 3 ) != VLC_SUCCESS)
364                 {
365                     /* Need more data */
366                     return NULL;
367                 }
368
369                 /* Get the new fragment and set the pts/dts */
370                 p_pic = block_New( p_dec, p_sys->i_offset +1 );
371                 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
372                 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
373                 /* Force 4 byte startcode 0 0 0 1 */
374                 p_pic->p_buffer[0] = 0;
375
376                 block_GetBytes( &p_sys->bytestream, &p_pic->p_buffer[1],
377                                 p_pic->i_buffer-1 );
378
379                 /* Remove trailing 0 bytes */
380                 while( p_pic->i_buffer && (!p_pic->p_buffer[p_pic->i_buffer-1] ) )
381                     p_pic->i_buffer--;
382                 p_sys->i_offset = 0;
383
384                 /* Parse the NAL */
385                 if( !( p_pic = ParseNALBlock( p_dec, p_pic ) ) )
386                 {
387                     p_sys->i_state = STATE_NOSYNC;
388                     break;
389                 }
390 #if 0
391                 msg_Dbg( p_dec, "pts="I64Fd" dts="I64Fd,
392                          p_pic->i_pts, p_pic->i_dts );
393 #endif
394
395                 /* So p_block doesn't get re-added several times */
396                 *pp_block = block_BytestreamPop( &p_sys->bytestream );
397
398                 p_sys->i_state = STATE_NOSYNC;
399
400                 return p_pic;
401         }
402     }
403 }
404
405 /****************************************************************************
406  * PacketizeAVC1: Takes VCL blocks of data and creates annexe B type NAL stream
407  * Will always use 4 byte 0 0 0 1 startcodes
408  * Will prepend a SPS and PPS before each keyframe
409  ****************************************************************************/
410 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
411 {
412     decoder_sys_t *p_sys = p_dec->p_sys;
413     block_t       *p_block;
414     block_t       *p_ret = NULL;
415     uint8_t       *p;
416
417     if( !pp_block || !*pp_block )
418         return NULL;
419     if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
420     {
421         block_Release( *pp_block );
422         return NULL;
423     }
424
425     p_block = *pp_block;
426     *pp_block = NULL;
427
428     for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
429     {
430         block_t *p_pic;
431         int i_size = 0;
432         int i;
433
434         for( i = 0; i < p_sys->i_avcC_length_size; i++ )
435         {
436             i_size = (i_size << 8) | (*p++);
437         }
438
439         if( i_size > 0 )
440         {
441             block_t *p_part = nal_get_annexeb( p_dec, p, i_size );
442
443             p_part->i_dts = p_block->i_dts;
444             p_part->i_pts = p_block->i_pts;
445
446             /* Parse the NAL */
447             if( ( p_pic = ParseNALBlock( p_dec, p_part ) ) )
448             {
449                 block_ChainAppend( &p_ret, p_pic );
450             }
451         }
452         p += i_size;
453     }
454     block_Release( p_block );
455
456     return p_ret;
457 }
458
459 static block_t *nal_get_annexeb( decoder_t *p_dec, uint8_t *p, int i_size )
460 {
461     block_t *p_nal;
462
463     p_nal = block_New( p_dec, 4 + i_size );
464
465     /* Add start code */
466     p_nal->p_buffer[0] = 0x00;
467     p_nal->p_buffer[1] = 0x00;
468     p_nal->p_buffer[2] = 0x00;
469     p_nal->p_buffer[3] = 0x01;
470
471     /* Copy nalu */
472     memcpy( &p_nal->p_buffer[4], p, i_size );
473
474     return p_nal;
475 }
476
477 static void nal_get_decoded( uint8_t **pp_ret, int *pi_ret,
478                              uint8_t *src, int i_src )
479 {
480     uint8_t *end = &src[i_src];
481     uint8_t *dst = malloc( i_src );
482
483     *pp_ret = dst;
484
485     while( src < end )
486     {
487         if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 &&
488             src[2] == 0x03 )
489         {
490             *dst++ = 0x00;
491             *dst++ = 0x00;
492
493             src += 3;
494             continue;
495         }
496         *dst++ = *src++;
497     }
498
499     *pi_ret = dst - *pp_ret;
500 }
501
502 static inline int bs_read_ue( bs_t *s )
503 {
504     int i = 0;
505
506     while( bs_read1( s ) == 0 && s->p < s->p_end && i < 32 )
507     {
508         i++;
509     }
510     return( ( 1 << i) - 1 + bs_read( s, i ) );
511 }
512
513 static inline int bs_read_se( bs_t *s )
514 {
515     int val = bs_read_ue( s );
516
517     return val&0x01 ? (val+1)/2 : -(val/2);
518 }
519
520
521 /*****************************************************************************
522  * ParseNALBlock: parses annexB type NALs
523  * All p_frag blocks are required to start with 0 0 0 1 4-byte startcode
524  *****************************************************************************/
525 static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
526 {
527     decoder_sys_t *p_sys = p_dec->p_sys;
528     block_t *p_pic = NULL;
529
530     const int i_nal_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
531     const int i_nal_type = p_frag->p_buffer[4]&0x1f;
532
533 #define OUTPUT \
534     do {                                                      \
535         if( !p_sys->b_header && p_sys->slice.i_frame_type != BLOCK_FLAG_TYPE_I) \
536             break;                                            \
537                                                               \
538         if( p_sys->slice.i_frame_type == BLOCK_FLAG_TYPE_I && p_sys->p_sps && p_sys->p_pps ) \
539         { \
540             block_t *p_sps = block_Duplicate( p_sys->p_sps ); \
541             block_t *p_pps = block_Duplicate( p_sys->p_pps ); \
542             p_sps->i_dts = p_sys->p_frame->i_dts;           \
543             p_sps->i_pts = p_sys->p_frame->i_pts;           \
544             block_ChainAppend( &p_sps, p_pps );               \
545             block_ChainAppend( &p_sps, p_sys->p_frame );      \
546             p_pic = block_ChainGather( p_sps );               \
547         } else { \
548             p_pic = block_ChainGather( p_sys->p_frame ); \
549         } \
550         p_pic->i_length = 0;    /* FIXME */                   \
551         p_pic->i_flags |= p_sys->slice.i_frame_type;          \
552             \
553         p_sys->slice.i_frame_type = 0;                        \
554         p_sys->p_frame = NULL;                                \
555         p_sys->b_slice = VLC_FALSE;                           \
556     } while(0)
557
558     if( p_sys->b_slice && ( !p_sys->b_sps || !p_sys->b_pps ) )
559     {
560         block_ChainRelease( p_sys->p_frame );
561         msg_Warn( p_dec, "waiting for SPS/PPS" );
562
563         /* Reset context */
564         p_sys->slice.i_frame_type = 0;
565         p_sys->p_frame = NULL;
566         p_sys->b_slice = VLC_FALSE;
567     }
568
569     if( ( !p_sys->b_sps || !p_sys->b_pps ) &&
570         i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
571     {
572         p_sys->b_slice = VLC_TRUE;
573         /* Fragment will be discarded later on */
574     }
575     else if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
576     {
577         uint8_t *dec;
578         int i_dec, i_first_mb, i_slice_type;
579         slice_t slice;
580         vlc_bool_t b_pic;
581         bs_t s;
582
583         /* do not convert the whole frame */
584         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5],
585                          __MIN( p_frag->i_buffer - 5, 60 ) );
586         bs_init( &s, dec, i_dec );
587
588         /* first_mb_in_slice */
589         i_first_mb = bs_read_ue( &s );
590
591         /* slice_type */
592         switch( (i_slice_type = bs_read_ue( &s )) )
593         {
594         case 0: case 5:
595             slice.i_frame_type = BLOCK_FLAG_TYPE_P;
596             break;
597         case 1: case 6:
598             slice.i_frame_type = BLOCK_FLAG_TYPE_B;
599             break;
600         case 2: case 7:
601             slice.i_frame_type = BLOCK_FLAG_TYPE_I;
602             break;
603         case 3: case 8: /* SP */
604             slice.i_frame_type = BLOCK_FLAG_TYPE_P;
605             break;
606         case 4: case 9:
607             slice.i_frame_type = BLOCK_FLAG_TYPE_I;
608             break;
609         default:
610             slice.i_frame_type = 0;
611             break;
612         }
613
614         /* */
615         slice.i_nal_type = i_nal_type;
616         slice.i_nal_ref_idc = i_nal_ref_idc;
617
618         slice.i_pic_parameter_set_id = bs_read_ue( &s );
619         slice.i_frame_num = bs_read( &s, p_sys->i_log2_max_frame_num + 4 );
620
621         slice.i_field_pic_flag = 0;
622         slice.i_bottom_field_flag = -1;
623         if( !p_sys->b_frame_mbs_only )
624         {
625             /* field_pic_flag */
626             slice.i_field_pic_flag = bs_read( &s, 1 );
627             if( slice.i_field_pic_flag )
628                 slice.i_bottom_field_flag = bs_read( &s, 1 );
629         }
630
631         slice.i_idr_pic_id = p_sys->slice.i_idr_pic_id;
632         if( slice.i_nal_type == NAL_SLICE_IDR )
633             slice.i_idr_pic_id = bs_read_ue( &s );
634
635         slice.i_pic_order_cnt_lsb = -1;
636         slice.i_delta_pic_order_cnt_bottom = -1;
637         slice.i_delta_pic_order_cnt0 = 0;
638         slice.i_delta_pic_order_cnt1 = 0;
639         if( p_sys->i_pic_order_cnt_type == 0 )
640         {
641             slice.i_pic_order_cnt_lsb = bs_read( &s, p_sys->i_log2_max_pic_order_cnt_lsb + 4 );
642             if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
643                 slice.i_delta_pic_order_cnt_bottom = bs_read_se( &s );
644         }
645         else if( p_sys->i_pic_order_cnt_type == 1 && !p_sys->i_delta_pic_order_always_zero_flag )
646         {
647             slice.i_delta_pic_order_cnt0 = bs_read_se( &s );
648             if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
649                 slice.i_delta_pic_order_cnt1 = bs_read_se( &s );
650         }
651
652         /* Detection of the first VCL NAL unit of a primary coded picture
653          * (cf. 7.4.1.2.4) */
654         b_pic = VLC_FALSE;
655         if( slice.i_frame_num != p_sys->slice.i_frame_num ||
656             slice.i_pic_parameter_set_id != p_sys->slice.i_pic_parameter_set_id ||
657             slice.i_field_pic_flag != p_sys->slice.i_field_pic_flag ||
658             slice.i_nal_ref_idc != p_sys->slice.i_nal_ref_idc )
659             b_pic = VLC_TRUE;
660         if( slice.i_bottom_field_flag != -1 && p_sys->slice.i_bottom_field_flag != -1 && slice.i_bottom_field_flag != p_sys->slice.i_bottom_field_flag )
661             b_pic = VLC_TRUE;
662         if( p_sys->i_pic_order_cnt_type == 0 &&
663             ( slice.i_pic_order_cnt_lsb != p_sys->slice.i_pic_order_cnt_lsb ||
664               slice.i_delta_pic_order_cnt_bottom != p_sys->slice.i_delta_pic_order_cnt_bottom ) )
665             b_pic = VLC_TRUE;
666         else if( p_sys->i_pic_order_cnt_type == 1 &&
667                  ( slice.i_delta_pic_order_cnt0 != p_sys->slice.i_delta_pic_order_cnt0 ||
668                    slice.i_delta_pic_order_cnt1 != p_sys->slice.i_delta_pic_order_cnt1 ) )
669             b_pic = VLC_TRUE;
670         if( ( slice.i_nal_type == NAL_SLICE_IDR || p_sys->slice.i_nal_type == NAL_SLICE_IDR ) &&
671             ( slice.i_nal_type != p_sys->slice.i_nal_type || slice.i_idr_pic_id != p_sys->slice.i_idr_pic_id ) )
672                 b_pic = VLC_TRUE;
673
674         /* */
675         p_sys->slice = slice;
676
677         if( b_pic && p_sys->b_slice )
678             OUTPUT;
679
680         p_sys->b_slice = VLC_TRUE;
681
682         free( dec );
683     }
684     else if( i_nal_type == NAL_SPS )
685     {
686         uint8_t *dec;
687         int     i_dec;
688         bs_t s;
689         int i_tmp;
690
691         if( !p_sys->b_sps ) msg_Dbg( p_dec, "found NAL_SPS" );
692
693         p_sys->b_sps = VLC_TRUE;
694
695         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5],
696                          p_frag->i_buffer - 5 );
697
698         bs_init( &s, dec, i_dec );
699         /* Skip profile(8), constraint_set012, reserver(5), level(8) */
700         bs_skip( &s, 8 + 1+1+1 + 5 + 8 );
701         /* sps id */
702         bs_read_ue( &s );
703         /* Skip i_log2_max_frame_num */
704         p_sys->i_log2_max_frame_num = bs_read_ue( &s );
705         if( p_sys->i_log2_max_frame_num > 12)
706             p_sys->i_log2_max_frame_num = 12;
707         /* Read poc_type */
708         p_sys->i_pic_order_cnt_type = bs_read_ue( &s );
709         if( p_sys->i_pic_order_cnt_type == 0 )
710         {
711             /* skip i_log2_max_poc_lsb */
712             p_sys->i_log2_max_pic_order_cnt_lsb = bs_read_ue( &s );
713             if( p_sys->i_log2_max_pic_order_cnt_lsb > 12 )
714                 p_sys->i_log2_max_pic_order_cnt_lsb = 12;
715         }
716         else if( p_sys->i_pic_order_cnt_type == 1 )
717         {
718             int i_cycle;
719             /* skip b_delta_pic_order_always_zero */
720             p_sys->i_delta_pic_order_always_zero_flag = bs_read( &s, 1 );
721             /* skip i_offset_for_non_ref_pic */
722             bs_read_se( &s );
723             /* skip i_offset_for_top_to_bottom_field */
724             bs_read_se( &s );
725             /* read i_num_ref_frames_in_poc_cycle */
726             i_cycle = bs_read_ue( &s );
727             if( i_cycle > 256 ) i_cycle = 256;
728             while( i_cycle > 0 )
729             {
730                 /* skip i_offset_for_ref_frame */
731                 bs_read_se(&s );
732             }
733         }
734         /* i_num_ref_frames */
735         bs_read_ue( &s );
736         /* b_gaps_in_frame_num_value_allowed */
737         bs_skip( &s, 1 );
738
739         /* Read size */
740         p_dec->fmt_out.video.i_width  = 16 * ( bs_read_ue( &s ) + 1 );
741         p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
742
743         /* b_frame_mbs_only */
744         p_sys->b_frame_mbs_only = bs_read( &s, 1 );
745         if( p_sys->b_frame_mbs_only == 0 )
746         {
747             bs_skip( &s, 1 );
748         }
749         /* b_direct8x8_inference */
750         bs_skip( &s, 1 );
751
752         /* crop */
753         i_tmp = bs_read( &s, 1 );
754         if( i_tmp )
755         {
756             /* left */
757             bs_read_ue( &s );
758             /* right */
759             bs_read_ue( &s );
760             /* top */
761             bs_read_ue( &s );
762             /* bottom */
763             bs_read_ue( &s );
764         }
765
766         /* vui */
767         i_tmp = bs_read( &s, 1 );
768         if( i_tmp )
769         {
770             /* read the aspect ratio part if any FIXME check it */
771             i_tmp = bs_read( &s, 1 );
772             if( i_tmp )
773             {
774                 static const struct { int w, h; } sar[14] =
775                 {
776                     { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
777                     { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
778                     { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
779                     { 64, 33 }, { 160,99 },
780                 };
781                 int i_sar = bs_read( &s, 8 );
782                 int w, h;
783
784                 if( i_sar < 14 )
785                 {
786                     w = sar[i_sar].w;
787                     h = sar[i_sar].h;
788                 }
789                 else
790                 {
791                     w = bs_read( &s, 16 );
792                     h = bs_read( &s, 16 );
793                 }
794                 if( h != 0 )
795                     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * w /
796                         h * p_dec->fmt_out.video.i_width /
797                         p_dec->fmt_out.video.i_height;
798                 else
799                     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR;
800             }
801         }
802
803         free( dec );
804
805         if( p_sys->b_slice ) OUTPUT;
806
807         /* We have a new SPS */
808         if( p_sys->p_sps ) block_Release( p_sys->p_sps );
809         p_sys->p_sps = p_frag;
810
811         /* Do not append the SPS because we will insert it on keyframes */
812         return p_pic;
813     }
814     else if( i_nal_type == NAL_PPS )
815     {
816         bs_t s;
817
818         bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
819         bs_read_ue( &s ); // pps id
820         bs_read_ue( &s ); // sps id
821         bs_skip( &s, 1 ); // entropy coding mode flag
822         p_sys->i_pic_order_present_flag = bs_read( &s, 1 );
823
824         if( !p_sys->b_pps ) msg_Dbg( p_dec, "found NAL_PPS" );
825         p_sys->b_pps = VLC_TRUE;
826
827         /* TODO */
828
829         if( p_sys->b_slice ) OUTPUT;
830
831         /* We have a new PPS */
832         if( p_sys->p_pps ) block_Release( p_sys->p_pps );
833         p_sys->p_pps = p_frag;
834
835         /* Do not append the PPS because we will insert it on keyframes */
836         return p_pic;
837     }
838     else if( i_nal_type == NAL_AU_DELIMITER ||
839              i_nal_type == NAL_SEI ||
840              ( i_nal_type >= 13 && i_nal_type <= 18 ) )
841     {
842         if( p_sys->b_slice ) OUTPUT;
843     }
844
845 #undef OUTPUT
846
847     /* Append the block */
848     block_ChainAppend( &p_sys->p_frame, p_frag );
849
850     return p_pic;
851 }