mkv.cpp: I was wrong, the dirent export works
[vlc.git] / 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@videolan.org>
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_category( CAT_SOUT );
46     set_subcategory( SUBCAT_SOUT_PACKETIZER );
47     set_description( _("H264 video packetizer") );
48     set_capability( "packetizer", 50 );
49     set_callbacks( Open, Close );
50 vlc_module_end();
51
52
53 /****************************************************************************
54  * Local prototypes
55  ****************************************************************************/
56 static block_t *Packetize( decoder_t *, block_t ** );
57 static block_t *PacketizeAVC1( decoder_t *, block_t ** );
58
59 struct decoder_sys_t
60 {
61     block_bytestream_t bytestream;
62
63     int     i_state;
64     int     i_offset;
65     uint8_t startcode[4];
66
67     vlc_bool_t b_slice;
68     block_t    *p_frame;
69
70     int64_t      i_dts;
71     int64_t      i_pts;
72     unsigned int i_flags;
73
74     vlc_bool_t   b_sps;
75
76     /* avcC data */
77     int i_avcC_length_size;
78
79     /* Useful values of the Sequence Parameter Set */
80     int i_log2_max_frame_num;
81     int b_frame_mbs_only;
82
83     /* Useful values of the Slice Header */
84     int i_nal_type;
85     int i_nal_ref_idc;
86     int i_idr_pic_id;
87     int i_frame_num;
88 };
89
90 enum
91 {
92     STATE_NOSYNC,
93     STATE_NEXT_SYNC,
94 };
95
96 enum nal_unit_type_e
97 {
98     NAL_UNKNOWN = 0,
99     NAL_SLICE   = 1,
100     NAL_SLICE_DPA   = 2,
101     NAL_SLICE_DPB   = 3,
102     NAL_SLICE_DPC   = 4,
103     NAL_SLICE_IDR   = 5,    /* ref_idc != 0 */
104     NAL_SEI         = 6,    /* ref_idc == 0 */
105     NAL_SPS         = 7,
106     NAL_PPS         = 8
107     /* ref_idc == 0 for 6,9,10,11,12 */
108 };
109
110 enum nal_priority_e
111 {
112     NAL_PRIORITY_DISPOSABLE = 0,
113     NAL_PRIORITY_LOW        = 1,
114     NAL_PRIORITY_HIGH       = 2,
115     NAL_PRIORITY_HIGHEST    = 3,
116 };
117
118 static block_t *ParseNALBlock( decoder_t *, block_t * );
119
120 static block_t *nal_get_annexeb( decoder_t *, uint8_t *p, int );
121
122 /*****************************************************************************
123  * Open: probe the packetizer and return score
124  *****************************************************************************/
125 static int Open( vlc_object_t *p_this )
126 {
127     decoder_t     *p_dec = (decoder_t*)p_this;
128     decoder_sys_t *p_sys;
129
130     if( p_dec->fmt_in.i_codec != VLC_FOURCC( 'h', '2', '6', '4') &&
131         p_dec->fmt_in.i_codec != VLC_FOURCC( 'H', '2', '6', '4') &&
132         p_dec->fmt_in.i_codec != VLC_FOURCC( 'V', 'S', 'S', 'H') &&
133         p_dec->fmt_in.i_codec != VLC_FOURCC( 'v', 's', 's', 'h') &&
134         ( p_dec->fmt_in.i_codec != VLC_FOURCC( 'a', 'v', 'c', '1') ||
135           p_dec->fmt_in.i_extra < 7 ) )
136     {
137         return VLC_EGENERIC;
138     }
139
140     /* Allocate the memory needed to store the decoder's structure */
141     if( ( p_dec->p_sys = p_sys = malloc( sizeof(decoder_sys_t) ) ) == NULL )
142     {
143         msg_Err( p_dec, "out of memory" );
144         return VLC_EGENERIC;
145     }
146     p_sys->i_state = STATE_NOSYNC;
147     p_sys->i_offset = 0;
148     p_sys->startcode[0] = 0;
149     p_sys->startcode[1] = 0;
150     p_sys->startcode[2] = 0;
151     p_sys->startcode[3] = 1;
152     p_sys->bytestream = block_BytestreamInit( p_dec );
153     p_sys->b_slice = VLC_FALSE;
154     p_sys->p_frame = NULL;
155     p_sys->i_dts   = 0;
156     p_sys->i_pts   = 0;
157     p_sys->i_flags = 0;
158     p_sys->b_sps   = VLC_FALSE;
159
160     p_sys->i_nal_type = -1;
161     p_sys->i_nal_ref_idc = -1;
162     p_sys->i_idr_pic_id = -1;
163     p_sys->i_frame_num = -1;
164
165     /* Setup properties */
166     es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
167     p_dec->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
168     /* FIXME: FFMPEG isn't happy at all if you leave this */
169     if( p_dec->fmt_out.i_extra ) free( p_dec->fmt_out.p_extra );
170     p_dec->fmt_out.i_extra = 0; p_dec->fmt_out.p_extra = 0;
171
172     if( p_dec->fmt_in.i_codec == VLC_FOURCC( 'a', 'v', 'c', '1' ) )
173     {
174         uint8_t *p = &((uint8_t*)p_dec->fmt_in.p_extra)[4];
175         int i_sps, i_pps;
176         int i;
177
178         /* Parse avcC */
179         p_sys->i_avcC_length_size = 1 + ((*p++)&0x03);
180
181         /* Read SPS */
182         i_sps = (*p++)&0x1f;
183
184         for( i = 0; i < i_sps; i++ )
185         {
186             int i_length = GetWBE( p );
187             block_t *p_sps = nal_get_annexeb( p_dec, p + 2, i_length );
188
189             ParseNALBlock( p_dec, p_sps );
190             p += 2 + i_length;
191         }
192         /* Read PPS */
193         i_pps = *p++;
194         for( i = 0; i < i_pps; i++ )
195         {
196             int i_length = GetWBE( p );
197             block_t *p_pps = nal_get_annexeb( p_dec, p + 2, i_length );
198
199             ParseNALBlock( p_dec, p_pps );
200             p += 2 + i_length;
201         }
202         msg_Dbg( p_dec, "avcC length size=%d sps=%d pps=%d",
203                  p_sys->i_avcC_length_size, i_sps, i_pps );
204
205         /* Set callback */
206         p_dec->pf_packetize = PacketizeAVC1;
207     }
208     else
209     {
210         /* Set callback */
211         p_dec->pf_packetize = Packetize;
212     }
213
214     return VLC_SUCCESS;
215 }
216
217 /*****************************************************************************
218  * Close: clean up the packetizer
219  *****************************************************************************/
220 static void Close( vlc_object_t *p_this )
221 {
222     decoder_t *p_dec = (decoder_t*)p_this;
223     decoder_sys_t *p_sys = p_dec->p_sys;
224
225     block_BytestreamRelease( &p_sys->bytestream );
226     free( p_sys );
227 }
228
229 /****************************************************************************
230  * Packetize: the whole thing
231  ****************************************************************************/
232 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
233 {
234     decoder_sys_t *p_sys = p_dec->p_sys;
235     block_t       *p_pic;
236
237     if( !pp_block || !*pp_block ) return NULL;
238
239     block_BytestreamPush( &p_sys->bytestream, *pp_block );
240
241     for( ;; )
242     {
243         switch( p_sys->i_state )
244         {
245             case STATE_NOSYNC:
246                 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
247                       &p_sys->i_offset, p_sys->startcode+1, 3 ) == VLC_SUCCESS)
248                 {
249                     p_sys->i_state = STATE_NEXT_SYNC;
250                 }
251
252                 if( p_sys->i_offset )
253                 {
254                     block_SkipBytes( &p_sys->bytestream, p_sys->i_offset );
255                     p_sys->i_offset = 0;
256                     block_BytestreamFlush( &p_sys->bytestream );
257                 }
258
259                 if( p_sys->i_state != STATE_NEXT_SYNC )
260                 {
261                     /* Need more data */
262                     return NULL;
263                 }
264
265                 p_sys->i_offset = 1; /* To find next startcode */
266
267             case STATE_NEXT_SYNC:
268                 /* Find the next startcode */
269                 if( block_FindStartcodeFromOffset( &p_sys->bytestream,
270                       &p_sys->i_offset, p_sys->startcode, 3 ) != VLC_SUCCESS)
271                 {
272                     if( block_FindStartcodeFromOffset( &p_sys->bytestream,
273                           &p_sys->i_offset, p_sys->startcode+1, 3 ) !=
274                         VLC_SUCCESS )
275                     {
276                         /* Need more data */
277                         return NULL;
278                     }
279                 }
280
281                 /* Get the new fragment and set the pts/dts */
282                 p_pic = block_New( p_dec, p_sys->i_offset );
283                 p_pic->i_pts = p_sys->bytestream.p_block->i_pts;
284                 p_pic->i_dts = p_sys->bytestream.p_block->i_dts;
285
286                 block_GetBytes( &p_sys->bytestream, p_pic->p_buffer,
287                                 p_pic->i_buffer );
288
289                 p_sys->i_offset = 0;
290
291                 /* Parse the NAL */
292                 if( !( p_pic = ParseNALBlock( p_dec, p_pic ) ) )
293                 {
294                     p_sys->i_state = STATE_NOSYNC;
295                     break;
296                 }
297
298                 /* So p_block doesn't get re-added several times */
299                 *pp_block = block_BytestreamPop( &p_sys->bytestream );
300
301                 p_sys->i_state = STATE_NOSYNC;
302
303                 return p_pic;
304         }
305     }
306 }
307
308 /****************************************************************************
309  * PacketizeAVC1: the whole thing
310  ****************************************************************************/
311 static block_t *PacketizeAVC1( decoder_t *p_dec, block_t **pp_block )
312 {
313     decoder_sys_t *p_sys = p_dec->p_sys;
314     block_t       *p_block;
315     block_t       *p_ret = NULL;
316     uint8_t       *p;
317
318     if( !pp_block || !*pp_block ) return NULL;
319
320     p_block = *pp_block;
321     *pp_block = NULL;
322
323     for( p = p_block->p_buffer; p < &p_block->p_buffer[p_block->i_buffer]; )
324     {
325         block_t *p_pic;
326         int i_size = 0;
327         int i;
328
329         for( i = 0; i < p_sys->i_avcC_length_size; i++ )
330         {
331             i_size = (i_size << 8) | (*p++);
332         }
333
334         if( i_size > 0 )
335         {
336             block_t *p_part = nal_get_annexeb( p_dec, p, i_size );
337
338             p_part->i_dts = p_block->i_dts;
339             p_part->i_pts = p_block->i_pts;
340
341             /* Parse the NAL */
342             if( ( p_pic = ParseNALBlock( p_dec, p_part ) ) )
343             {
344                 block_ChainAppend( &p_ret, p_pic );
345             }
346         }
347         p += i_size;
348     }
349
350     return p_ret;
351 }
352
353 static block_t *nal_get_annexeb( decoder_t *p_dec, uint8_t *p, int i_size )
354 {
355     block_t *p_nal;
356
357     p_nal = block_New( p_dec, 3 + i_size );
358
359     /* Add start code */
360     p_nal->p_buffer[0] = 0x00;
361     p_nal->p_buffer[1] = 0x00;
362     p_nal->p_buffer[2] = 0x01;
363
364     /* Copy nalu */
365     memcpy( &p_nal->p_buffer[3], p, i_size );
366
367     return p_nal;
368 }
369
370 static void nal_get_decoded( uint8_t **pp_ret, int *pi_ret,
371                              uint8_t *src, int i_src )
372 {
373     uint8_t *end = &src[i_src];
374     uint8_t *dst = malloc( i_src );
375
376     *pp_ret = dst;
377
378     while( src < end )
379     {
380         if( src < end - 3 && src[0] == 0x00 && src[1] == 0x00 &&
381             src[2] == 0x03 )
382         {
383             *dst++ = 0x00;
384             *dst++ = 0x00;
385
386             src += 3;
387             continue;
388         }
389         *dst++ = *src++;
390     }
391
392     *pi_ret = dst - *pp_ret;
393 }
394
395 static inline int bs_read_ue( bs_t *s )
396 {
397     int i = 0;
398
399     while( bs_read1( s ) == 0 && s->p < s->p_end && i < 32 )
400     {
401         i++;
402     }
403     return( ( 1 << i) - 1 + bs_read( s, i ) );
404 }
405
406 static inline int bs_read_se( bs_t *s )
407 {
408     int val = bs_read_ue( s );
409
410     return val&0x01 ? (val+1)/2 : -(val/2);
411 }
412
413
414 static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
415 {
416     decoder_sys_t *p_sys = p_dec->p_sys;
417     block_t *p_pic = NULL;
418
419     const int i_nal_ref_idc = (p_frag->p_buffer[3] >> 5)&0x03;
420     const int i_nal_type = p_frag->p_buffer[3]&0x1f;
421
422     if( p_sys->b_slice && !p_sys->b_sps )
423     {
424         block_ChainRelease( p_sys->p_frame );
425         msg_Warn( p_dec, "waiting for SPS" );
426
427         /* Reset context */
428         p_sys->p_frame = NULL;
429         p_sys->b_slice = VLC_FALSE;
430     }
431
432     if( !p_sys->b_sps &&
433         i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
434     {
435         p_sys->b_slice = VLC_TRUE;
436         /* Fragment will be discarded later on */
437     }
438     else if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
439     {
440         uint8_t *dec;
441         int i_dec, i_first_mb, i_slice_type, i_frame_num, i_pic_flags = 0;
442         vlc_bool_t b_pic = VLC_FALSE;
443         bs_t s;
444
445         /* do not convert the whole frame */
446         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[4],
447                          __MIN( p_frag->i_buffer - 4, 60 ) );
448         bs_init( &s, dec, i_dec );
449
450         /* first_mb_in_slice */
451         i_first_mb = bs_read_ue( &s );
452
453         /* slice_type */
454         switch( (i_slice_type = bs_read_ue( &s )) )
455         {
456             case 0: case 5:
457                 i_pic_flags = BLOCK_FLAG_TYPE_P;
458                 break;
459             case 1: case 6:
460                 i_pic_flags = BLOCK_FLAG_TYPE_B;
461                 break;
462             case 2: case 7:
463                 i_pic_flags = BLOCK_FLAG_TYPE_I;
464                 break;
465             case 3: case 8: /* SP */
466                 i_pic_flags = BLOCK_FLAG_TYPE_P;
467                 break;
468             case 4: case 9:
469                 i_pic_flags = BLOCK_FLAG_TYPE_I;
470                 break;
471         }
472
473         /* pic_parameter_set_id */
474         bs_read_ue( &s );
475         /* frame_num */
476         i_frame_num = bs_read( &s, p_sys->i_log2_max_frame_num + 4 );
477
478         /* Detection of the first VCL NAL unit of a primary coded picture
479          * (cf. 7.4.1.2.4) */
480         if( i_frame_num != p_sys->i_frame_num ||
481             ( (i_nal_ref_idc != p_sys->i_nal_ref_idc) &&
482               (!i_nal_ref_idc || !p_sys->i_nal_ref_idc) ) )
483         {
484             b_pic = VLC_TRUE;
485         }
486         p_sys->i_frame_num = i_frame_num;
487         p_sys->i_nal_ref_idc = i_nal_ref_idc;
488
489         if( !p_sys->b_frame_mbs_only )
490         {
491             /* field_pic_flag */
492             if( bs_read( &s, 1 ) )
493             {
494                 /* bottom_field_flag */
495                 bs_read( &s, 1 );
496             }
497         }
498
499         if( i_nal_type == NAL_SLICE_IDR )
500         {
501             /* id_pic_id */
502             int i_idr_pic_id = bs_read_ue( &s );
503             if( p_sys->i_nal_type != i_nal_type ) b_pic = VLC_TRUE;
504             if( p_sys->i_idr_pic_id != i_idr_pic_id ) b_pic = VLC_TRUE;
505             p_sys->i_idr_pic_id = i_idr_pic_id;
506         }
507         p_sys->i_nal_type = i_nal_type;
508
509         if( b_pic && p_sys->b_slice )
510         {
511             p_pic = block_ChainGather( p_sys->p_frame );
512             p_pic->i_dts = p_sys->i_dts;
513             p_pic->i_pts = p_sys->i_pts;
514             p_pic->i_length = 0;    /* FIXME */
515             p_pic->i_flags = p_sys->i_flags;
516
517             /* Reset context */
518             p_sys->p_frame = NULL;
519             p_sys->b_slice = VLC_FALSE;
520         }
521
522         p_sys->b_slice = VLC_TRUE;
523         p_sys->i_flags = i_pic_flags;
524         p_sys->i_dts   = p_frag->i_dts;
525         p_sys->i_pts   = p_frag->i_pts;
526
527         free( dec );
528     }
529     else if( i_nal_type == NAL_SPS )
530     {
531         uint8_t *dec;
532         int     i_dec;
533         bs_t s;
534         int i_tmp;
535
536         msg_Dbg( p_dec, "found NAL_SPS" );
537
538         p_sys->b_sps = VLC_TRUE;
539
540         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[4],
541                          p_frag->i_buffer - 4 );
542
543         bs_init( &s, dec, i_dec );
544         /* Skip profile(8), constraint_set012, reserver(5), level(8) */
545         bs_skip( &s, 8 + 1+1+1 + 5 + 8 );
546         /* sps id */
547         bs_read_ue( &s );
548         /* Skip i_log2_max_frame_num */
549         p_sys->i_log2_max_frame_num = bs_read_ue( &s );
550         /* Read poc_type */
551         i_tmp = bs_read_ue( &s );
552         if( i_tmp == 0 )
553         {
554             /* skip i_log2_max_poc_lsb */
555             bs_read_ue( &s );
556         }
557         else if( i_tmp == 1 )
558         {
559             int i_cycle;
560             /* skip b_delta_pic_order_always_zero */
561             bs_skip( &s, 1 );
562             /* skip i_offset_for_non_ref_pic */
563             bs_read_se( &s );
564             /* skip i_offset_for_top_to_bottom_field */
565             bs_read_se( &s );
566             /* read i_num_ref_frames_in_poc_cycle */
567             i_cycle = bs_read_ue( &s );
568             if( i_cycle > 256 ) i_cycle = 256;
569             while( i_cycle > 0 )
570             {
571                 /* skip i_offset_for_ref_frame */
572                 bs_read_se(&s );
573             }
574         }
575         /* i_num_ref_frames */
576         bs_read_ue( &s );
577         /* b_gaps_in_frame_num_value_allowed */
578         bs_skip( &s, 1 );
579
580         /* Read size */
581         p_dec->fmt_out.video.i_width  = 16 * ( bs_read_ue( &s ) + 1 );
582         p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
583
584         /* b_frame_mbs_only */
585         p_sys->b_frame_mbs_only = bs_read( &s, 1 );
586         if( p_sys->b_frame_mbs_only == 0 )
587         {
588             bs_skip( &s, 1 );
589         }
590         /* b_direct8x8_inference */
591         bs_skip( &s, 1 );
592
593         /* crop */
594         i_tmp = bs_read( &s, 1 );
595         if( i_tmp )
596         {
597             /* left */
598             bs_read_ue( &s );
599             /* right */
600             bs_read_ue( &s );
601             /* top */
602             bs_read_ue( &s );
603             /* bottom */
604             bs_read_ue( &s );
605         }
606
607         /* vui */
608         i_tmp = bs_read( &s, 1 );
609         if( i_tmp )
610         {
611             /* read the aspect ratio part if any FIXME check it */
612             i_tmp = bs_read( &s, 1 );
613             if( i_tmp )
614             {
615                 static const struct { int w, h; } sar[14] =
616                 {
617                     { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
618                     { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
619                     { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
620                     { 64, 33 }, { 160,99 },
621                 };
622                 int i_sar = bs_read( &s, 8 );
623                 int w, h;
624
625                 if( i_sar < 14 )
626                 {
627                     w = sar[i_sar].w;
628                     h = sar[i_sar].h;
629                 }
630                 else
631                 {
632                     w = bs_read( &s, 16 );
633                     h = bs_read( &s, 16 );
634                 }
635                 p_dec->fmt_out.video.i_aspect =
636                     VOUT_ASPECT_FACTOR * w / h * p_dec->fmt_out.video.i_width /
637                     p_dec->fmt_out.video.i_height;
638             }
639         }
640
641         free( dec );
642     }
643     else if( i_nal_type == NAL_PPS )
644     {
645         bs_t s;
646         bs_init( &s, &p_frag->p_buffer[4], p_frag->i_buffer - 4 );
647
648         /* TODO */
649         msg_Dbg( p_dec, "found NAL_PPS" );
650     }
651
652     /* Append the block */
653     block_ChainAppend( &p_sys->p_frame, p_frag );
654
655     return p_pic;
656 }