]> git.sesse.net Git - vlc/blob - modules/demux/pva.c
mediacodec: don't loop in GetOutput
[vlc] / modules / demux / pva.c
1 /*****************************************************************************
2  * pva.c: PVA demuxer
3  *****************************************************************************
4  * Copyright (C) 2004 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open    ( vlc_object_t * );
40 static void Close  ( vlc_object_t * );
41
42 vlc_module_begin ()
43     set_description( N_("PVA demuxer" ) )
44     set_capability( "demux", 10 )
45     set_category( CAT_INPUT )
46     set_subcategory( SUBCAT_INPUT_DEMUX )
47     set_callbacks( Open, Close )
48     add_shortcut( "pva" )
49 vlc_module_end ()
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54
55 struct demux_sys_t
56 {
57     es_out_id_t *p_video;
58     es_out_id_t *p_audio;
59
60     /* counter */
61     int          i_vc;
62     int          i_ac;
63
64     /* audio/video block */
65     block_t     *p_pes; /* audio */
66     block_t     *p_es;  /* video */
67
68     int64_t     b_pcr_audio;
69 };
70
71 static int  Demux   ( demux_t *p_demux );
72 static int  Control ( demux_t *p_demux, int i_query, va_list args );
73
74 static int  ReSynch ( demux_t * );
75 static void ParsePES( demux_t * );
76
77 /*****************************************************************************
78  * Open
79  *****************************************************************************/
80 static int Open( vlc_object_t *p_this )
81 {
82     demux_t     *p_demux = (demux_t*)p_this;
83     demux_sys_t *p_sys;
84     es_format_t  fmt;
85     const uint8_t *p_peek;
86
87     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 ) return VLC_EGENERIC;
88     if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
89     {
90         /* In case we had forced this demuxer we try to resynch */
91         if( !p_demux->b_force || ReSynch( p_demux ) )
92             return VLC_EGENERIC;
93     }
94
95     p_sys = malloc( sizeof( demux_sys_t ) );
96     if( unlikely(p_sys == NULL) )
97         return VLC_ENOMEM;
98
99     /* Fill p_demux field */
100     p_demux->pf_demux = Demux;
101     p_demux->pf_control = Control;
102     p_demux->p_sys = p_sys;
103
104     /* Register one audio and one video stream */
105     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_MPGA );
106     fmt.b_packetized = false;
107     p_sys->p_audio = es_out_Add( p_demux->out, &fmt );
108
109     es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_MPGV );
110     fmt.b_packetized = false;
111     p_sys->p_video = es_out_Add( p_demux->out, &fmt );
112
113     p_sys->i_vc    = -1;
114     p_sys->i_ac    = -1;
115     p_sys->p_pes   = NULL;
116     p_sys->p_es    = NULL;
117
118     p_sys->b_pcr_audio = false;
119
120     return VLC_SUCCESS;
121 }
122
123 /*****************************************************************************
124  * Close
125  *****************************************************************************/
126 static void Close( vlc_object_t *p_this )
127 {
128     demux_t     *p_demux = (demux_t*)p_this;
129     demux_sys_t *p_sys = p_demux->p_sys;
130
131     if( p_sys->p_es )  block_Release( p_sys->p_es );
132     if( p_sys->p_pes ) block_Release( p_sys->p_pes );
133
134     free( p_sys );
135 }
136
137
138 /*****************************************************************************
139  * Demux:
140  *****************************************************************************
141  * See http://multimedia.cx/mirror/av_format_v1.pdf
142  *****************************************************************************/
143 static int Demux( demux_t *p_demux )
144 {
145     demux_sys_t *p_sys = p_demux->p_sys;
146
147     const uint8_t *p_peek;
148     int         i_size;
149     block_t     *p_frame;
150     int64_t     i_pts;
151     int         i_skip;
152
153     if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
154     {
155         msg_Warn( p_demux, "eof ?" );
156         return 0;
157     }
158     if( p_peek[0] != 'A' || p_peek[1] != 'V' || p_peek[4] != 0x55 )
159     {
160         msg_Warn( p_demux, "lost synchro" );
161         if( ReSynch( p_demux ) )
162         {
163             return -1;
164         }
165         if( stream_Peek( p_demux->s, &p_peek, 8 ) < 8 )
166         {
167             msg_Warn( p_demux, "eof ?" );
168             return 0;
169         }
170     }
171
172     i_size = GetWBE( &p_peek[6] );
173     switch( p_peek[2] )
174     {
175         case 0x01:  /* VideoStream */
176             if( p_sys->i_vc < 0 )
177             {
178                 msg_Dbg( p_demux, "first packet for video" );
179             }
180             else if( ((p_sys->i_vc + 1)&0xff) != p_peek[3] )
181             {
182                 msg_Dbg( p_demux, "packet lost (video)" );
183                 if( p_sys->p_es )
184                 {
185                     block_ChainRelease( p_sys->p_es );
186                     p_sys->p_es = NULL;
187                 }
188             }
189             p_sys->i_vc = p_peek[3];
190
191             /* read the PTS and potential extra bytes TODO: make it a bit more optimised */
192             i_pts = -1;
193             i_skip = 8;
194             if( p_peek[5]&0x10 )
195             {
196                 int i_pre = p_peek[5]&0x3;
197
198                 if( ( p_frame = stream_Block( p_demux->s, 8 + 4 + i_pre ) ) )
199                 {
200                     i_pts = GetDWBE( &p_frame->p_buffer[8] );
201                     if( p_frame->i_buffer > 12 )
202                     {
203                         p_frame->p_buffer += 12;
204                         p_frame->i_buffer -= 12;
205                         block_ChainAppend( &p_sys->p_es, p_frame );
206                     }
207                     else
208                     {
209                         block_Release( p_frame );
210                     }
211                 }
212                 i_size -= 4 + i_pre;
213                 i_skip  = 0;
214                 /* Set PCR */
215                 if( ( p_frame = p_sys->p_es ) )
216                 {
217
218                     if( p_frame->i_pts > VLC_TS_INVALID && !p_sys->b_pcr_audio )
219                     {
220                         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_frame->i_pts);
221                     }
222                     es_out_Send( p_demux->out, p_sys->p_video, p_frame );
223
224                     p_sys->p_es = NULL;
225                 }
226             }
227
228             if( ( p_frame = stream_Block( p_demux->s, i_size + i_skip ) ) )
229             {
230                 p_frame->p_buffer += i_skip;
231                 p_frame->i_buffer -= i_skip;
232                 if( i_pts >= 0 )
233                     p_frame->i_pts = VLC_TS_0 + i_pts * 100 / 9;
234                 block_ChainAppend( &p_sys->p_es, p_frame );
235             }
236             break;
237
238         case 0x02:  /* MainAudioStream */
239             if( p_sys->i_ac < 0 )
240             {
241                 msg_Dbg( p_demux, "first packet for audio" );
242             }
243             else if( ((p_sys->i_ac + 1)&0xff) != p_peek[3] )
244             {
245                 msg_Dbg( p_demux, "packet lost (audio)" );
246                 if( p_sys->p_pes )
247                 {
248                     block_ChainRelease( p_sys->p_pes );
249                     p_sys->p_pes = NULL;
250                 }
251             }
252             p_sys->i_ac = p_peek[3];
253
254             if( p_peek[5]&0x10 && p_sys->p_pes )
255             {
256                 ParsePES( p_demux );
257             }
258             if( ( p_frame = stream_Block( p_demux->s, i_size + 8 ) ) )
259             {
260                 p_frame->p_buffer += 8;
261                 p_frame->i_buffer -= 8;
262                 /* XXX this a hack, some streams aren't compliant and
263                  * don't set pes_start flag */
264                 if( p_sys->p_pes && p_frame->i_buffer > 4 &&
265                     p_frame->p_buffer[0] == 0x00 &&
266                     p_frame->p_buffer[1] == 0x00 &&
267                     p_frame->p_buffer[2] == 0x01 )
268                 {
269                     ParsePES( p_demux );
270                 }
271                 block_ChainAppend( &p_sys->p_pes, p_frame );
272             }
273             break;
274
275         default:
276             msg_Warn( p_demux, "unknown id=0x%x", p_peek[2] );
277             stream_Read( p_demux->s, NULL, i_size + 8 );
278             break;
279     }
280     return 1;
281 }
282
283 /*****************************************************************************
284  * Control:
285  *****************************************************************************/
286 static int Control( demux_t *p_demux, int i_query, va_list args )
287 {
288     /* demux_sys_t *p_sys = p_demux->p_sys; */
289     double f, *pf;
290     int64_t i64;
291     switch( i_query )
292     {
293         case DEMUX_GET_POSITION:
294             if( ( i64 = stream_Size( p_demux->s ) ) > 0 )
295             {
296                 pf = (double*) va_arg( args, double* );
297                 double current = stream_Tell( p_demux->s );
298                 *pf = current / (double)i64;
299                 return VLC_SUCCESS;
300             }
301             return VLC_EGENERIC;
302
303         case DEMUX_SET_POSITION:
304             f = (double) va_arg( args, double );
305             i64 = stream_Size( p_demux->s );
306
307             if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) || ReSynch( p_demux ) )
308             {
309                 return VLC_EGENERIC;
310             }
311             return VLC_SUCCESS;
312
313 #if 0
314         case DEMUX_GET_TIME:
315             pi64 = (int64_t*)va_arg( args, int64_t * );
316             if( p_sys->i_time < 0 )
317             {
318                 *pi64 = 0;
319                 return VLC_EGENERIC;
320             }
321             *pi64 = p_sys->i_time;
322             return VLC_SUCCESS;
323
324 #if 0
325         case DEMUX_GET_LENGTH:
326             pi64 = (int64_t*)va_arg( args, int64_t * );
327             if( p_sys->i_mux_rate > 0 )
328             {
329                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) / p_sys->i_mux_rate;
330                 return VLC_SUCCESS;
331             }
332             *pi64 = 0;
333             return VLC_EGENERIC;
334
335 #endif
336         case DEMUX_GET_FPS:
337             pf = (double*)va_arg( args, double * );
338             *pf = (double)1000000.0 / (double)p_sys->i_pcr_inc;
339             return VLC_SUCCESS;
340 #endif
341         case DEMUX_SET_TIME:
342         default:
343             return VLC_EGENERIC;
344     }
345 }
346
347 /*****************************************************************************
348  * ReSynch:
349  *****************************************************************************/
350 static int ReSynch( demux_t *p_demux )
351 {
352     for( ;; )
353     {
354         const uint8_t *p_peek;
355         int i_peek = stream_Peek( p_demux->s, &p_peek, 1024 );
356         if( i_peek < 8 )
357             break;
358
359         int i_skip = 0;
360
361         while( i_skip < i_peek - 5 )
362         {
363             if( p_peek[0] == 'A' && p_peek[1] == 'V' && p_peek[4] == 0x55 )
364             {
365                 if( i_skip > 0
366                  && stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
367                     return VLC_EGENERIC;
368                 return VLC_SUCCESS;
369             }
370             p_peek++;
371             i_skip++;
372         }
373
374         if( stream_Read( p_demux->s, NULL, i_skip ) < i_skip )
375             break;
376     }
377
378     return VLC_EGENERIC;
379 }
380
381 static void ParsePES( demux_t *p_demux )
382 {
383     demux_sys_t *p_sys = p_demux->p_sys;
384     block_t     *p_pes = p_sys->p_pes;
385     uint8_t     hdr[30];
386
387     unsigned    i_skip;
388     mtime_t     i_dts = -1;
389     mtime_t     i_pts = -1;
390
391     p_sys->p_pes = NULL;
392
393     /* FIXME find real max size */
394     block_ChainExtract( p_pes, hdr, 30 );
395
396     /* See ยง2.4.3.6 of ISO 13818-1 */
397     if( hdr[0] != 0 || hdr[1] != 0 || hdr[2] != 1 )
398     {
399         msg_Warn( p_demux, "invalid hdr [0x%2.2x:%2.2x:%2.2x:%2.2x]",
400                   hdr[0], hdr[1],hdr[2],hdr[3] );
401         block_ChainRelease( p_pes );
402         return;
403     }
404     // hdr[4] i_pes_size, 2 bytes
405     // hdr[6] Marker -> original_or_copy
406
407     /* we assume mpeg2 PES */
408     i_skip = hdr[8] + 9;
409     if( hdr[7]&0x80 )    /* has pts */
410     {
411         i_pts = ((mtime_t)(hdr[ 9]&0x0e ) << 29)|
412                  (mtime_t)(hdr[10] << 22)|
413                 ((mtime_t)(hdr[11]&0xfe) << 14)|
414                  (mtime_t)(hdr[12] << 7)|
415                  (mtime_t)(hdr[12] >> 1);
416
417         if( hdr[7]&0x40 )    /* has dts */
418         {
419              i_dts = ((mtime_t)(hdr[14]&0x0e ) << 29)|
420                      (mtime_t)(hdr[15] << 22)|
421                     ((mtime_t)(hdr[16]&0xfe) << 14)|
422                      (mtime_t)(hdr[17] << 7)|
423                      (mtime_t)(hdr[18] >> 1);
424         }
425     }
426
427     p_pes = block_ChainGather( p_pes );
428     if( p_pes->i_buffer <= i_skip )
429     {
430         block_ChainRelease( p_pes );
431         return;
432     }
433
434     p_pes->i_buffer -= i_skip;
435     p_pes->p_buffer += i_skip;
436
437     if( i_dts >= 0 )
438         p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
439     if( i_pts >= 0 )
440         p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
441
442     /* Set PCR */
443     if( p_pes->i_pts > 0 )
444     {
445         es_out_Control( p_demux->out, ES_OUT_SET_PCR, (int64_t)p_pes->i_pts);
446         p_sys->b_pcr_audio = true;
447     }
448     es_out_Send( p_demux->out, p_sys->p_audio, p_pes );
449 }
450