1e83016231d1205a328b1cb828fc9a4a48c32f4c
[vlc.git] / modules / packetizer / h264.c
1 /*****************************************************************************
2  * h264.c: h264/avc video packetizer
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: mpeg4video.c 7338 2004-04-13 10:52:29Z gbazin $
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         }
271         *dst++ = *src++;
272     }
273
274     *pi_ret = dst - *pp_ret;
275 }
276
277 static inline int bs_read_ue( bs_t *s )
278 {
279     int i = 0;
280
281     while( bs_read1( s ) == 0 && s->p < s->p_end && i < 32 )
282     {
283         i++;
284     }
285     return( ( 1 << i) - 1 + bs_read( s, i ) );
286 }
287 static inline int bs_read_se( bs_t *s ) 
288 {
289     int val = bs_read_ue( s );
290
291     return val&0x01 ? (val+1)/2 : -(val/2);
292 }
293
294
295 static block_t *ParseNALBlock( decoder_t *p_dec, block_t *p_frag )
296 {
297     decoder_sys_t *p_sys = p_dec->p_sys;
298     block_t *p_pic = NULL;
299
300     const int i_ref_idc = (p_frag->p_buffer[4] >> 5)&0x03;
301     const int i_nal_type= p_frag->p_buffer[4]&0x1f;
302
303     if( p_sys->b_slice &&
304         ( i_nal_type == NAL_SLICE || i_nal_type == NAL_SLICE_IDR ||
305           i_nal_type == NAL_SLICE_DPC || i_nal_type == NAL_SPS || i_nal_type == NAL_PPS ) )
306     {
307         if( p_sys->b_sps )
308         {
309             p_pic = block_ChainGather( p_sys->p_frame );
310             p_pic->i_dts = p_sys->i_dts;
311             p_pic->i_pts = p_sys->i_pts;
312             p_pic->i_length = 0;    /* FIXME */
313             p_pic->i_flags = p_sys->i_flags;
314         }
315         else
316         {
317             block_ChainRelease( p_sys->p_frame );
318         }
319
320         /* reset context */
321         p_sys->p_frame = NULL;
322         p_sys->b_slice = VLC_FALSE;
323         //p_sys->i_dts += 40000;
324     }
325
326     if( i_nal_type >= NAL_SLICE && i_nal_type <= NAL_SLICE_IDR )
327     {
328         uint8_t *dec;
329         int     i_dec;
330         bs_t s;
331
332         p_sys->b_slice = VLC_TRUE;
333         p_sys->i_dts   = p_frag->i_dts;
334         p_sys->i_pts   = p_frag->i_pts;
335
336         /* do not convert the whole frame */
337         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5], __MIN( p_frag->i_buffer - 5, 60 ) );
338         bs_init( &s, dec, i_dec );
339
340         /* i_first_mb */
341         bs_read_ue( &s );
342         /* picture type */
343         switch( bs_read_ue( &s ) )
344         {
345             case 0: case 5:
346                 p_sys->i_flags = BLOCK_FLAG_TYPE_P;
347                 break;
348             case 1: case 6:
349                 p_sys->i_flags =BLOCK_FLAG_TYPE_B;
350                 break;
351             case 2: case 7:
352                 p_sys->i_flags = BLOCK_FLAG_TYPE_I;
353                 break;
354             case 3: case 8: /* SP */
355                 p_sys->i_flags = BLOCK_FLAG_TYPE_P;
356                 break;
357             case 4: case 9:
358                 p_sys->i_flags = BLOCK_FLAG_TYPE_I;
359                 break;
360         }
361
362         free( dec );
363     }
364     else if( i_nal_type == NAL_SPS )
365     {
366         uint8_t *dec;
367         int     i_dec;
368         bs_t s;
369         int i_tmp;
370
371         p_sys->b_sps = VLC_TRUE;
372
373         nal_get_decoded( &dec, &i_dec, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
374
375         bs_init( &s, dec, i_dec );
376         /* Skip profile(8), constraint_set012, reserver(5), level(8) */
377         bs_skip( &s, 8 + 1+1+1 + 5 + 8 );
378         /* sps id */
379         bs_read_ue( &s );
380         /* Skip i_log2_max_frame_num */
381         bs_read_ue( &s );
382         /* Read poc_type */
383         i_tmp = bs_read_ue( &s );
384         if( i_tmp == 0 )
385         {
386             /* skip i_log2_max_poc_lsb */
387             bs_read_ue( &s );
388         }
389         else if( i_tmp == 1 )
390         {
391             int i_cycle;
392             /* skip b_delta_pic_order_always_zero */
393             bs_skip( &s, 1 );
394             /* skip i_offset_for_non_ref_pic */
395             bs_read_se( &s );
396             /* skip i_offset_for_top_to_bottom_field */
397             bs_read_se( &s );
398             /* read i_num_ref_frames_in_poc_cycle */
399             i_cycle = bs_read_ue( &s );
400             if( i_cycle > 256 ) i_cycle = 256;
401             while( i_cycle > 0 )
402             {
403                 /* skip i_offset_for_ref_frame */
404                 bs_read_se(&s );
405             }
406         }
407         /* i_num_ref_frames */
408         bs_read_ue( &s );
409         /* b_gaps_in_frame_num_value_allowed */
410         bs_skip( &s, 1 );
411
412         /* Read size */
413         p_dec->fmt_out.video.i_width  = 16 * ( bs_read_ue( &s ) + 1 );
414         p_dec->fmt_out.video.i_height = 16 * ( bs_read_ue( &s ) + 1 );
415
416         /* b_frame_mbs_only */
417         i_tmp = bs_read( &s, 1 );
418         if( i_tmp == 0 )
419         {
420             bs_skip( &s, 1 );
421         }
422         /* b_direct8x8_inference */
423         bs_skip( &s, 1 );
424
425         /* crop ? */
426         i_tmp = bs_read( &s, 1 );
427         if( i_tmp )
428         {
429             /* left */
430             p_dec->fmt_out.video.i_width -= 2 * bs_read_ue( &s );
431             /* right */
432             p_dec->fmt_out.video.i_width -= 2 * bs_read_ue( &s );
433             /* top */
434             p_dec->fmt_out.video.i_height -= 2 * bs_read_ue( &s );
435             /* bottom */
436             p_dec->fmt_out.video.i_height -= 2 * bs_read_ue( &s );
437         }
438
439         /* vui */
440         i_tmp = bs_read( &s, 1 );
441         if( i_tmp )
442         {
443             /* read the aspect ratio part if any FIXME check it */
444             i_tmp = bs_read( &s, 1 );
445             if( i_tmp )
446             {
447                 static const struct { int w, h; } sar[14] =
448                 {
449                     { 0,   0 }, { 1,   1 }, { 12, 11 }, { 10, 11 },
450                     { 16, 11 }, { 40, 33 }, { 24, 11 }, { 20, 11 },
451                     { 32, 11 }, { 80, 33 }, { 18, 11 }, { 15, 11 },
452                     { 64, 33 }, { 160,99 },
453                 };
454                 int i_sar = bs_read( &s, 8 );
455                 int w, h;
456
457                 if( i_sar < 14 )
458                 {
459                     w = sar[i_sar].w;
460                     h = sar[i_sar].h;
461                 }
462                 else
463                 {
464                     w = bs_read( &s, 16 );
465                     h = bs_read( &s, 16 );
466                 }
467                 p_dec->fmt_out.video.i_aspect =
468                     VOUT_ASPECT_FACTOR *
469                     w / h *
470                     p_dec->fmt_out.video.i_width / p_dec->fmt_out.video.i_height;
471             }
472         }
473
474         free( dec );
475     }
476     else if( i_nal_type == NAL_PPS )
477     {
478         bs_t s;
479         bs_init( &s, &p_frag->p_buffer[5], p_frag->i_buffer - 5 );
480
481         /* TODO */
482     }
483
484
485     /* Append the block */
486     block_ChainAppend( &p_sys->p_frame, p_frag );
487
488     return p_pic;
489 }
490