]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
* Moved video_decoder's headers from include/ to src/video_decoder.
[vlc] / src / video_decoder / video_decoder.c
1 /*****************************************************************************
2  * video_decoder.c : video decoder thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: video_decoder.c,v 1.41 2000/12/21 17:19:52 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          GaĆ«l Hendryckx <jimmy@via.ecp.fr>
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 "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 "stream_control.h"
44 #include "input_ext-dec.h"
45
46 #include "video.h"
47 #include "video_output.h"
48
49 #include "vdec_idct.h"
50 #include "video_decoder.h"
51 #include "vdec_motion.h"
52
53 #include "vpar_blocks.h"
54 #include "vpar_headers.h"
55 #include "vpar_synchro.h"
56 #include "video_parser.h"
57 #include "video_fifo.h"
58
59 /*
60  * Local prototypes
61  */
62 #ifdef VDEC_SMP
63 static int      vdec_InitThread     ( vdec_thread_t *p_vdec );
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 #ifndef HAVE_MMX
156     int i_dummy;
157 #endif
158
159     intf_DbgMsg("vdec debug: initializing video decoder thread %p\n", p_vdec);
160
161 #ifndef HAVE_MMX
162     /* Init crop table */
163     p_vdec->pi_crop = p_vdec->pi_crop_buf + (VDEC_CROPRANGE >> 1);
164     for( i_dummy = -(VDEC_CROPRANGE >> 1); i_dummy < 0; i_dummy++ )
165     {
166         p_vdec->pi_crop[i_dummy] = 0;
167     }
168     for( ; i_dummy < 255; i_dummy ++ )
169     {
170         p_vdec->pi_crop[i_dummy] = i_dummy;
171     }
172     for( ; i_dummy < (VDEC_CROPRANGE >> 1) -1; i_dummy++ )
173     {
174         p_vdec->pi_crop[i_dummy] = 255;
175     }
176 #endif
177
178 #ifdef VDEC_SMP
179     /* Re-nice ourself */
180     if( nice(VDEC_NICE) == -1 )
181     {
182         intf_WarnMsg( 2, "vdec warning : couldn't nice() (%s)\n",
183                       strerror(errno) );
184     }
185 #endif
186
187     /* Mark thread as running and return */
188     intf_DbgMsg("vdec debug: InitThread(%p) succeeded\n", p_vdec);
189     return( 0 );
190 }
191
192 /*****************************************************************************
193  * ErrorThread: RunThread() error loop
194  *****************************************************************************
195  * This function is called when an error occured during thread main's loop. The
196  * thread can still receive feed, but must be ready to terminate as soon as
197  * possible.
198  *****************************************************************************/
199 static void ErrorThread( vdec_thread_t *p_vdec )
200 {
201     macroblock_t *       p_mb;
202
203     /* Wait until a `die' order */
204     while( !p_vdec->b_die )
205     {
206         p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo );
207         vpar_DestroyMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
208     }
209 }
210
211 /*****************************************************************************
212  * EndThread: thread destruction
213  *****************************************************************************
214  * This function is called when the thread ends after a sucessful
215  * initialization.
216  *****************************************************************************/
217 static void EndThread( vdec_thread_t *p_vdec )
218 {
219     intf_DbgMsg("vdec debug: EndThread(%p)\n", p_vdec);
220 }
221
222 /*****************************************************************************
223  * AddBlock : add a block
224  *****************************************************************************/
225 #ifndef HAVE_MMX
226 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
227                                  yuv_data_t * p_data, int i_incr )
228 {
229     int i_x, i_y;
230
231     for( i_y = 0; i_y < 8; i_y++ )
232     {
233         for( i_x = 0; i_x < 8; i_x++ )
234         {
235             *p_data = p_vdec->pi_crop[*p_data + *p_block++];
236             p_data++;
237         }
238         p_data += i_incr;
239     }
240 }
241 #else
242 static __inline__ void AddBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
243                                           yuv_data_t * p_data, int i_incr )
244 {
245     asm __volatile__ (
246             "pxor       %%mm7,%%mm7\n\t"
247
248             "movq       (%0),%%mm1\n\t"
249             "movq       %%mm1,%%mm2\n\t"
250             "punpckhbw  %%mm7,%%mm1\n\t"
251             "punpcklbw  %%mm7,%%mm2\n\t"
252             "paddw      (%1),%%mm2\n\t"
253             "paddw      8(%1),%%mm1\n\t"
254             "packuswb   %%mm1,%%mm2\n\t"
255             "movq       %%mm2,(%0)\n\t"
256             "addl       %2,%0\n\t"
257
258             "movq       (%0),%%mm1\n\t"
259             "movq       %%mm1,%%mm2\n\t"
260             "punpckhbw  %%mm7,%%mm1\n\t"
261             "punpcklbw  %%mm7,%%mm2\n\t"
262             "paddw      16(%1),%%mm2\n\t"
263             "paddw      24(%1),%%mm1\n\t"
264             "packuswb   %%mm1,%%mm2\n\t"
265             "movq       %%mm2,(%0)\n\t"
266             "addl       %2,%0\n\t"
267
268             "movq       (%0),%%mm1\n\t"
269             "movq       %%mm1,%%mm2\n\t"
270             "punpckhbw  %%mm7,%%mm1\n\t"
271             "punpcklbw  %%mm7,%%mm2\n\t"
272             "paddw      32(%1),%%mm2\n\t"
273             "paddw      40(%1),%%mm1\n\t"
274             "packuswb   %%mm1,%%mm2\n\t"
275             "movq       %%mm2,(%0)\n\t"
276             "addl       %2,%0\n\t"
277
278             "movq       (%0),%%mm1\n\t"
279             "movq       %%mm1,%%mm2\n\t"
280             "punpckhbw  %%mm7,%%mm1\n\t"
281             "punpcklbw  %%mm7,%%mm2\n\t"
282             "paddw      48(%1),%%mm2\n\t"
283             "paddw      56(%1),%%mm1\n\t"
284             "packuswb   %%mm1,%%mm2\n\t"
285             "movq       %%mm2,(%0)\n\t"
286             "addl       %2,%0\n\t"
287
288             "movq       (%0),%%mm1\n\t"
289             "movq       %%mm1,%%mm2\n\t"
290             "punpckhbw  %%mm7,%%mm1\n\t"
291             "punpcklbw  %%mm7,%%mm2\n\t"
292             "paddw      64(%1),%%mm2\n\t"
293             "paddw      72(%1),%%mm1\n\t"
294             "packuswb   %%mm1,%%mm2\n\t"
295             "movq       %%mm2,(%0)\n\t"
296             "addl       %2,%0\n\t"
297
298             "movq       (%0),%%mm1\n\t"
299             "movq       %%mm1,%%mm2\n\t"
300             "punpckhbw  %%mm7,%%mm1\n\t"
301             "punpcklbw  %%mm7,%%mm2\n\t"
302             "paddw      80(%1),%%mm2\n\t"
303             "paddw      88(%1),%%mm1\n\t"
304             "packuswb   %%mm1,%%mm2\n\t"
305             "movq       %%mm2,(%0)\n\t"
306             "addl       %2,%0\n\t"
307
308             "movq       (%0),%%mm1\n\t"
309             "movq       %%mm1,%%mm2\n\t"
310             "punpckhbw  %%mm7,%%mm1\n\t"
311             "punpcklbw  %%mm7,%%mm2\n\t"
312             "paddw      96(%1),%%mm2\n\t"
313             "paddw      104(%1),%%mm1\n\t"
314             "packuswb   %%mm1,%%mm2\n\t"
315             "movq       %%mm2,(%0)\n\t"
316             "addl       %2,%0\n\t"
317
318             "movq       (%0),%%mm1\n\t"
319             "movq       %%mm1,%%mm2\n\t"
320             "punpckhbw  %%mm7,%%mm1\n\t"
321             "punpcklbw  %%mm7,%%mm2\n\t"
322             "paddw      112(%1),%%mm2\n\t"
323             "paddw      120(%1),%%mm1\n\t"
324             "packuswb   %%mm1,%%mm2\n\t"
325             "movq       %%mm2,(%0)\n\t"
326
327             //"emms"
328             :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
329 }
330 #endif
331
332
333 /*****************************************************************************
334  * CopyBlock : copy a block
335  *****************************************************************************/
336 #ifndef HAVE_MMX
337 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
338                                   yuv_data_t * p_data, int i_incr )
339 {
340     int i_x, i_y;
341
342     for( i_y = 0; i_y < 8; i_y++ )
343     {
344         for( i_x = 0; i_x < 8; i_x++ )
345         {
346             *p_data++ = p_vdec->pi_crop[*p_block++];
347         }
348         p_data += i_incr;
349     }
350 }
351 #else
352 static  __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
353                                           yuv_data_t * p_data, int i_incr )
354 {
355     asm __volatile__ (
356             "movq         (%1),%%mm0\n\t"
357             "packuswb   8(%1),%%mm0\n\t"
358             "movq        %%mm0,(%0)\n\t"
359             "addl           %2,%0\n\t"
360
361             "movq        16(%1),%%mm0\n\t"
362             "packuswb   24(%1),%%mm0\n\t"
363             "movq        %%mm0,(%0)\n\t"
364             "addl           %2,%0\n\t"
365
366             "movq        32(%1),%%mm0\n\t"
367             "packuswb   40(%1),%%mm0\n\t"
368             "movq        %%mm0,(%0)\n\t"
369             "addl           %2,%0\n\t"
370
371             "movq        48(%1),%%mm0\n\t"
372             "packuswb   56(%1),%%mm0\n\t"
373             "movq        %%mm0,(%0)\n\t"
374             "addl           %2,%0\n\t"
375
376             "movq        64(%1),%%mm0\n\t"
377             "packuswb   72(%1),%%mm0\n\t"
378             "movq        %%mm0,(%0)\n\t"
379             "addl           %2,%0\n\t"
380
381             "movq        80(%1),%%mm0\n\t"
382             "packuswb   88(%1),%%mm0\n\t"
383             "movq        %%mm0,(%0)\n\t"
384             "addl           %2,%0\n\t"
385
386             "movq        96(%1),%%mm0\n\t"
387             "packuswb   104(%1),%%mm0\n\t"
388             "movq        %%mm0,(%0)\n\t"
389             "addl           %2,%0\n\t"
390
391             "movq        112(%1),%%mm0\n\t"
392             "packuswb   120(%1),%%mm0\n\t"
393             "movq        %%mm0,(%0)\n\t"
394             //"emms"
395             :"+r" (p_data): "r" (p_block),"r" (i_incr+8));
396 }
397 #endif
398
399
400 /*****************************************************************************
401  * vdec_DecodeMacroblock : decode a macroblock of a picture
402  *****************************************************************************/
403 #define DECODEBLOCKSC( OPBLOCK )                                        \
404 {                                                                       \
405     int             i_b, i_mask;                                        \
406                                                                         \
407     i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks);                       \
408                                                                         \
409     /* luminance */                                                     \
410     for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 )                        \
411     {                                                                   \
412         if( p_mb->i_coded_block_pattern & i_mask )                      \
413         {                                                               \
414             /*                                                          \
415              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
416              */                                                         \
417             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
418                                   p_mb->pi_sparse_pos[i_b] );           \
419                                                                         \
420             /*                                                          \
421              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
422              * section 7.6.8)                                           \
423              */                                                         \
424             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
425                      p_mb->p_data[i_b], p_mb->i_addb_l_stride );        \
426         }                                                               \
427     }                                                                   \
428                                                                         \
429     /* chrominance */                                                   \
430     for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks;                   \
431          i_b++, i_mask >>= 1 )                                          \
432     {                                                                   \
433         if( p_mb->i_coded_block_pattern & i_mask )                      \
434         {                                                               \
435             /*                                                          \
436              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
437              */                                                         \
438             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
439                                   p_mb->pi_sparse_pos[i_b] );           \
440                                                                         \
441             /*                                                          \
442              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
443              * section 7.6.8)                                           \
444              */                                                         \
445             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
446                      p_mb->p_data[i_b], p_mb->i_addb_c_stride );        \
447         }                                                               \
448     }                                                                   \
449 }
450
451 #define DECODEBLOCKSBW( OPBLOCK )                                       \
452 {                                                                       \
453     int             i_b, i_mask;                                        \
454                                                                         \
455     i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks);                       \
456                                                                         \
457     /* luminance */                                                     \
458     for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 )                        \
459     {                                                                   \
460         if( p_mb->i_coded_block_pattern & i_mask )                      \
461         {                                                               \
462             /*                                                          \
463              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
464              */                                                         \
465             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
466                                   p_mb->pi_sparse_pos[i_b] );           \
467                                                                         \
468             /*                                                          \
469              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
470              * section 7.6.8)                                           \
471              */                                                         \
472             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
473                      p_mb->p_data[i_b], p_mb->i_addb_l_stride );        \
474         }                                                               \
475     }                                                                   \
476 }
477
478 void vdec_DecodeMacroblockC ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
479 {
480     if( !(p_mb->i_mb_type & MB_INTRA) )
481     {
482         /*
483          * Motion Compensation (ISO/IEC 13818-2 section 7.6)
484          */
485         if( p_mb->pf_motion == 0 )
486         {
487             intf_ErrMsg( "vdec error: pf_motion set to NULL\n" );
488         }
489         else
490         {
491             p_mb->pf_motion( p_mb );
492         }
493
494         DECODEBLOCKSC( AddBlock )
495     }
496     else
497     {
498         DECODEBLOCKSC( CopyBlock )
499     }
500
501     /*
502      * Decoding is finished, release the macroblock and free
503      * unneeded memory.
504      */
505     vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
506 }
507
508 void vdec_DecodeMacroblockBW ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
509 {
510     if( !(p_mb->i_mb_type & MB_INTRA) )
511     {
512         /*
513          * Motion Compensation (ISO/IEC 13818-2 section 7.6)
514          */
515         if( p_mb->pf_motion == 0 )
516         {
517             intf_ErrMsg( "vdec error: pf_motion set to NULL\n" );
518         }
519         else
520         {
521             p_mb->pf_motion( p_mb );
522         }
523
524         DECODEBLOCKSBW( AddBlock )
525     }
526     else
527     {
528         DECODEBLOCKSBW( CopyBlock )
529     }
530
531     /*
532      * Decoding is finished, release the macroblock and free
533      * unneeded memory.
534      */
535     vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
536 }
537
538
539
540 /*****************************************************************************
541  * RunThread: video decoder thread
542  *****************************************************************************
543  * Video decoder thread. This function does only return when the thread is
544  * terminated.
545  *****************************************************************************/
546 static void RunThread( vdec_thread_t *p_vdec )
547 {
548     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)\n",
549                 p_vdec, getpid());
550
551     /*
552      * Initialize thread and free configuration
553      */
554     p_vdec->b_error = vdec_InitThread( p_vdec );
555     if( p_vdec->b_error )
556     {
557         return;
558     }
559     p_vdec->b_run = 1;
560
561     /*
562      * Main loop - it is not executed if an error occured during
563      * initialization
564      */
565     while( (!p_vdec->b_die) && (!p_vdec->b_error) )
566     {
567         macroblock_t *          p_mb;
568
569         if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
570         {
571             vdec_DecodeMacroblockC ( p_vdec, p_mb );
572         }
573     }
574
575     /*
576      * Error loop
577      */
578     if( p_vdec->b_error )
579     {
580         ErrorThread( p_vdec );
581     }
582
583     /* End of thread */
584     EndThread( p_vdec );
585     p_vdec->b_run = 0;
586 }