]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4video.c
* some workaround for clock.
[vlc] / modules / packetizer / mpeg4video.c
1 /*****************************************************************************
2  * mpeg4video.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: mpeg4video.c,v 1.4 2003/01/12 04:11:35 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc/aout.h>
30 #include <vlc/decoder.h>
31 #include <vlc/input.h>
32 #include <vlc/sout.h>
33
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <string.h>                                              /* strdup() */
36
37 #include "codecs.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 typedef struct packetizer_thread_s
43 {
44     /* Input properties */
45     decoder_fifo_t          *p_fifo;
46
47     /* Output properties */
48     sout_input_t            *p_sout_input;
49     sout_packet_format_t    output_format;
50
51     mtime_t i_pts_start;
52
53     int                     i_vol;
54     uint8_t                 *p_vol;
55
56 } packetizer_thread_t;
57
58 static int  Open    ( vlc_object_t * );
59 static int  Run     ( decoder_fifo_t * );
60
61 static int  InitThread     ( packetizer_thread_t * );
62 static void PacketizeThread   ( packetizer_thread_t * );
63 static void EndThread      ( packetizer_thread_t * );
64
65
66 static void input_ShowPES( decoder_fifo_t *p_fifo, pes_packet_t **pp_pes );
67
68 /*****************************************************************************
69  * Module descriptor
70  *****************************************************************************/
71
72 vlc_module_begin();
73     set_description( _("MPEG4 Video packetizer") );
74     set_capability( "packetizer", 50 );
75     set_callbacks( Open, NULL );
76 vlc_module_end();
77
78 #define VIDEO_OBJECT_MASK                       0x01f
79 #define VIDEO_OBJECT_LAYER_MASK                 0x00f
80
81 #define VIDEO_OBJECT_START_CODE                 0x100
82 #define VIDEO_OBJECT_LAYER_START_CODE           0x120
83 #define VISUAL_OBJECT_SEQUENCE_START_CODE       0x1b0
84 #define VISUAL_OBJECT_SEQUENCE_END_CODE         0x1b1
85 #define USER_DATA_START_CODE                    0x1b2
86 #define GROUP_OF_VOP_START_CODE                 0x1b3
87 #define VIDEO_SESSION_ERROR_CODE                0x1b4
88 #define VISUAL_OBJECT_START_CODE                0x1b5
89 #define VOP_START_CODE                          0x1b6
90 #define FACE_OBJECT_START_CODE                  0x1ba
91 #define FACE_OBJECT_PLANE_START_CODE            0x1bb
92 #define MESH_OBJECT_START_CODE                  0x1bc
93 #define MESH_OBJECT_PLANE_START_CODE            0x1bd
94 #define STILL_TEXTURE_OBJECT_START_CODE         0x1be
95 #define TEXTURE_SPATIAL_LAYER_START_CODE        0x1bf
96 #define TEXTURE_SNR_LAYER_START_CODE            0x1c0
97
98
99 /*****************************************************************************
100  * OpenDecoder: probe the packetizer and return score
101  *****************************************************************************
102  * Tries to launch a decoder and return score so that the interface is able
103  * to choose.
104  *****************************************************************************/
105 static int Open( vlc_object_t *p_this )
106 {
107     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
108
109     p_fifo->pf_run = Run;
110
111     switch(  p_fifo->i_fourcc )
112     {
113         case VLC_FOURCC( 'm', 'p', '4', 'v'):
114         case VLC_FOURCC( 'D', 'I', 'V', 'X'):
115         case VLC_FOURCC( 'd', 'i', 'v', 'x'):
116         case VLC_FOURCC( 'X', 'V', 'I', 'D'):
117         case VLC_FOURCC( 'X', 'v', 'i', 'D'):
118         case VLC_FOURCC( 'x', 'v', 'i', 'd'):
119         case VLC_FOURCC( 'D', 'X', '5', '0'):
120             return VLC_SUCCESS;
121         default:
122             return VLC_EGENERIC;
123     }
124 }
125
126 /*****************************************************************************
127  * RunDecoder: this function is called just after the thread is created
128  *****************************************************************************/
129 static int Run( decoder_fifo_t *p_fifo )
130 {
131     packetizer_thread_t *p_pack;
132     int b_error;
133
134     msg_Info( p_fifo, "Running MPEG4 Video packetizer" );
135     if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
136     {
137         msg_Err( p_fifo, "out of memory" );
138         DecoderError( p_fifo );
139         return( -1 );
140     }
141     memset( p_pack, 0, sizeof( packetizer_thread_t ) );
142
143     p_pack->p_fifo = p_fifo;
144
145     if( InitThread( p_pack ) != 0 )
146     {
147         DecoderError( p_fifo );
148         return( -1 );
149     }
150
151     while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
152     {
153         PacketizeThread( p_pack );
154     }
155
156
157     if( ( b_error = p_pack->p_fifo->b_error ) )
158     {
159         DecoderError( p_pack->p_fifo );
160     }
161
162     EndThread( p_pack );
163     if( b_error )
164     {
165         return( -1 );
166     }
167
168     return( 0 );
169 }
170
171
172 #define FREE( p ) if( p ) free( p ); p = NULL
173
174 /*****************************************************************************
175  * InitThread: initialize data before entering main loop
176  *****************************************************************************/
177
178 static int InitThread( packetizer_thread_t *p_pack )
179 {
180     BITMAPINFOHEADER *p_bih;
181
182     p_bih = (BITMAPINFOHEADER*)p_pack->p_fifo->p_bitmapinfoheader;
183
184     if( p_bih && p_bih->biSize > sizeof( BITMAPINFOHEADER ) )
185     {
186         /* We have a vol */
187         p_pack->i_vol = p_bih->biSize - sizeof( BITMAPINFOHEADER );
188         p_pack->p_vol = malloc( p_pack->i_vol );
189         memcpy( p_pack->p_vol, &p_bih[1], p_pack->i_vol );
190
191         /* create stream input output */
192         p_pack->output_format.i_cat = VIDEO_ES;
193         p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'v' );
194         p_pack->output_format.p_format = malloc( p_bih->biSize );
195         memcpy( p_pack->output_format.p_format, p_bih, p_bih->biSize );
196
197         msg_Warn( p_pack->p_fifo, "opening with vol size:%d", p_pack->i_vol );
198         p_pack->p_sout_input =
199             sout_InputNew( p_pack->p_fifo,
200                            &p_pack->output_format );
201     }
202     else
203     {
204         p_pack->i_vol = 0;
205         p_pack->p_vol = 0;
206         p_pack->output_format.i_cat = UNKNOWN_ES;
207         p_pack->output_format.i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
208         p_pack->output_format.p_format = NULL;
209
210         p_pack->p_sout_input =
211             sout_InputNew( p_pack->p_fifo,
212                            &p_pack->output_format );
213     }
214
215     if( !p_pack->p_sout_input )
216     {
217         msg_Err( p_pack->p_fifo, "cannot add a new stream" );
218         return( -1 );
219     }
220     p_pack->i_pts_start = -1;
221     return( 0 );
222 }
223
224 static int m4v_FindStartCode( uint8_t **pp_data, uint8_t *p_end )
225 {
226     for( ; *pp_data < p_end - 4; (*pp_data)++ )
227     {
228         if( (*pp_data)[0] == 0 && (*pp_data)[1] == 0 && (*pp_data)[2] == 1 )
229         {
230             return( 0 );
231         }
232     }
233     fprintf( stderr, "\n********* cannot find startcode\n" );
234     return( -1 );
235 }
236 /*****************************************************************************
237  * PacketizeThread: packetize an unit (here copy a complete pes)
238  *****************************************************************************/
239 static void PacketizeThread( packetizer_thread_t *p_pack )
240 {
241     sout_buffer_t   *p_sout_buffer;
242     pes_packet_t    *p_pes;
243     size_t          i_size;
244
245     /* **** get samples count **** */
246     input_ExtractPES( p_pack->p_fifo, &p_pes );
247     if( !p_pes )
248     {
249         p_pack->p_fifo->b_error = 1;
250         return;
251     }
252     if( p_pack->i_pts_start < 0 )
253     {
254         p_pack->i_pts_start = p_pes->i_pts;
255     }
256
257     i_size = p_pes->i_pes_size;
258     if( i_size > 0 )
259     {
260         pes_packet_t    *p_pes_next;
261         data_packet_t   *p_data;
262         size_t          i_buffer;
263
264         p_sout_buffer = 
265             sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
266         if( !p_sout_buffer )
267         {
268             p_pack->p_fifo->b_error = 1;
269             return;
270         }
271         /* TODO: memcpy of the pes packet */
272         for( i_buffer = 0, p_data = p_pes->p_first;
273              p_data != NULL && i_buffer < i_size;
274              p_data = p_data->p_next)
275         {
276             size_t          i_copy;
277
278             i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start, 
279                             i_size - i_buffer );
280             if( i_copy > 0 )
281             {
282                 p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
283                                                   p_data->p_payload_start,
284                                                   i_copy );
285             }
286             i_buffer += i_copy;
287         }
288         p_sout_buffer->i_length = 0;
289         p_sout_buffer->i_dts = p_pes->i_pts - p_pack->i_pts_start;
290         p_sout_buffer->i_pts = p_pes->i_pts - p_pack->i_pts_start;
291         p_sout_buffer->i_bitrate = 0;
292
293         if( p_pack->p_vol == NULL )
294         {
295             uint8_t *p_vol_begin, *p_vol_end, *p_end;
296             /* search if p_sout_buffer contains with a vol */
297             p_vol_begin = p_sout_buffer->p_buffer;
298             p_vol_end   = NULL;
299             p_end       = p_sout_buffer->p_buffer + p_sout_buffer->i_size;
300
301             for( ;; )
302             {
303                 if( m4v_FindStartCode( &p_vol_begin, p_end ) )
304                 {
305                     break;
306                 }
307                 msg_Dbg( p_pack->p_fifo,
308                           "starcode 0x%2.2x%2.2x%2.2x%2.2x",
309                           p_vol_begin[0], p_vol_begin[1], p_vol_begin[2], p_vol_begin[3] );
310
311                 if( ( p_vol_begin[3] & ~VIDEO_OBJECT_MASK ) == ( VIDEO_OBJECT_START_CODE&0xff ) )
312                 {
313                     p_vol_end = p_vol_begin + 4;
314                     if( m4v_FindStartCode( &p_vol_end, p_end ) )
315                     {
316                         break;
317                     }
318                     if( ( p_vol_end[3] & ~VIDEO_OBJECT_LAYER_MASK ) == ( VIDEO_OBJECT_LAYER_START_CODE&0xff ) )
319                     {
320                         p_vol_end += 4;
321                         if( m4v_FindStartCode( &p_vol_end, p_end ) )
322                         {
323                             p_vol_end = p_end;
324                         }
325                     }
326                     else
327                     {
328                         p_vol_end = NULL;
329                     }
330                 }
331                 else if( ( p_vol_begin[3] & ~VIDEO_OBJECT_LAYER_MASK ) == ( VIDEO_OBJECT_LAYER_START_CODE&0xff) )
332                 {
333                     p_vol_end = p_vol_begin + 4;
334                     if( m4v_FindStartCode( &p_vol_end, p_end ) )
335                     {
336                         p_vol_end = p_end;
337                     }
338                 }
339
340                 if( p_vol_end != NULL && p_vol_begin < p_vol_end )
341                 {
342                     BITMAPINFOHEADER *p_bih;
343
344                     p_pack->i_vol = p_vol_end - p_vol_begin;
345                     msg_Dbg( p_pack->p_fifo, "Reopening output" );
346
347                     p_pack->p_vol = malloc( p_pack->i_vol );
348                     memcpy( p_pack->p_vol, p_vol_begin, p_pack->i_vol );
349
350                     sout_InputDelete( p_pack->p_sout_input );
351
352                     p_pack->output_format.i_cat = VIDEO_ES;
353                     p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'v' );
354                     p_pack->output_format.p_format =
355                         (void*)p_bih = malloc( sizeof( BITMAPINFOHEADER ) + p_pack->i_vol);
356
357                     p_bih->biSize = sizeof( BITMAPINFOHEADER ) + p_pack->i_vol;
358                     p_bih->biWidth  = 0;
359                     p_bih->biHeight = 0;
360                     p_bih->biPlanes = 1;
361                     p_bih->biBitCount = 24;
362                     p_bih->biCompression = 0x64697678; /* "divx" */
363                     p_bih->biSizeImage = 0;
364                     p_bih->biXPelsPerMeter = 0;
365                     p_bih->biYPelsPerMeter = 0;
366                     p_bih->biClrUsed = 0;
367                     p_bih->biClrImportant = 0;
368                     memcpy( &p_bih[1], p_pack->p_vol, p_pack->i_vol );
369
370                     p_pack->p_sout_input =
371                         sout_InputNew( p_pack->p_fifo,
372                                        &p_pack->output_format );
373                     if( !p_pack->p_sout_input )
374                     {
375                         p_pack->p_fifo->b_error = 1;
376                         return;
377                     }
378
379                     break;
380                 }
381                 else
382                 {
383                     p_vol_begin += 4;
384                 }
385             }
386         }
387
388         input_ShowPES( p_pack->p_fifo, &p_pes_next );
389         if( p_pes_next )
390         {
391             mtime_t i_gap;
392
393             i_gap = p_pes_next->i_pts - p_pes->i_pts;
394
395             if( i_gap > 1000000 / 80 )  // too big fps > 80 is no sense
396             {
397                 i_gap = 1000000 / 25;
398                 p_pack->i_pts_start =
399                     ( p_pes->i_pts - p_pack->i_pts_start ) + p_pes_next->i_pts - i_gap;
400             }
401             else if( i_gap < 0 )
402             {
403                 p_pack->i_pts_start =
404                     ( p_pes->i_pts - p_pack->i_pts_start ) + p_pes_next->i_pts;
405                 i_gap = 0;
406             }
407             p_sout_buffer->i_length = i_gap;
408         }
409         sout_InputSendBuffer( p_pack->p_sout_input,
410                                p_sout_buffer );
411     }
412
413     input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
414 }
415
416
417 /*****************************************************************************
418  * EndThread : packetizer thread destruction
419  *****************************************************************************/
420 static void EndThread ( packetizer_thread_t *p_pack)
421 {
422     if( p_pack->p_sout_input )
423     {
424         sout_InputDelete( p_pack->p_sout_input );
425     }
426 }
427
428 static void input_ShowPES( decoder_fifo_t *p_fifo, pes_packet_t **pp_pes )
429 {
430     pes_packet_t *p_pes;
431
432     vlc_mutex_lock( &p_fifo->data_lock );
433
434     if( p_fifo->p_first == NULL )
435     {
436         if( p_fifo->b_die )
437         {
438             vlc_mutex_unlock( &p_fifo->data_lock );
439             if( pp_pes ) *pp_pes = NULL;
440             return;
441         }
442
443         /* Signal the input thread we're waiting. This is only
444          * needed in case of slave clock (ES plug-in) but it won't
445          * harm. */
446         vlc_cond_signal( &p_fifo->data_wait );
447
448         /* Wait for the input to tell us when we received a packet. */
449         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
450     }
451     p_pes = p_fifo->p_first;
452     vlc_mutex_unlock( &p_fifo->data_lock );
453
454     if( pp_pes )
455     {
456         *pp_pes = p_pes;
457     }
458 }
459