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