]> git.sesse.net Git - vlc/blob - modules/codec/mash.cpp
Removes trailing spaces. Removes tabs.
[vlc] / modules / codec / mash.cpp
1 /*****************************************************************************
2  * mash.c: Video decoder using openmash codec implementations
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc_codec.h>
29 #include <vlc_vout.h>
30 #include <vlc_block.h>
31
32 #include <p64/p64.h>
33
34 /*****************************************************************************
35  * decoder_sys_t : video decoder descriptor
36  *****************************************************************************/
37 struct decoder_sys_t
38 {
39     /*
40      * Common properties
41      */
42     mtime_t i_pts;
43     IntraP64Decoder *p_decoder;
44     vlc_bool_t b_inited;
45     int i_counter;
46
47 };
48
49 /****************************************************************************
50  * Local prototypes
51  ****************************************************************************/
52 static int  OpenDecoder   ( vlc_object_t * );
53 static void CloseDecoder  ( vlc_object_t * );
54
55 static void *DecodeBlock  ( decoder_t *, block_t ** );
56
57 #if 0
58 static picture_t *DecodeFrame( decoder_t *, block_t * );
59 static block_t   *SendFrame  ( decoder_t *, block_t * );
60 #endif
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66     set_description( _("Video decoder using openmash") );
67     set_capability( "decoder", 50 );
68     set_category( CAT_INPUT );
69     set_subcategory( SUBCAT_INPUT_VCODEC );
70     set_callbacks( OpenDecoder, CloseDecoder );
71 vlc_module_end();
72
73 /*****************************************************************************
74  * OpenDecoder: probe the decoder and return score
75  *****************************************************************************/
76 static int OpenDecoder( vlc_object_t *p_this )
77 {
78     decoder_t *p_dec = (decoder_t*)p_this;
79     decoder_sys_t *p_sys;
80
81     switch( p_dec->fmt_in.i_codec )
82     {
83         /* Planar YUV */
84         case VLC_FOURCC('h','2','6','1'):
85         case VLC_FOURCC('H','2','6','1'):
86             break;
87
88         default:
89             return VLC_EGENERIC;
90     }
91
92     /* Allocate the memory needed to store the decoder's structure */
93     if( ( p_dec->p_sys = p_sys =
94           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
95     {
96         msg_Err( p_dec, "out of memory" );
97         return VLC_EGENERIC;
98     }
99     /* Misc init */
100     p_sys->i_pts = 0;
101     p_sys->b_inited = VLC_FALSE;
102     p_sys->i_counter = 0;
103
104     /* Set output properties */
105     p_dec->fmt_out.i_cat = VIDEO_ES;
106     p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
107
108     /* Set callbacks */
109     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
110         DecodeBlock;
111     p_sys->p_decoder = new IntraP64Decoder();
112 //     foo->sync();
113
114     return VLC_SUCCESS;
115 }
116
117 /*****************************************************************************
118  * CloseDecoder: decoder destruction
119  *****************************************************************************/
120 static void CloseDecoder( vlc_object_t *p_this )
121 {
122     decoder_t *p_dec = (decoder_t*)p_this;
123     free( p_dec->p_sys );
124 }
125
126
127 /****************************************************************************
128  * DecodeBlock: the whole thing
129  ****************************************************************************
130  * This function must be fed with complete frames.
131  ****************************************************************************/
132 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
133 {
134     decoder_sys_t *p_sys = p_dec->p_sys;
135     block_t *p_block;
136     picture_t *p_pic;
137     uint32_t i_video_header;
138     uint8_t *p_frame;
139     int cc, sbit, ebit, mba, gob, quant, mvdh, mvdv;
140     int i_width, i_height;
141
142     if( !pp_block || !*pp_block ) return NULL;
143
144     p_block = *pp_block;
145
146     if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
147     {
148         /* We've just started the stream, wait for the first PTS. */
149         block_Release( p_block );
150         return NULL;
151     }
152
153
154     /* Date management */
155     if( p_block->i_pts > 0 || p_block->i_dts > 0 )
156     {
157         if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
158         else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
159     }
160
161     i_video_header = *(uint32_t*)p_block->p_buffer; /* yes, it is native endian */
162     sbit = i_video_header >> 29; /* start bit position */
163     ebit = (i_video_header >> 26) & 7; /* end bit position */
164     msg_Dbg( p_dec, "sbit, ebit: %d,%d", sbit, ebit );
165     gob = (i_video_header >> 20) & 0xf; /* GOB number */
166     if( gob > 12 )
167     {
168         msg_Warn( p_dec, "invalid gob, buggy vic streamer?");
169     }
170     mba = (i_video_header >> 15) & 0x1f; /* Macroblock address predictor */
171     quant = (i_video_header >> 10) & 0x1f; /* quantizer */
172     mvdh = (i_video_header >> 5) & 0x1f; /* horizontal motion vector data */
173     mvdv = i_video_header & 0x1f; /* vertical motion vector data */
174     cc = p_block->i_buffer - 4;
175     msg_Dbg( p_dec, "packet size %d", cc );
176  
177     /* Find out p_vdec->i_raw_size */
178     p_sys->p_decoder->decode( p_block->p_buffer + 4 /*bp?*/,
179                               cc /*cc?*/,
180                               sbit /*sbit?*/,
181                               ebit /*ebit?*/,
182                               mba /* mba?*/,
183                               gob /* gob?*/,
184                               quant /* quant?*/,
185                               mvdh /* mvdh?*/,
186                               mvdv /* mvdv?*/ );
187     i_width = p_sys->p_decoder->width();
188     i_height = p_sys->p_decoder->height();
189     if( !p_sys->b_inited )
190     {
191         msg_Dbg( p_dec, "video size is perhaps %dx%d", i_width,
192                   i_height);
193         vout_InitFormat( &p_dec->fmt_out.video, VLC_FOURCC('I','4','2','0'),
194                          i_width, i_height,
195                          VOUT_ASPECT_FACTOR * i_width / i_height );
196         p_sys->b_inited = VLC_TRUE;
197     }
198     p_pic = NULL;
199     p_sys->i_counter++;
200 //    p_sys->p_decoder->sync();
201     if( p_block->i_flags & BLOCK_FLAG_END_OF_FRAME )
202     {
203         p_pic = p_dec->pf_vout_buffer_new( p_dec );
204         if( !p_pic )
205         {
206             block_Release( p_block );
207             return NULL;
208         }
209         p_sys->p_decoder->sync();
210         p_sys->i_counter = 0;
211         p_frame = p_sys->p_decoder->frame();
212         p_dec->p_libvlc->pf_memcpy( p_pic->p[0].p_pixels, p_frame, i_width*i_height );
213         p_frame += i_width * i_height;
214         p_dec->p_libvlc->pf_memcpy( p_pic->p[1].p_pixels, p_frame, i_width*i_height/4 );
215         p_frame += i_width * i_height/4;
216         p_dec->p_libvlc->pf_memcpy( p_pic->p[2].p_pixels, p_frame, i_width*i_height/4 );
217         p_pic->date = p_sys->i_pts;
218     }
219     block_Release( p_block);
220     *pp_block = NULL;
221     return p_pic;
222 //    return NULL;
223 }