dlna: add PrepareForConnection action
[vlc.git] / modules / packetizer / mpeg4video.c
blob281e911a226d0071e1e7b7cf84af952f8407042c
1 /*****************************************************************************
2 * mpeg4video.c: mpeg 4 video packetizer
3 *****************************************************************************
4 * Copyright (C) 2001-2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Laurent Aimar <fenrir@via.ecp.fr>
9 * Eric Petit <titer@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_sout.h>
37 #include <vlc_codec.h>
38 #include <vlc_block.h>
40 #include <vlc_bits.h>
41 #include <vlc_block_helper.h>
42 #include "packetizer_helper.h"
43 #include "startcode_helper.h"
44 #include "iso_color_tables.h"
46 /*****************************************************************************
47 * Module descriptor
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
52 vlc_module_begin ()
53 set_category( CAT_SOUT )
54 set_subcategory( SUBCAT_SOUT_PACKETIZER )
55 set_description( N_("MPEG4 video packetizer") )
56 set_capability( "packetizer", 50 )
57 set_callbacks( Open, Close )
58 vlc_module_end ()
60 /****************************************************************************
61 * Local prototypes
62 ****************************************************************************/
63 typedef struct
66 * Input properties
68 packetizer_t packetizer;
71 * Common properties
73 vlc_tick_t i_interpolated_pts;
74 vlc_tick_t i_interpolated_dts;
75 vlc_tick_t i_last_ref_pts;
76 int64_t i_last_time_ref;
77 int64_t i_time_ref;
78 int64_t i_last_time;
79 int64_t i_last_timeincr;
81 unsigned int i_flags;
83 int i_fps_num;
84 int i_fps_den;
86 bool b_frame;
88 /* Current frame being built */
89 block_t *p_frame;
90 block_t **pp_last;
91 } decoder_sys_t;
93 static block_t *Packetize( decoder_t *, block_t ** );
94 static void PacketizeFlush( decoder_t * );
96 static void PacketizeReset( void *p_private, bool b_broken );
97 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t * );
98 static int PacketizeValidate( void *p_private, block_t * );
100 static block_t *ParseMPEGBlock( decoder_t *, block_t * );
101 static int ParseVOL( decoder_t *, es_format_t *, uint8_t *, int );
102 static int ParseVO( decoder_t *, block_t * );
103 static int ParseVOP( decoder_t *, block_t * );
104 static int vlc_log2( unsigned int );
106 #define VIDEO_OBJECT_MASK 0x01f
107 #define VIDEO_OBJECT_LAYER_MASK 0x00f
109 #define VIDEO_OBJECT_START_CODE 0x100
110 #define VIDEO_OBJECT_LAYER_START_CODE 0x120
111 #define RESERVED_START_CODE 0x130
112 #define VISUAL_OBJECT_SEQUENCE_START_CODE 0x1b0
113 #define VISUAL_OBJECT_SEQUENCE_END_CODE 0x1b1
114 #define USER_DATA_START_CODE 0x1b2
115 #define GROUP_OF_VOP_START_CODE 0x1b3
116 #define VIDEO_SESSION_ERROR_CODE 0x1b4
117 #define VISUAL_OBJECT_START_CODE 0x1b5
118 #define VOP_START_CODE 0x1b6
119 #define FACE_OBJECT_START_CODE 0x1ba
120 #define FACE_OBJECT_PLANE_START_CODE 0x1bb
121 #define MESH_OBJECT_START_CODE 0x1bc
122 #define MESH_OBJECT_PLANE_START_CODE 0x1bd
123 #define STILL_TEXTURE_OBJECT_START_CODE 0x1be
124 #define TEXTURE_SPATIAL_LAYER_START_CODE 0x1bf
125 #define TEXTURE_SNR_LAYER_START_CODE 0x1c0
127 static const uint8_t p_mp4v_startcode[3] = { 0x00, 0x00, 0x01 };
129 /*****************************************************************************
130 * Open: probe the packetizer and return score
131 *****************************************************************************/
132 static int Open( vlc_object_t *p_this )
134 decoder_t *p_dec = (decoder_t*)p_this;
135 decoder_sys_t *p_sys;
137 if( p_dec->fmt_in.i_codec != VLC_CODEC_MP4V )
138 return VLC_EGENERIC;
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 return VLC_ENOMEM;
143 memset( p_sys, 0, sizeof(decoder_sys_t) );
145 /* Misc init */
146 packetizer_Init( &p_sys->packetizer,
147 p_mp4v_startcode, sizeof(p_mp4v_startcode), startcode_FindAnnexB,
148 NULL, 0, 4,
149 PacketizeReset, PacketizeParse, PacketizeValidate, p_dec );
151 p_sys->p_frame = NULL;
152 p_sys->pp_last = &p_sys->p_frame;
154 /* Setup properties */
155 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
156 p_dec->fmt_out.i_codec = VLC_CODEC_MP4V;
158 if( p_dec->fmt_out.i_extra )
160 /* We have a vol */
161 msg_Dbg( p_dec, "opening with vol size: %d", p_dec->fmt_out.i_extra );
162 ParseVOL( p_dec, &p_dec->fmt_out,
163 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
166 /* Set callback */
167 p_dec->pf_packetize = Packetize;
168 p_dec->pf_flush = PacketizeFlush;
169 p_dec->pf_get_cc = NULL;
171 return VLC_SUCCESS;
174 /*****************************************************************************
175 * Close: clean up the packetizer
176 *****************************************************************************/
177 static void Close( vlc_object_t *p_this )
179 decoder_t *p_dec = (decoder_t*)p_this;
180 decoder_sys_t *p_sys = p_dec->p_sys;
182 packetizer_Clean( &p_sys->packetizer );
183 if( p_sys->p_frame )
184 block_ChainRelease( p_sys->p_frame );
185 free( p_sys );
188 /****************************************************************************
189 * Packetize: the whole thing
190 ****************************************************************************/
191 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
193 decoder_sys_t *p_sys = p_dec->p_sys;
195 return packetizer_Packetize( &p_sys->packetizer, pp_block );
198 static void PacketizeFlush( decoder_t *p_dec )
200 decoder_sys_t *p_sys = p_dec->p_sys;
202 packetizer_Flush( &p_sys->packetizer );
205 /*****************************************************************************
206 * Helpers:
207 *****************************************************************************/
208 static void PacketizeReset( void *p_private, bool b_broken )
210 decoder_t *p_dec = p_private;
211 decoder_sys_t *p_sys = p_dec->p_sys;
213 if( b_broken )
215 if( p_sys->p_frame )
216 block_ChainRelease( p_sys->p_frame );
217 p_sys->p_frame = NULL;
218 p_sys->pp_last = &p_sys->p_frame;
221 p_sys->i_interpolated_pts =
222 p_sys->i_interpolated_dts =
223 p_sys->i_last_ref_pts = VLC_TICK_INVALID;
225 p_sys->i_last_time_ref =
226 p_sys->i_time_ref =
227 p_sys->i_last_time =
228 p_sys->i_last_timeincr = 0;
231 static block_t *PacketizeParse( void *p_private, bool *pb_ts_used, block_t *p_block )
233 decoder_t *p_dec = p_private;
234 const vlc_tick_t i_dts = p_block->i_dts;
235 const vlc_tick_t i_pts = p_block->i_pts;
237 block_t *p_au = ParseMPEGBlock( p_dec, p_block );
239 *pb_ts_used = p_au && p_au->i_dts == i_dts && p_au->i_pts == i_pts;
241 return p_au;
245 static int PacketizeValidate( void *p_private, block_t *p_au )
247 decoder_t *p_dec = p_private;
248 decoder_sys_t *p_sys = p_dec->p_sys;
250 /* We've just started the stream, wait for the first PTS.
251 * We discard here so we can still get the sequence header. */
252 if( p_sys->i_interpolated_pts == VLC_TICK_INVALID &&
253 p_sys->i_interpolated_dts == VLC_TICK_INVALID )
255 msg_Dbg( p_dec, "need a starting pts/dts" );
256 return VLC_EGENERIC;
259 /* When starting the stream we can have the first frame with
260 * a null DTS (i_interpolated_pts is initialized to 0) */
261 if( p_au->i_dts == VLC_TICK_INVALID )
262 p_au->i_dts = p_au->i_pts;
263 return VLC_SUCCESS;
266 /*****************************************************************************
267 * ParseMPEGBlock: Re-assemble fragments into a block containing a picture
268 *****************************************************************************/
269 static block_t *ParseMPEGBlock( decoder_t *p_dec, block_t *p_frag )
271 decoder_sys_t *p_sys = p_dec->p_sys;
272 block_t *p_pic = NULL;
274 if( p_frag->i_buffer < 4 )
275 return p_frag;
277 const uint32_t i_startcode = GetDWBE( p_frag->p_buffer );
278 if( i_startcode == VISUAL_OBJECT_SEQUENCE_START_CODE ||
279 i_startcode == VISUAL_OBJECT_SEQUENCE_END_CODE ||
280 i_startcode == USER_DATA_START_CODE )
281 { /* VOS and USERDATA */
282 #if 0
283 /* Remove VOS start/end code from the original stream */
284 block_Release( p_frag );
285 #else
286 /* Append the block for now since ts/ps muxers rely on VOL
287 * being present in the stream */
288 block_ChainLastAppend( &p_sys->pp_last, p_frag );
289 #endif
290 return NULL;
292 else if( i_startcode >= VIDEO_OBJECT_LAYER_START_CODE &&
293 i_startcode < RESERVED_START_CODE )
295 /* Copy the complete VOL */
296 if( (size_t)p_dec->fmt_out.i_extra != p_frag->i_buffer )
298 p_dec->fmt_out.p_extra =
299 xrealloc( p_dec->fmt_out.p_extra, p_frag->i_buffer );
300 p_dec->fmt_out.i_extra = p_frag->i_buffer;
302 memcpy( p_dec->fmt_out.p_extra, p_frag->p_buffer, p_frag->i_buffer );
303 ParseVOL( p_dec, &p_dec->fmt_out,
304 p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
306 #if 0
307 /* Remove from the original stream */
308 block_Release( p_frag );
309 #else
310 /* Append the block for now since ts/ps muxers rely on VOL
311 * being present in the stream */
312 block_ChainLastAppend( &p_sys->pp_last, p_frag );
313 #endif
314 return NULL;
316 else
318 if( !p_dec->fmt_out.i_extra )
320 msg_Warn( p_dec, "waiting for VOL" );
321 block_Release( p_frag );
322 return NULL;
325 /* Append the block */
326 block_ChainLastAppend( &p_sys->pp_last, p_frag );
329 if( i_startcode == VISUAL_OBJECT_START_CODE )
331 ParseVO( p_dec, p_frag );
333 else
334 if( i_startcode == VOP_START_CODE &&
335 ParseVOP( p_dec, p_frag ) == VLC_SUCCESS )
337 /* We are dealing with a VOP */
338 p_pic = block_ChainGather( p_sys->p_frame );
339 p_pic->i_flags = p_sys->i_flags;
340 p_pic->i_pts = p_sys->i_interpolated_pts;
341 p_pic->i_dts = p_sys->i_interpolated_dts;
343 #if 0
344 msg_Err( p_dec, "output dts/pts (%"PRId64",%"PRId64")", p_pic->i_dts, p_pic->i_pts );
345 #endif
346 /* Reset context */
347 p_sys->p_frame = NULL;
348 p_sys->pp_last = &p_sys->p_frame;
351 return p_pic;
354 /* ParseVOL:
355 * TODO:
356 * - support aspect ratio
358 static int ParseVOL( decoder_t *p_dec, es_format_t *fmt,
359 uint8_t *p_vol, int i_vol )
361 decoder_sys_t *p_sys = p_dec->p_sys;
362 int i_vo_ver_id, i_ar, i_shape;
363 bs_t s;
365 for( ;; )
367 if( i_vol <= 5 )
368 return VLC_EGENERIC;
370 if( p_vol[0] == 0x00 && p_vol[1] == 0x00 && p_vol[2] == 0x01 &&
371 p_vol[3] >= 0x20 && p_vol[3] <= 0x2f ) break;
373 p_vol++; i_vol--;
376 bs_init( &s, &p_vol[4], i_vol - 4 );
378 bs_skip( &s, 1 ); /* random access */
379 bs_skip( &s, 8 ); /* vo_type */
380 if( bs_read1( &s ) )
382 i_vo_ver_id = bs_read( &s, 4 );
383 bs_skip( &s, 3 );
385 else
387 i_vo_ver_id = 1;
389 i_ar = bs_read( &s, 4 );
390 if( i_ar == 0xf )
392 bs_skip( &s, 8 ); /* ar_width */
393 bs_skip( &s, 8 ); /* ar_height */
395 if( bs_read1( &s ) )
397 /* vol control parameter */
398 bs_skip( &s, 2 ); /* chroma_format */
399 bs_read1( &s ); /* low_delay */
401 if( bs_read1( &s ) )
403 bs_skip( &s, 16 );
404 bs_skip( &s, 16 );
405 bs_skip( &s, 16 );
406 bs_skip( &s, 3 );
407 bs_skip( &s, 11 );
408 bs_skip( &s, 1 );
409 bs_skip( &s, 16 );
412 /* shape 0->RECT, 1->BIN, 2->BIN_ONLY, 3->GRAY */
413 i_shape = bs_read( &s, 2 );
414 if( i_shape == 3 && i_vo_ver_id != 1 )
416 bs_skip( &s, 4 );
419 if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
421 p_sys->i_fps_num = bs_read( &s, 16 ); /* Time increment resolution*/
422 if( !p_sys->i_fps_num ) p_sys->i_fps_num = 1;
424 if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
426 if( bs_read1( &s ) )
428 int i_time_increment_bits = vlc_log2( p_sys->i_fps_num - 1 ) + 1;
430 if( i_time_increment_bits < 1 ) i_time_increment_bits = 1;
432 p_sys->i_fps_den = bs_read( &s, i_time_increment_bits );
434 if( i_shape == 0 )
436 bs_skip( &s, 1 );
437 fmt->video.i_width = bs_read( &s, 13 );
438 bs_skip( &s, 1 );
439 fmt->video.i_height= bs_read( &s, 13 );
440 bs_skip( &s, 1 );
443 return VLC_SUCCESS;
446 static int ParseVO( decoder_t *p_dec, block_t *p_vo )
448 bs_t s;
449 bs_init( &s, &p_vo->p_buffer[4], p_vo->i_buffer - 4 );
450 if( bs_read1( &s ) )
451 bs_skip( &s, 7 );
453 const uint8_t visual_object_type = bs_read( &s, 4 );
454 if( visual_object_type == 1 /* video ID */ ||
455 visual_object_type == 2 /* still texture ID */ )
457 uint8_t colour_primaries = 1;
458 uint8_t colour_xfer = 1;
459 uint8_t colour_matrix_coeff = 1;
460 uint8_t full_range = 0;
461 if( bs_read1( &s ) ) /* video_signal_type */
463 bs_read( &s, 3 );
464 full_range = bs_read( &s, 1 );
465 if( bs_read( &s, 1 ) ) /* colour description */
467 colour_primaries = bs_read( &s, 8 );
468 colour_xfer = bs_read( &s, 8 );
469 colour_matrix_coeff = bs_read( &s, 8 );
473 if( p_dec->fmt_in.video.primaries == COLOR_PRIMARIES_UNDEF )
475 p_dec->fmt_out.video.primaries = iso_23001_8_cp_to_vlc_primaries( colour_primaries );
476 p_dec->fmt_out.video.transfer = iso_23001_8_tc_to_vlc_xfer( colour_xfer );
477 p_dec->fmt_out.video.space = iso_23001_8_mc_to_vlc_coeffs( colour_matrix_coeff );
478 p_dec->fmt_out.video.color_range = full_range ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
482 return VLC_SUCCESS;
485 static int ParseVOP( decoder_t *p_dec, block_t *p_vop )
487 decoder_sys_t *p_sys = p_dec->p_sys;
488 int64_t i_time_increment, i_time_ref;
489 int i_modulo_time_base = 0, i_time_increment_bits;
490 bs_t s;
492 bs_init( &s, &p_vop->p_buffer[4], p_vop->i_buffer - 4 );
494 switch( bs_read( &s, 2 ) )
496 case 0:
497 p_sys->i_flags = BLOCK_FLAG_TYPE_I;
498 break;
499 case 1:
500 p_sys->i_flags = BLOCK_FLAG_TYPE_P;
501 break;
502 case 2:
503 p_sys->i_flags = BLOCK_FLAG_TYPE_B;
504 p_sys->b_frame = true;
505 break;
506 case 3: /* gni ? */
507 p_sys->i_flags = BLOCK_FLAG_TYPE_PB;
508 break;
511 while( bs_read( &s, 1 ) ) i_modulo_time_base++;
512 if( !bs_read1( &s ) ) return VLC_EGENERIC; /* Marker */
514 /* VOP time increment */
515 i_time_increment_bits = vlc_log2(p_sys->i_fps_num - 1) + 1;
516 if( i_time_increment_bits < 1 ) i_time_increment_bits = 1;
517 i_time_increment = bs_read( &s, i_time_increment_bits );
519 /* Interpolate PTS/DTS */
520 if( !(p_sys->i_flags & BLOCK_FLAG_TYPE_B) )
522 p_sys->i_last_time_ref = p_sys->i_time_ref;
523 p_sys->i_time_ref +=
524 (i_modulo_time_base * p_sys->i_fps_num);
525 i_time_ref = p_sys->i_time_ref;
527 else
529 i_time_ref = p_sys->i_last_time_ref +
530 (i_modulo_time_base * p_sys->i_fps_num);
533 int64_t i_time_diff = (i_time_ref + i_time_increment) - (p_sys->i_last_time + p_sys->i_last_timeincr);
534 if( p_sys->i_fps_num && i_modulo_time_base == 0 && i_time_diff < 0 && -i_time_diff > p_sys->i_fps_num )
536 msg_Warn(p_dec, "missing modulo_time_base update");
537 i_modulo_time_base += -i_time_diff / p_sys->i_fps_num;
538 p_sys->i_time_ref += (i_modulo_time_base * p_sys->i_fps_num);
539 p_sys->i_time_ref += p_sys->i_last_timeincr % p_sys->i_fps_num;
540 i_time_ref = p_sys->i_time_ref;
543 if( p_sys->i_fps_num < 5 && /* Work-around buggy streams */
544 p_dec->fmt_in.video.i_frame_rate > 0 &&
545 p_dec->fmt_in.video.i_frame_rate_base > 0 )
547 p_sys->i_interpolated_pts += vlc_tick_from_samples(
548 p_dec->fmt_in.video.i_frame_rate_base,
549 p_dec->fmt_in.video.i_frame_rate);
551 else if( p_sys->i_fps_num )
553 i_time_diff = (i_time_ref + i_time_increment) - (p_sys->i_last_time + p_sys->i_last_timeincr);
554 p_sys->i_interpolated_pts += vlc_tick_from_samples( i_time_diff, p_sys->i_fps_num );
557 #if 0
558 msg_Err( p_dec, "interp dts/pts (%"PRId64",%"PRId64"), dts/pts (%"PRId64",%"PRId64") %"PRId64" mod %d inc %"PRId64,
559 p_sys->i_interpolated_dts, p_sys->i_interpolated_pts,
560 p_vop->i_dts, p_vop->i_pts, p_sys->i_time_ref, i_modulo_time_base, i_time_increment );
561 #endif
563 p_sys->i_last_time = i_time_ref;
564 p_sys->i_last_timeincr = i_time_increment;
566 /* Correct interpolated dts when we receive a new pts/dts */
567 if( p_vop->i_pts != VLC_TICK_INVALID )
568 p_sys->i_interpolated_pts = p_vop->i_pts;
569 if( p_vop->i_dts != VLC_TICK_INVALID )
570 p_sys->i_interpolated_dts = p_vop->i_dts;
572 if( (p_sys->i_flags & BLOCK_FLAG_TYPE_B) || !p_sys->b_frame )
574 /* Trivial case (DTS == PTS) */
576 p_sys->i_interpolated_dts = p_sys->i_interpolated_pts;
578 if( p_vop->i_pts != VLC_TICK_INVALID )
579 p_sys->i_interpolated_dts = p_vop->i_pts;
580 if( p_vop->i_dts != VLC_TICK_INVALID )
581 p_sys->i_interpolated_dts = p_vop->i_dts;
583 p_sys->i_interpolated_pts = p_sys->i_interpolated_dts;
585 else
587 if( p_sys->i_last_ref_pts != VLC_TICK_INVALID )
588 p_sys->i_interpolated_dts = p_sys->i_last_ref_pts;
590 p_sys->i_last_ref_pts = p_sys->i_interpolated_pts;
593 return VLC_SUCCESS;
596 /* look at libavutil av_log2 ;) */
597 static int vlc_log2( unsigned int v )
599 int n = 0;
600 static const int vlc_log2_table[16] =
602 0,0,1,1,2,2,2,2, 3,3,3,3,3,3,3,3
605 if( v&0xffff0000 )
607 v >>= 16;
608 n += 16;
610 if( v&0xff00 )
612 v >>= 8;
613 n += 8;
615 if( v&0xf0 )
617 v >>= 4;
618 n += 4;
620 n += vlc_log2_table[v];
622 return n;