]> git.sesse.net Git - vlc/blob - modules/packetizer/h264.c
remove b_header in packetizer so sps/pps are send with every keyframe,
[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->slice.i_frame_type == BLOCK_FLAG_TYPE_I && p_sys->p_sps && p_sys->p_pps ) \
536         { \
537             block_t *p_sps = block_Duplicate( p_sys->p_sps ); \
538             block_t *p_pps = block_Duplicate( p_sys->p_pps ); \
539             p_sps->i_dts = p_sys->p_frame->i_dts;           \
540             p_sps->i_pts = p_sys->p_frame->i_pts;           \
541             block_ChainAppend( &p_sps, p_pps );               \
542             block_ChainAppend( &p_sps, p_sys->p_frame );      \
543             p_pic = block_ChainGather( p_sps );               \
544         } else { \
545             p_pic = block_ChainGather( p_sys->p_frame ); \
546         } \
547         p_pic->i_length = 0;    /* FIXME */                   \
548         p_pic->i_flags |= p_sys->slice.i_frame_type;          \
549             \
550         p_sys->slice.i_frame_type = 0;                        \
551         p_sys->p_frame = NULL;                                \
552         p_sys->b_slice = VLC_FALSE;                           \
553     } while(0)
554
555     if( p_sys->b_slice && ( !p_sys->b_sps || !p_sys->b_pps ) )
556     {
557         block_ChainRelease( p_sys->p_frame );
558         msg_Warn( p_dec, "waiting for SPS/PPS" );
559
560         /* Reset context */
561         p_sys->slice.i_frame_type = 0;
562         p_sys->p_frame = NULL;
563         p_sys->b_slice = VLC_FALSE;
564     }
565
566     if( ( !p_sys->b_sps || !p_sys->b_pps ) &&
567         i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
568     {
569         p_sys->b_slice = VLC_TRUE;
570         /* Fragment will be discarded later on */
571     }
572     else if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
573     {
574         uint8_t *dec;
575         int i_dec, i_first_mb, i_slice_type;
576         slice_t slice;
577         vlc_bool_t b_pic;
578         bs_t s;
579
580         /* do not convert the whole frame */
581         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5],
582                          __MIN( p_frag->i_buffer - 5, 60 ) );
583         bs_init( &s, dec, i_dec );
584
585         /* first_mb_in_slice */
586         i_first_mb = bs_read_ue( &s );
587
588         /* slice_type */
589         switch( (i_slice_type = bs_read_ue( &s )) )
590         {
591         case 0: case 5:
592             slice.i_frame_type = BLOCK_FLAG_TYPE_P;
593             break;
594         case 1: case 6:
595             slice.i_frame_type = BLOCK_FLAG_TYPE_B;
596             break;
597         case 2: case 7:
598             slice.i_frame_type = BLOCK_FLAG_TYPE_I;
599             break;
600         case 3: case 8: /* SP */
601             slice.i_frame_type = BLOCK_FLAG_TYPE_P;
602             break;
603         case 4: case 9:
604             slice.i_frame_type = BLOCK_FLAG_TYPE_I;
605             break;
606         default:
607             slice.i_frame_type = 0;
608             break;
609         }
610
611         /* */
612         slice.i_nal_type = i_nal_type;
613         slice.i_nal_ref_idc = i_nal_ref_idc;
614
615         slice.i_pic_parameter_set_id = bs_read_ue( &s );
616         slice.i_frame_num = bs_read( &s, p_sys->i_log2_max_frame_num + 4 );
617
618         slice.i_field_pic_flag = 0;
619         slice.i_bottom_field_flag = -1;
620         if( !p_sys->b_frame_mbs_only )
621         {
622             /* field_pic_flag */
623             slice.i_field_pic_flag = bs_read( &s, 1 );
624             if( slice.i_field_pic_flag )
625                 slice.i_bottom_field_flag = bs_read( &s, 1 );
626         }
627
628         slice.i_idr_pic_id = p_sys->slice.i_idr_pic_id;
629         if( slice.i_nal_type == NAL_SLICE_IDR )
630             slice.i_idr_pic_id = bs_read_ue( &s );
631
632         slice.i_pic_order_cnt_lsb = -1;
633         slice.i_delta_pic_order_cnt_bottom = -1;
634         slice.i_delta_pic_order_cnt0 = 0;
635         slice.i_delta_pic_order_cnt1 = 0;
636         if( p_sys->i_pic_order_cnt_type == 0 )
637         {
638             slice.i_pic_order_cnt_lsb = bs_read( &s, p_sys->i_log2_max_pic_order_cnt_lsb + 4 );
639             if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
640                 slice.i_delta_pic_order_cnt_bottom = bs_read_se( &s );
641         }
642         else if( p_sys->i_pic_order_cnt_type == 1 && !p_sys->i_delta_pic_order_always_zero_flag )
643         {
644             slice.i_delta_pic_order_cnt0 = bs_read_se( &s );
645             if( p_sys->i_pic_order_present_flag && !slice.i_field_pic_flag )
646                 slice.i_delta_pic_order_cnt1 = bs_read_se( &s );
647         }
648
649         /* Detection of the first VCL NAL unit of a primary coded picture
650          * (cf. 7.4.1.2.4) */
651         b_pic = VLC_FALSE;
652         if( slice.i_frame_num != p_sys->slice.i_frame_num ||
653             slice.i_pic_parameter_set_id != p_sys->slice.i_pic_parameter_set_id ||
654             slice.i_field_pic_flag != p_sys->slice.i_field_pic_flag ||
655             slice.i_nal_ref_idc != p_sys->slice.i_nal_ref_idc )
656             b_pic = VLC_TRUE;
657         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 )
658             b_pic = VLC_TRUE;
659         if( p_sys->i_pic_order_cnt_type == 0 &&
660             ( slice.i_pic_order_cnt_lsb != p_sys->slice.i_pic_order_cnt_lsb ||
661               slice.i_delta_pic_order_cnt_bottom != p_sys->slice.i_delta_pic_order_cnt_bottom ) )
662             b_pic = VLC_TRUE;
663         else if( p_sys->i_pic_order_cnt_type == 1 &&
664                  ( slice.i_delta_pic_order_cnt0 != p_sys->slice.i_delta_pic_order_cnt0 ||
665                    slice.i_delta_pic_order_cnt1 != p_sys->slice.i_delta_pic_order_cnt1 ) )
666             b_pic = VLC_TRUE;
667         if( ( slice.i_nal_type == NAL_SLICE_IDR || p_sys->slice.i_nal_type == NAL_SLICE_IDR ) &&
668             ( slice.i_nal_type != p_sys->slice.i_nal_type || slice.i_idr_pic_id != p_sys->slice.i_idr_pic_id ) )
669                 b_pic = VLC_TRUE;
670
671         /* */
672         p_sys->slice = slice;
673
674         if( b_pic && p_sys->b_slice )
675             OUTPUT;
676
677         p_sys->b_slice = VLC_TRUE;
678
679         free( dec );
680     }
681     else if( i_nal_type == NAL_SPS )
682     {
683         uint8_t *dec;
684         int     i_dec;
685         bs_t s;
686         int i_tmp;
687
688         if( !p_sys->b_sps ) msg_Dbg( p_dec, "found NAL_SPS" );
689
690         p_sys->b_sps = VLC_TRUE;
691
692         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5],
693                          p_frag->i_buffer - 5 );
694
695         bs_init( &s, dec, i_dec );
696         /* Skip profile(8), constraint_set012, reserver(5), level(8) */
697         bs_skip( &s, 8 + 1+1+1 + 5 + 8 );
698         /* sps id */
699         bs_read_ue( &s );
700         /* Skip i_log2_max_frame_num */
701         p_sys->i_log2_max_frame_num = bs_read_ue( &s );
702         if( p_sys->i_log2_max_frame_num > 12)
703             p_sys->i_log2_max_frame_num = 12;
704         /* Read poc_type */
705         p_sys->i_pic_order_cnt_type = bs_read_ue( &s );
706         if( p_sys->i_pic_order_cnt_type == 0 )
707         {
708             /* skip i_log2_max_poc_lsb */
709             p_sys->i_log2_max_pic_order_cnt_lsb = bs_read_ue( &s );
710             if( p_sys->i_log2_max_pic_order_cnt_lsb > 12 )
711                 p_sys->i_log2_max_pic_order_cnt_lsb = 12;
712         }
713         else if( p_sys->i_pic_order_cnt_type == 1 )
714         {
715             int i_cycle;
716             /* skip b_delta_pic_order_always_zero */
717             p_sys->i_delta_pic_order_always_zero_flag = bs_read( &s, 1 );
718             /* skip i_offset_for_non_ref_pic */
719             bs_read_se( &s );
720             /* skip i_offset_for_top_to_bottom_field */
721             bs_read_se( &s );
722             /* read i_num_ref_frames_in_poc_cycle */
723             i_cycle = bs_read_ue( &s );
724             if( i_cycle > 256 ) i_cycle = 256;
725             while( i_cycle > 0 )
726             {
727                 /* skip i_offset_for_ref_frame */
728                 bs_read_se(&s );
729             }
730         }
731         /* i_num_ref_frames */
732         bs_read_ue( &s );
733         /* b_gaps_in_frame_num_value_allowed */
734         bs_skip( &s, 1 );
735
736         /* Read size */
737         p_dec->fmt_out.video.i_width  = 16 * ( bs_read_ue( &s ) + 1 );
738         p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
739
740         /* b_frame_mbs_only */
741         p_sys->b_frame_mbs_only = bs_read( &s, 1 );
742         if( p_sys->b_frame_mbs_only == 0 )
743         {
744             bs_skip( &s, 1 );
745         }
746         /* b_direct8x8_inference */
747         bs_skip( &s, 1 );
748
749         /* crop */
750         i_tmp = bs_read( &s, 1 );
751         if( i_tmp )
752         {
753             /* left */
754             bs_read_ue( &s );
755             /* right */
756             bs_read_ue( &s );
757             /* top */
758             bs_read_ue( &s );
759             /* bottom */
760             bs_read_ue( &s );
761         }
762
763         /* vui */
764         i_tmp = bs_read( &s, 1 );
765         if( i_tmp )
766         {
767             /* read the aspect ratio part if any FIXME check it */
768             i_tmp = bs_read( &s, 1 );
769             if( i_tmp )
770             {
771                 static const struct { int w, h; } sar[14] =
772                 {
773                     { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
774                     { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
775                     { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
776                     { 64, 33 }, { 160,99 },
777                 };
778                 int i_sar = bs_read( &s, 8 );
779                 int w, h;
780
781                 if( i_sar < 14 )
782                 {
783                     w = sar[i_sar].w;
784                     h = sar[i_sar].h;
785                 }
786                 else
787                 {
788                     w = bs_read( &s, 16 );
789                     h = bs_read( &s, 16 );
790                 }
791                 if( h != 0 )
792                     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * w /
793                         h * p_dec->fmt_out.video.i_width /
794                         p_dec->fmt_out.video.i_height;
795                 else
796                     p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR;
797             }
798         }
799
800         free( dec );
801
802         if( p_sys->b_slice ) OUTPUT;
803
804         /* We have a new SPS */
805         if( p_sys->p_sps ) block_Release( p_sys->p_sps );
806         p_sys->p_sps = p_frag;
807
808         /* Do not append the SPS because we will insert it on keyframes */
809         return p_pic;
810     }
811     else if( i_nal_type == NAL_PPS )
812     {
813         bs_t s;
814
815         bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
816         bs_read_ue( &s ); // pps id
817         bs_read_ue( &s ); // sps id
818         bs_skip( &s, 1 ); // entropy coding mode flag
819         p_sys->i_pic_order_present_flag = bs_read( &s, 1 );
820
821         if( !p_sys->b_pps ) msg_Dbg( p_dec, "found NAL_PPS" );
822         p_sys->b_pps = VLC_TRUE;
823
824         /* TODO */
825
826         if( p_sys->b_slice ) OUTPUT;
827
828         /* We have a new PPS */
829         if( p_sys->p_pps ) block_Release( p_sys->p_pps );
830         p_sys->p_pps = p_frag;
831
832         /* Do not append the PPS because we will insert it on keyframes */
833         return p_pic;
834     }
835     else if( i_nal_type == NAL_AU_DELIMITER ||
836              i_nal_type == NAL_SEI ||
837              ( i_nal_type >= 13 && i_nal_type <= 18 ) )
838     {
839         if( p_sys->b_slice ) OUTPUT;
840     }
841
842 #undef OUTPUT
843
844     /* Append the block */
845     block_ChainAppend( &p_sys->p_frame, p_frag );
846
847     return p_pic;
848 }