]> git.sesse.net Git - vlc/blob - src/video_decoder/video_decoder.c
34df5a820ec268fa9ac0fb6dc95f5f4df0abcbb1
[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.48 2001/04/06 09:15:48 sam 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 <unistd.h>                                              /* getpid() */
31
32 #include <stdlib.h>                                                /* free() */
33 #include <string.h>                                    /* memcpy(), memset() */
34 #include <errno.h>                                                  /* errno */
35
36 #include "config.h"
37 #include "common.h"
38 #include "threads.h"
39 #include "mtime.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_motion.h"
50 #include "vdec_idct.h"
51 #include "video_decoder.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");
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");
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");
106         free( p_vdec );
107         return( NULL );
108     }
109
110     intf_DbgMsg("vdec debug: video decoder thread (%p) created", 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", 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", 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)",
183                       strerror(errno) );
184     }
185 #endif
186
187     /* Mark thread as running and return */
188     intf_DbgMsg("vdec debug: InitThread(%p) succeeded", 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)", 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      (%2),%%mm2\n\t"
253             "paddw      8(%2),%%mm1\n\t"
254             "packuswb   %%mm1,%%mm2\n\t"
255             "movq       %%mm2,(%0)\n\t"
256             "addl       %3,%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(%2),%%mm2\n\t"
263             "paddw      24(%2),%%mm1\n\t"
264             "packuswb   %%mm1,%%mm2\n\t"
265             "movq       %%mm2,(%0)\n\t"
266             "addl       %3,%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(%2),%%mm2\n\t"
273             "paddw      40(%2),%%mm1\n\t"
274             "packuswb   %%mm1,%%mm2\n\t"
275             "movq       %%mm2,(%0)\n\t"
276             "addl       %3,%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(%2),%%mm2\n\t"
283             "paddw      56(%2),%%mm1\n\t"
284             "packuswb   %%mm1,%%mm2\n\t"
285             "movq       %%mm2,(%0)\n\t"
286             "addl       %3,%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(%2),%%mm2\n\t"
293             "paddw      72(%2),%%mm1\n\t"
294             "packuswb   %%mm1,%%mm2\n\t"
295             "movq       %%mm2,(%0)\n\t"
296             "addl       %3,%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(%2),%%mm2\n\t"
303             "paddw      88(%2),%%mm1\n\t"
304             "packuswb   %%mm1,%%mm2\n\t"
305             "movq       %%mm2,(%0)\n\t"
306             "addl       %3,%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(%2),%%mm2\n\t"
313             "paddw      104(%2),%%mm1\n\t"
314             "packuswb   %%mm1,%%mm2\n\t"
315             "movq       %%mm2,(%0)\n\t"
316             "addl       %3,%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(%2),%%mm2\n\t"
323             "paddw      120(%2),%%mm1\n\t"
324             "packuswb   %%mm1,%%mm2\n\t"
325             "movq       %%mm2,(%0)\n\t"
326
327             //"emms"
328             : "=r" (p_data)
329             : "0" (p_data), "r" (p_block), "r" (i_incr + 8) );
330 }
331 #endif
332
333
334 /*****************************************************************************
335  * CopyBlock : copy a block
336  *****************************************************************************/
337 #ifndef HAVE_MMX
338 static __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
339                                   yuv_data_t * p_data, int i_incr )
340 {
341     int i_x, i_y;
342
343     for( i_y = 0; i_y < 8; i_y++ )
344     {
345         for( i_x = 0; i_x < 8; i_x++ )
346         {
347             *p_data++ = p_vdec->pi_crop[*p_block++];
348         }
349         p_data += i_incr;
350     }
351 }
352 #else
353 static  __inline__ void CopyBlock( vdec_thread_t * p_vdec, dctelem_t * p_block,
354                                    yuv_data_t * p_data, int i_incr )
355 {
356     asm __volatile__ (
357             "movq         (%2),%%mm0\n\t"
358             "packuswb   8(%2),%%mm0\n\t"
359             "movq        %%mm0,(%0)\n\t"
360             "addl           %3,%0\n\t"
361
362             "movq        16(%2),%%mm0\n\t"
363             "packuswb   24(%2),%%mm0\n\t"
364             "movq        %%mm0,(%0)\n\t"
365             "addl           %3,%0\n\t"
366
367             "movq        32(%2),%%mm0\n\t"
368             "packuswb   40(%2),%%mm0\n\t"
369             "movq        %%mm0,(%0)\n\t"
370             "addl           %3,%0\n\t"
371
372             "movq        48(%2),%%mm0\n\t"
373             "packuswb   56(%2),%%mm0\n\t"
374             "movq        %%mm0,(%0)\n\t"
375             "addl           %3,%0\n\t"
376
377             "movq        64(%2),%%mm0\n\t"
378             "packuswb   72(%2),%%mm0\n\t"
379             "movq        %%mm0,(%0)\n\t"
380             "addl           %3,%0\n\t"
381
382             "movq        80(%2),%%mm0\n\t"
383             "packuswb   88(%2),%%mm0\n\t"
384             "movq        %%mm0,(%0)\n\t"
385             "addl           %3,%0\n\t"
386
387             "movq        96(%2),%%mm0\n\t"
388             "packuswb   104(%2),%%mm0\n\t"
389             "movq        %%mm0,(%0)\n\t"
390             "addl           %3,%0\n\t"
391
392             "movq        112(%2),%%mm0\n\t"
393             "packuswb   120(%2),%%mm0\n\t"
394             "movq        %%mm0,(%0)\n\t"
395
396             //"emms"
397             : "=r" (p_data)
398             : "0" (p_data), "r" (p_block), "r" (i_incr + 8) );
399 }
400 #endif
401
402
403 /*****************************************************************************
404  * vdec_DecodeMacroblock : decode a macroblock of a picture
405  *****************************************************************************/
406 #define DECODEBLOCKSC( OPBLOCK )                                        \
407 {                                                                       \
408     int             i_b, i_mask;                                        \
409                                                                         \
410     i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks);                       \
411                                                                         \
412     /* luminance */                                                     \
413     for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 )                        \
414     {                                                                   \
415         if( p_mb->i_coded_block_pattern & i_mask )                      \
416         {                                                               \
417             /*                                                          \
418              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
419              */                                                         \
420             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
421                                   p_mb->pi_sparse_pos[i_b] );           \
422                                                                         \
423             /*                                                          \
424              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
425              * section 7.6.8)                                           \
426              */                                                         \
427             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
428                      p_mb->p_data[i_b], p_mb->i_addb_l_stride );        \
429         }                                                               \
430     }                                                                   \
431                                                                         \
432     /* chrominance */                                                   \
433     for( i_b = 4; i_b < 4 + p_mb->i_chroma_nb_blocks;                   \
434          i_b++, i_mask >>= 1 )                                          \
435     {                                                                   \
436         if( p_mb->i_coded_block_pattern & i_mask )                      \
437         {                                                               \
438             /*                                                          \
439              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
440              */                                                         \
441             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
442                                   p_mb->pi_sparse_pos[i_b] );           \
443                                                                         \
444             /*                                                          \
445              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
446              * section 7.6.8)                                           \
447              */                                                         \
448             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
449                      p_mb->p_data[i_b], p_mb->i_addb_c_stride );        \
450         }                                                               \
451     }                                                                   \
452 }
453
454 #define DECODEBLOCKSBW( OPBLOCK )                                       \
455 {                                                                       \
456     int             i_b, i_mask;                                        \
457                                                                         \
458     i_mask = 1 << (3 + p_mb->i_chroma_nb_blocks);                       \
459                                                                         \
460     /* luminance */                                                     \
461     for( i_b = 0; i_b < 4; i_b++, i_mask >>= 1 )                        \
462     {                                                                   \
463         if( p_mb->i_coded_block_pattern & i_mask )                      \
464         {                                                               \
465             /*                                                          \
466              * Inverse DCT (ISO/IEC 13818-2 section Annex A)            \
467              */                                                         \
468             (p_mb->pf_idct[i_b])( p_vdec, p_mb->ppi_blocks[i_b],        \
469                                   p_mb->pi_sparse_pos[i_b] );           \
470                                                                         \
471             /*                                                          \
472              * Adding prediction and coefficient data (ISO/IEC 13818-2  \
473              * section 7.6.8)                                           \
474              */                                                         \
475             OPBLOCK( p_vdec, p_mb->ppi_blocks[i_b],                     \
476                      p_mb->p_data[i_b], p_mb->i_addb_l_stride );        \
477         }                                                               \
478     }                                                                   \
479 }
480
481 void vdec_DecodeMacroblockC ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
482 {
483     if( !(p_mb->i_mb_type & MB_INTRA) )
484     {
485         /*
486          * Motion Compensation (ISO/IEC 13818-2 section 7.6)
487          */
488         if( p_mb->pf_motion == 0 )
489         {
490             intf_WarnMsg( 2, "pf_motion set to NULL" );
491         }
492         else
493         {
494             p_mb->pf_motion( p_mb );
495         }
496
497         DECODEBLOCKSC( AddBlock )
498     }
499     else
500     {
501         DECODEBLOCKSC( CopyBlock )
502     }
503
504     /*
505      * Decoding is finished, release the macroblock and free
506      * unneeded memory.
507      */
508     vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
509 }
510
511 void vdec_DecodeMacroblockBW ( vdec_thread_t *p_vdec, macroblock_t * p_mb )
512 {
513     if( !(p_mb->i_mb_type & MB_INTRA) )
514     {
515         /*
516          * Motion Compensation (ISO/IEC 13818-2 section 7.6)
517          */
518         if( p_mb->pf_motion == 0 )
519         {
520             intf_WarnMsg( 2, "pf_motion set to NULL" );
521         }
522         else
523         {
524             p_mb->pf_motion( p_mb );
525         }
526
527         DECODEBLOCKSBW( AddBlock )
528     }
529     else
530     {
531         DECODEBLOCKSBW( CopyBlock )
532     }
533
534     /*
535      * Decoding is finished, release the macroblock and free
536      * unneeded memory.
537      */
538     vpar_ReleaseMacroblock( &p_vdec->p_vpar->vfifo, p_mb );
539 }
540
541
542
543 /*****************************************************************************
544  * RunThread: video decoder thread
545  *****************************************************************************
546  * Video decoder thread. This function does only return when the thread is
547  * terminated.
548  *****************************************************************************/
549 static void RunThread( vdec_thread_t *p_vdec )
550 {
551     intf_DbgMsg("vdec debug: running video decoder thread (%p) (pid == %i)",
552                 p_vdec, getpid());
553
554     /*
555      * Initialize thread and free configuration
556      */
557     p_vdec->b_error = vdec_InitThread( p_vdec );
558     if( p_vdec->b_error )
559     {
560         return;
561     }
562     p_vdec->b_run = 1;
563
564     /*
565      * Main loop - it is not executed if an error occured during
566      * initialization
567      */
568     while( (!p_vdec->b_die) && (!p_vdec->b_error) )
569     {
570         macroblock_t *          p_mb;
571
572         if( (p_mb = vpar_GetMacroblock( &p_vdec->p_vpar->vfifo )) != NULL )
573         {
574             vdec_DecodeMacroblockC ( p_vdec, p_mb );
575         }
576     }
577
578     /*
579      * Error loop
580      */
581     if( p_vdec->b_error )
582     {
583         ErrorThread( p_vdec );
584     }
585
586     /* End of thread */
587     EndThread( p_vdec );
588     p_vdec->b_run = 0;
589 }