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