]> git.sesse.net Git - vlc/blob - modules/packetizer/mpeg4video.c
* v4l: updated, now it should grab (and compress if you want) the video.
[vlc] / modules / packetizer / mpeg4video.c
1 /*****************************************************************************
2  * mpeg4video.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: mpeg4video.c,v 1.9 2003/03/31 03:46:11 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_packetizer_input_t *p_sout_input;
49     sout_packet_format_t    output_format;
50
51     mtime_t                 i_last_pts;
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', '4', 's', '2'):
114         case VLC_FOURCC( 'M', '4', 'S', '2'):
115         case VLC_FOURCC( 'm', 'p', '4', 's'):
116         case VLC_FOURCC( 'M', 'P', '4', 'S'):
117         case VLC_FOURCC( 'm', 'p', '4', 'v'):
118         case VLC_FOURCC( 'D', 'I', 'V', 'X'):
119         case VLC_FOURCC( 'd', 'i', 'v', 'x'):
120         case VLC_FOURCC( 'X', 'V', 'I', 'D'):
121         case VLC_FOURCC( 'X', 'v', 'i', 'D'):
122         case VLC_FOURCC( 'x', 'v', 'i', 'd'):
123         case VLC_FOURCC( 'D', 'X', '5', '0'):
124         case VLC_FOURCC( 0x04, 0,   0,   0):
125         case VLC_FOURCC( '3', 'I', 'V', '2'):
126
127             return VLC_SUCCESS;
128         default:
129             return VLC_EGENERIC;
130     }
131 }
132
133 /*****************************************************************************
134  * RunDecoder: this function is called just after the thread is created
135  *****************************************************************************/
136 static int Run( decoder_fifo_t *p_fifo )
137 {
138     packetizer_thread_t *p_pack;
139     int b_error;
140
141     msg_Info( p_fifo, "Running MPEG4 Video packetizer" );
142     if( !( p_pack = malloc( sizeof( packetizer_thread_t ) ) ) )
143     {
144         msg_Err( p_fifo, "out of memory" );
145         DecoderError( p_fifo );
146         return( -1 );
147     }
148     memset( p_pack, 0, sizeof( packetizer_thread_t ) );
149
150     p_pack->p_fifo = p_fifo;
151
152     if( InitThread( p_pack ) != 0 )
153     {
154         DecoderError( p_fifo );
155         return( -1 );
156     }
157
158     while( ( !p_pack->p_fifo->b_die )&&( !p_pack->p_fifo->b_error ) )
159     {
160         PacketizeThread( p_pack );
161     }
162
163
164     if( ( b_error = p_pack->p_fifo->b_error ) )
165     {
166         DecoderError( p_pack->p_fifo );
167     }
168
169     EndThread( p_pack );
170     if( b_error )
171     {
172         return( -1 );
173     }
174
175     return( 0 );
176 }
177
178
179 #define FREE( p ) if( p ) free( p ); p = NULL
180
181 /*****************************************************************************
182  * InitThread: initialize data before entering main loop
183  *****************************************************************************/
184
185 static int InitThread( packetizer_thread_t *p_pack )
186 {
187     BITMAPINFOHEADER *p_bih;
188
189     p_bih = (BITMAPINFOHEADER*)p_pack->p_fifo->p_bitmapinfoheader;
190
191     if( p_bih && p_bih->biSize > sizeof( BITMAPINFOHEADER ) )
192     {
193         /* We have a vol */
194         p_pack->i_vol = p_bih->biSize - sizeof( BITMAPINFOHEADER );
195         p_pack->p_vol = malloc( p_pack->i_vol );
196         memcpy( p_pack->p_vol, &p_bih[1], p_pack->i_vol );
197
198         /* create stream input output */
199         p_pack->output_format.i_cat = VIDEO_ES;
200         p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'v' );
201         p_pack->output_format.p_format = malloc( p_bih->biSize );
202         memcpy( p_pack->output_format.p_format, p_bih, p_bih->biSize );
203
204         msg_Warn( p_pack->p_fifo, "opening with vol size:%d", p_pack->i_vol );
205         p_pack->p_sout_input =
206             sout_InputNew( p_pack->p_fifo,
207                            &p_pack->output_format );
208     }
209     else
210     {
211         p_pack->i_vol = 0;
212         p_pack->p_vol = 0;
213         p_pack->output_format.i_cat = UNKNOWN_ES;
214         p_pack->output_format.i_fourcc = VLC_FOURCC( 'n', 'u', 'l', 'l' );
215         p_pack->output_format.p_format = NULL;
216
217         p_pack->p_sout_input =
218             sout_InputNew( p_pack->p_fifo,
219                            &p_pack->output_format );
220     }
221
222     if( !p_pack->p_sout_input )
223     {
224         msg_Err( p_pack->p_fifo, "cannot add a new stream" );
225         return( -1 );
226     }
227     p_pack->i_last_pts = 0;
228
229     return VLC_SUCCESS;
230 }
231
232 static int m4v_FindStartCode( uint8_t **pp_data, uint8_t *p_end )
233 {
234     for( ; *pp_data < p_end - 4; (*pp_data)++ )
235     {
236         if( (*pp_data)[0] == 0 && (*pp_data)[1] == 0 && (*pp_data)[2] == 1 )
237         {
238             return( 0 );
239         }
240     }
241     fprintf( stderr, "\n********* cannot find startcode\n" );
242     return( -1 );
243 }
244 /*****************************************************************************
245  * PacketizeThread: packetize an unit (here copy a complete pes)
246  *****************************************************************************/
247 static void PacketizeThread( packetizer_thread_t *p_pack )
248 {
249     sout_buffer_t   *p_sout_buffer;
250     pes_packet_t    *p_pes;
251     ssize_t         i_size;
252     mtime_t         i_pts;
253
254     /* **** get samples count **** */
255     input_ExtractPES( p_pack->p_fifo, &p_pes );
256     if( !p_pes )
257     {
258         p_pack->p_fifo->b_error = 1;
259         return;
260     }
261
262     i_pts = p_pes->i_pts;
263     if( i_pts <= 0 && p_pack->i_last_pts <= 0 )
264     {
265         msg_Err( p_pack->p_fifo, "need a starting pts" );
266         input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
267         return;
268     }
269
270     i_size = p_pes->i_pes_size;
271     if( i_size > 0 )
272     {
273         pes_packet_t    *p_pes_next;
274         data_packet_t   *p_data;
275         ssize_t          i_buffer;
276
277         p_sout_buffer =
278             sout_BufferNew( p_pack->p_sout_input->p_sout, i_size );
279         if( !p_sout_buffer )
280         {
281             p_pack->p_fifo->b_error = 1;
282             return;
283         }
284         /* TODO: memcpy of the pes packet */
285         for( i_buffer = 0, p_data = p_pes->p_first;
286              p_data != NULL && i_buffer < i_size;
287              p_data = p_data->p_next)
288         {
289             ssize_t i_copy;
290
291             i_copy = __MIN( p_data->p_payload_end - p_data->p_payload_start, 
292                             i_size - i_buffer );
293             if( i_copy > 0 )
294             {
295                 p_pack->p_fifo->p_vlc->pf_memcpy( p_sout_buffer->p_buffer + i_buffer,
296                                                   p_data->p_payload_start,
297                                                   i_copy );
298             }
299             i_buffer += i_copy;
300         }
301
302         input_ShowPES( p_pack->p_fifo, &p_pes_next );
303         if( p_pes_next && p_pes_next->i_pts > 0 )
304         {
305             mtime_t i_gap;
306
307             i_gap = p_pes_next->i_pts - p_pes->i_pts;
308             p_sout_buffer->i_length = i_gap;
309         }
310         else
311         {
312             p_sout_buffer->i_length = 0;
313         }
314         p_sout_buffer->i_dts = i_pts;
315         p_sout_buffer->i_pts = i_pts;
316         p_sout_buffer->i_bitrate = 0;
317
318         if( p_pack->p_vol == NULL )
319         {
320             uint8_t *p_vol_begin, *p_vol_end, *p_end;
321             /* search if p_sout_buffer contains with a vol */
322             p_vol_begin = p_sout_buffer->p_buffer;
323             p_vol_end   = NULL;
324             p_end       = p_sout_buffer->p_buffer + p_sout_buffer->i_size;
325
326             for( ;; )
327             {
328                 if( m4v_FindStartCode( &p_vol_begin, p_end ) )
329                 {
330                     break;
331                 }
332                 msg_Dbg( p_pack->p_fifo,
333                           "starcode 0x%2.2x%2.2x%2.2x%2.2x",
334                           p_vol_begin[0], p_vol_begin[1], p_vol_begin[2], p_vol_begin[3] );
335
336                 if( ( p_vol_begin[3] & ~VIDEO_OBJECT_MASK ) == ( VIDEO_OBJECT_START_CODE&0xff ) )
337                 {
338                     p_vol_end = p_vol_begin + 4;
339                     if( m4v_FindStartCode( &p_vol_end, p_end ) )
340                     {
341                         break;
342                     }
343                     if( ( p_vol_end[3] & ~VIDEO_OBJECT_LAYER_MASK ) == ( VIDEO_OBJECT_LAYER_START_CODE&0xff ) )
344                     {
345                         p_vol_end += 4;
346                         if( m4v_FindStartCode( &p_vol_end, p_end ) )
347                         {
348                             p_vol_end = p_end;
349                         }
350                     }
351                     else
352                     {
353                         p_vol_end = NULL;
354                     }
355                 }
356                 else if( ( p_vol_begin[3] & ~VIDEO_OBJECT_LAYER_MASK ) == ( VIDEO_OBJECT_LAYER_START_CODE&0xff) )
357                 {
358                     p_vol_end = p_vol_begin + 4;
359                     if( m4v_FindStartCode( &p_vol_end, p_end ) )
360                     {
361                         p_vol_end = p_end;
362                     }
363                 }
364
365                 if( p_vol_end != NULL && p_vol_begin < p_vol_end )
366                 {
367                     BITMAPINFOHEADER *p_bih;
368
369                     p_pack->i_vol = p_vol_end - p_vol_begin;
370                     msg_Dbg( p_pack->p_fifo, "Reopening output" );
371
372                     p_pack->p_vol = malloc( p_pack->i_vol );
373                     memcpy( p_pack->p_vol, p_vol_begin, p_pack->i_vol );
374
375                     sout_InputDelete( p_pack->p_sout_input );
376
377                     p_pack->output_format.i_cat = VIDEO_ES;
378                     p_pack->output_format.i_fourcc = VLC_FOURCC( 'm', 'p', '4', 'v' );
379                     p_pack->output_format.p_format =
380                         (void*)p_bih = malloc( sizeof( BITMAPINFOHEADER ) + p_pack->i_vol);
381
382                     p_bih->biSize = sizeof( BITMAPINFOHEADER ) + p_pack->i_vol;
383                     p_bih->biWidth  = 0;
384                     p_bih->biHeight = 0;
385                     p_bih->biPlanes = 1;
386                     p_bih->biBitCount = 24;
387                     p_bih->biCompression = VLC_FOURCC( 'd', 'i', 'v', 'x' );
388                     p_bih->biSizeImage = 0;
389                     p_bih->biXPelsPerMeter = 0;
390                     p_bih->biYPelsPerMeter = 0;
391                     p_bih->biClrUsed = 0;
392                     p_bih->biClrImportant = 0;
393                     memcpy( &p_bih[1], p_pack->p_vol, p_pack->i_vol );
394
395                     p_pack->p_sout_input =
396                         sout_InputNew( p_pack->p_fifo,
397                                        &p_pack->output_format );
398                     if( !p_pack->p_sout_input )
399                     {
400                         p_pack->p_fifo->b_error = 1;
401                         return;
402                     }
403
404                     break;
405                 }
406                 else
407                 {
408                     p_vol_begin += 4;
409                 }
410             }
411         }
412
413         sout_InputSendBuffer( p_pack->p_sout_input,
414                                p_sout_buffer );
415     }
416
417     input_DeletePES( p_pack->p_fifo->p_packets_mgt, p_pes );
418 }
419
420
421 /*****************************************************************************
422  * EndThread : packetizer thread destruction
423  *****************************************************************************/
424 static void EndThread ( packetizer_thread_t *p_pack)
425 {
426     if( p_pack->p_sout_input )
427     {
428         sout_InputDelete( p_pack->p_sout_input );
429     }
430 }
431
432 static void input_ShowPES( decoder_fifo_t *p_fifo, pes_packet_t **pp_pes )
433 {
434     pes_packet_t *p_pes;
435
436     vlc_mutex_lock( &p_fifo->data_lock );
437
438     if( p_fifo->p_first == NULL )
439     {
440         if( p_fifo->b_die )
441         {
442             vlc_mutex_unlock( &p_fifo->data_lock );
443             if( pp_pes ) *pp_pes = NULL;
444             return;
445         }
446
447         /* Signal the input thread we're waiting. This is only
448          * needed in case of slave clock (ES plug-in) but it won't
449          * harm. */
450         vlc_cond_signal( &p_fifo->data_wait );
451
452         /* Wait for the input to tell us when we received a packet. */
453         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
454     }
455     p_pes = p_fifo->p_first;
456     vlc_mutex_unlock( &p_fifo->data_lock );
457
458     if( pp_pes )
459     {
460         *pp_pes = p_pes;
461     }
462 }
463