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