]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
D�but du portage BeOS. Beaucoup de fuchiers ont �t� modifi� car il a fallu
[vlc] / src / video_decoder / video_decoder.c
1 /*****************************************************************************
2  * video_decoder.c : video decoder thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *****************************************************************************/
23
24 /* FIXME: passer en terminate/destroy avec les signaux supplémentaires ?? */
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                                /* free() */
30 #include <unistd.h>                                              /* getpid() */
31 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
32 #include <sys/uio.h>                                          /* for input.h */
33
34 #include "threads.h"
35 #include "config.h"
36 #include "common.h"
37 #include "mtime.h"
38 #include "plugins.h"
39
40 #include "intf_msg.h"
41
42 #include "input.h"
43 #include "decoder_fifo.h"
44 #include "video.h"
45 #include "video_output.h"
46
47 #include "vdec_idct.h"
48 #include "video_decoder.h"
49 #include "vdec_motion.h"
50
51 #include "vpar_blocks.h"
52 #include "vpar_headers.h"
53 #include "vpar_synchro.h"
54 #include "video_parser.h"
55 #include "video_fifo.h"
56
57 /*
58  * Local prototypes
59  */
60 #ifdef VDEC_SMP
61 static int      vdec_InitThread     ( vdec_thread_t *p_vdec );
62 static void     vdec_DecodeMacroblock( vdec_thread_t *p_vdec,
63                                        macroblock_t * p_mb );
64 #endif
65 static void     RunThread           ( vdec_thread_t *p_vdec );
66 static void     ErrorThread         ( vdec_thread_t *p_vdec );
67 static void     EndThread           ( vdec_thread_t *p_vdec );
68
69 /*****************************************************************************
70  * vdec_CreateThread: create a video decoder thread
71  *****************************************************************************
72  * This function creates a new video decoder thread, and returns a pointer
73  * to its description. On error, it returns NULL.
74  * Following configuration properties are used:
75  * XXX??
76  *****************************************************************************/
77 vdec_thread_t * vdec_CreateThread( vpar_thread_t *p_vpar /*, int *pi_status */ )
78 {
79     vdec_thread_t *     p_vdec;
80
81     intf_DbgMsg("vdec debug: creating video decoder thread\n");
82
83     /* Allocate the memory needed to store the thread's structure */
84     if ( (p_vdec = (vdec_thread_t *)malloc( sizeof(vdec_thread_t) )) == NULL )
85     {
86         intf_ErrMsg("vdec error: not enough memory for vdec_CreateThread() to create the new thread\n");
87         return( NULL );
88     }
89
90     /*
91      * Initialize the thread properties
92      */
93     p_vdec->b_die = 0;
94     p_vdec->b_error = 0;
95
96     /*
97      * Initialize the parser properties
98      */
99     p_vdec->p_vpar = p_vpar;
100
101     /* Spawn the video decoder thread */
102     if ( vlc_thread_create(&p_vdec->thread_id, "video decoder",
103          (vlc_thread_func_t)RunThread, (void *)p_vdec) )
104     {
105         intf_ErrMsg("vdec error: can't spawn video decoder thread\n");
106         free( p_vdec );
107         return( NULL );
108     }
109
110     intf_DbgMsg("vdec debug: video decoder thread (%p) created\n", p_vdec);
111     return( p_vdec );
112 }
113
114 /*****************************************************************************
115  * vdec_DestroyThread: destroy a video decoder thread
116  *****************************************************************************
117  * Destroy and terminate thread. This function will return 0 if the thread could
118  * be destroyed, and non 0 else. The last case probably means that the thread
119  * was still active, and another try may succeed.
120  *****************************************************************************/
121 void vdec_DestroyThread( vdec_thread_t *p_vdec /*, int *pi_status */ )
122 {
123     intf_DbgMsg("vdec debug: requesting termination of video decoder thread %p\n", p_vdec);
124
125     /* Ask thread to kill itself */
126     p_vdec->b_die = 1;
127
128 #ifdef VDEC_SMP
129     /* Make sure the decoder thread leaves the vpar_GetMacroblock() function */
130     vlc_mutex_lock( &(p_vdec->p_vpar->vfifo.lock) );
131     vlc_cond_signal( &(p_vdec->p_vpar->vfifo.wait) );
132     vlc_mutex_unlock( &(p_vdec->p_vpar->vfifo.lock) );
133 #endif
134
135     /* Waiting for the decoder thread to exit */
136     /* Remove this as soon as the "status" flag is implemented */
137     vlc_thread_join( p_vdec->thread_id );
138 }
139
140 /* following functions are local */
141
142 /*****************************************************************************
143  * vdec_InitThread: initialize video decoder thread
144  *****************************************************************************
145  * This function is called from RunThread and performs the second step of the
146  * initialization. It returns 0 on success. Note that the thread's flag are not
147  * modified inside this function.
148  *****************************************************************************/
149 #ifdef VDEC_SMP
150 static int vdec_InitThread( vdec_thread_t *p_vdec )
151 #else
152 int vdec_InitThread( vdec_thread_t *p_vdec )
153 #endif
154 {
155     int i_dummy;
156
157     intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
158
159     /* Initialize other properties */
160 #ifdef STATS
161     p_vdec->c_loops = 0;
162     p_vdec->c_idle_loops = 0;
163     p_vdec->c_decoded_pictures = 0;
164     p_vdec->c_decoded_i_pictures = 0;
165     p_vdec->c_decoded_p_pictures = 0;
166     p_vdec->c_decoded_b_pictures = 0;
167 #endif
168
169     /* Init crop table */
170     p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
171     for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
172     {
173         p_vdec->pi_crop[i_dummy] = 0;
174     }
175     for( ; i_dummy < 255; i_dummy ++ )
176     {
177         p_vdec->pi_crop[i_dummy] = i_dummy;
178     }
179     for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
180     {
181         p_vdec->pi_crop[i_dummy] = 255;
182     }
183
184     /* Mark thread as running and return */
185     intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
186     return( 0 );
187 }
188
189 /*****************************************************************************
190  * ErrorThread: RunThread() error loop
191  *****************************************************************************
192  * This function is called when an error occured during thread main's loop. The
193  * thread can still receive feed, but must be ready to terminate as soon as
194  * possible.
195  *****************************************************************************/
196 static void ErrorThread( vdec_thread_t *p_vdec )
197 {
198     macroblock_t *       p_mb;
199
200     /* Wait until a `die' order */
201     while( !p_vdec->b_die )
202     {
203         p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
204         vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
205     }
206 }
207
208 /*****************************************************************************
209  * EndThread: thread destruction
210  *****************************************************************************
211  * This function is called when the thread ends after a sucessfull
212  * initialization.
213  *****************************************************************************/
214 static void EndThread( vdec_thread_t *p_vdec )
215 {
216     intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
217 }
218
219 /*****************************************************************************
220  * AddBlock : add a block
221  *****************************************************************************/
222 #ifndef HAVE_MMX
223 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
224                                  yuv_data_t * p_data, int i_incr )
225 {
226     int i_x, i_y;
227
228     for( i_y = 0; i_y < 8; i_y++ )
229     {
230         for( i_x = 0; i_x < 8; i_x++ )
231         {
232             *p_data = p_vdec->pi_crop[*p_data + *p_block++];
233             p_data++;
234         }
235         p_data += i_incr;
236     }
237 }
238 #else
239 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
240                                           yuv_data_t * p_data, int i_incr )
241 {
242     asm __volatile__ (
243             "pxor       %%mm7,%%mm7\n\t"
244
245             "movq       (%0),%%mm1\n\t"
246             "movq       %%mm1,%%mm2\n\t"
247             "punpckhbw  %%mm7,%%mm1\n\t"
248             "punpcklbw  %%mm7,%%mm2\n\t"
249             "paddw      (%1),%%mm2\n\t"
250             "paddw      8(%1),%%mm1\n\t"
251             "packuswb   %%mm1,%%mm2\n\t"
252             "movq       %%mm2,(%0)\n\t"
253             "addl       %2,%0\n\t"
254
255             "movq       (%0),%%mm1\n\t"
256             "movq       %%mm1,%%mm2\n\t"
257             "punpckhbw  %%mm7,%%mm1\n\t"
258             "punpcklbw  %%mm7,%%mm2\n\t"
259             "paddw      16(%1),%%mm2\n\t"
260             "paddw      24(%1),%%mm1\n\t"
261             "packuswb   %%mm1,%%mm2\n\t"
262             "movq       %%mm2,(%0)\n\t"
263             "addl       %2,%0\n\t"
264
265             "movq       (%0),%%mm1\n\t"
266             "movq       %%mm1,%%mm2\n\t"
267             "punpckhbw  %%mm7,%%mm1\n\t"
268             "punpcklbw  %%mm7,%%mm2\n\t"
269             "paddw      32(%1),%%mm2\n\t"
270             "paddw      40(%1),%%mm1\n\t"
271             "packuswb   %%mm1,%%mm2\n\t"
272             "movq       %%mm2,(%0)\n\t"
273             "addl       %2,%0\n\t"
274
275             "movq       (%0),%%mm1\n\t"
276             "movq       %%mm1,%%mm2\n\t"
277             "punpckhbw  %%mm7,%%mm1\n\t"
278             "punpcklbw  %%mm7,%%mm2\n\t"
279             "paddw      48(%1),%%mm2\n\t"
280             "paddw      56(%1),%%mm1\n\t"
281             "packuswb   %%mm1,%%mm2\n\t"
282             "movq       %%mm2,(%0)\n\t"
283             "addl       %2,%0\n\t"
284
285             "movq       (%0),%%mm1\n\t"
286             "movq       %%mm1,%%mm2\n\t"
287             "punpckhbw  %%mm7,%%mm1\n\t"
288             "punpcklbw  %%mm7,%%mm2\n\t"
289             "paddw      64(%1),%%mm2\n\t"
290             "paddw      72(%1),%%mm1\n\t"
291             "packuswb   %%mm1,%%mm2\n\t"
292             "movq       %%mm2,(%0)\n\t"
293             "addl       %2,%0\n\t"
294
295             "movq       (%0),%%mm1\n\t"
296             "movq       %%mm1,%%mm2\n\t"
297             "punpckhbw  %%mm7,%%mm1\n\t"
298             "punpcklbw  %%mm7,%%mm2\n\t"
299             "paddw      80(%1),%%mm2\n\t"
300             "paddw      88(%1),%%mm1\n\t"
301             "packuswb   %%mm1,%%mm2\n\t"
302             "movq       %%mm2,(%0)\n\t"
303             "addl       %2,%0\n\t"
304
305             "movq       (%0),%%mm1\n\t"
306             "movq       %%mm1,%%mm2\n\t"
307             "punpckhbw  %%mm7,%%mm1\n\t"
308             "punpcklbw  %%mm7,%%mm2\n\t"
309             "paddw      96(%1),%%mm2\n\t"
310             "paddw      104(%1),%%mm1\n\t"
311             "packuswb   %%mm1,%%mm2\n\t"
312             "movq       %%mm2,(%0)\n\t"
313             "addl       %2,%0\n\t"
314
315             "movq       (%0),%%mm1\n\t"
316             "movq       %%mm1,%%mm2\n\t"
317             "punpckhbw  %%mm7,%%mm1\n\t"
318             "punpcklbw  %%mm7,%%mm2\n\t"
319             "paddw      112(%1),%%mm2\n\t"
320             "paddw      120(%1),%%mm1\n\t"
321             "packuswb   %%mm1,%%mm2\n\t"
322             "movq       %%mm2,(%0)\n\t"
323
324             "emms"
325              :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
326 }
327 #endif
328
329
330 /*****************************************************************************
331  * CopyBlock : copy a block
332  *****************************************************************************/
333 #ifndef HAVE_MMX
334 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
335                                   yuv_data_t * p_data, int i_incr )
336 {
337     int i_x, i_y;
338
339     for( i_y = 0; i_y < 8; i_y++ )
340     {
341         for( i_x = 0; i_x < 8; i_x++ )
342         {
343             *p_data++ = p_vdec->pi_crop[*p_block++];
344         }
345         p_data += i_incr;
346     }
347 }
348 #else
349 static  __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
350                                           yuv_data_t * p_data, int i_incr )
351 {
352     asm __volatile__ (
353             "movq         (%1),%%mm0\n\t"
354             "packuswb   8(%1),%%mm0\n\t"
355             "movq        %%mm0,(%0)\n\t"
356             "addl           %2,%0\n\t"
357
358             "movq        16(%1),%%mm0\n\t"
359             "packuswb   24(%1),%%mm0\n\t"
360             "movq        %%mm0,(%0)\n\t"
361             "addl           %2,%0\n\t"
362
363             "movq        32(%1),%%mm0\n\t"
364             "packuswb   40(%1),%%mm0\n\t"
365             "movq        %%mm0,(%0)\n\t"
366             "addl           %2,%0\n\t"
367
368             "movq        48(%1),%%mm0\n\t"
369             "packuswb   56(%1),%%mm0\n\t"
370             "movq        %%mm0,(%0)\n\t"
371             "addl           %2,%0\n\t"
372
373             "movq        64(%1),%%mm0\n\t"
374             "packuswb   72(%1),%%mm0\n\t"
375             "movq        %%mm0,(%0)\n\t"
376             "addl           %2,%0\n\t"
377
378             "movq        80(%1),%%mm0\n\t"
379             "packuswb   88(%1),%%mm0\n\t"
380             "movq        %%mm0,(%0)\n\t"
381             "addl           %2,%0\n\t"
382
383             "movq        96(%1),%%mm0\n\t"
384             "packuswb   104(%1),%%mm0\n\t"
385             "movq        %%mm0,(%0)\n\t"
386             "addl           %2,%0\n\t"
387
388             "movq        112(%1),%%mm0\n\t"
389             "packuswb   120(%1),%%mm0\n\t"
390             "movq        %%mm0,(%0)\n\t"
391             "emms"
392             :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
393 }
394 #endif
395
396
397 /*****************************************************************************
398  * vdec_DecodeMacroblock : decode a macroblock of a picture
399  *****************************************************************************/
400 #define DECODEBLOCKS( OPBLOCK )                                         \
401 {                                                                       \
402     int             i_b, i_mask;                                        \
403                                                                         \
404     i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks);                       \
405                                                                         \
406     /* luminance */                                                     \
407     for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 )                        \
408     {                                                                   \
409         if( p_mb->i_coded_block_pattern & i_mask )                      \
410         {                                                               \
411             /*                                                          \
412              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
413              */                                                         \
414             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
415                                   p_mb->pi_sparse_pos[i_b] );           \
416                                                                         \
417             /*                                                          \
418              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
419              * section 7.6.8)                                           \
420              */                                                         \
421             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
422                      p_mb->p_data[i_b], p_mb->i_addb_l_stride );        \
423         }                                                               \
424     }                                                                   \
425                                                                         \
426     /* chrominance */                                                   \
427     for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks;                   \
428          i_b++, i_mask >>= 1 )                                          \
429     {                                                                   \
430         if( p_mb->i_coded_block_pattern & i_mask )                      \
431         {                                                               \
432             /*                                                          \
433              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
434              */                                                         \
435             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
436                                   p_mb->pi_sparse_pos[i_b] );           \
437                                                                         \
438             /*                                                          \
439              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
440              * section 7.6.8)                                           \
441              */                                                         \
442             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
443                      p_mb->p_data[i_b], p_mb->i_addb_c_stride );        \
444         }                                                               \
445     }                                                                   \
446 }
447
448 #ifdef VDEC_SMP
449 static __inline__ void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
450 #else
451 void vdec_DecodeMacroblock( vdec_thread_t *p_vdec, macroblock_t * p_mb )
452 #endif
453 {
454     if( !(p_mb->i_mb_type & MB_INTRA) )
455     {
456         /*
457          * Motion Compensation (ISO/IEC 13818-2 section 7.6)
458          */
459         if( p_mb->pf_motion == 0 )
460         {
461             intf_ErrMsg( "vdec error: pf_motion set to NULL\n" );
462         }
463         else
464         {
465             p_mb->pf_motion( p_mb );
466         }
467
468         DECODEBLOCKS( AddBlock )
469     }
470     else
471     {
472         DECODEBLOCKS( CopyBlock )
473     }
474
475     /*
476      * Decoding is finished, release the macroblock and free
477      * unneeded memory.
478      */
479     vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
480 }
481
482
483 /*****************************************************************************
484  * RunThread: video decoder thread
485  *****************************************************************************
486  * Video decoder thread. This function does only return when the thread is
487  * terminated.
488  *****************************************************************************/
489 static void RunThread( vdec_thread_t *p_vdec )
490 {
491     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
492                 p_vdec, getpid());
493
494     /*
495      * Initialize thread and free configuration
496      */
497     p_vdec->b_error = vdec_InitThread( p_vdec );
498     if( p_vdec->b_error )
499     {
500         return;
501     }
502     p_vdec->b_run = 1;
503
504     /*
505      * Main loop - it is not executed if an error occured during
506      * initialization
507      */
508     while( (!p_vdec->b_die) && (!p_vdec->b_error) )
509     {
510         macroblock_t *          p_mb;
511
512         if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
513         {
514             vdec_DecodeMacroblock( p_vdec, p_mb );
515         }
516     }
517
518     /*
519      * Error loop
520      */
521     if( p_vdec->b_error )
522     {
523         ErrorThread( p_vdec );
524     }
525
526     /* End of thread */
527     EndThread( p_vdec );
528     p_vdec->b_run = 0;
529 }