]> git.sesse.net Git - vlc/blob - modules/codec/mash.cpp
b534eede5f5d63c8d9b672f4c59f8bdcd7c4f5c1
[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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/decoder.h>
29 #include <vlc/vout.h>
30
31 #include <p64/p64.h>
32
33 /*****************************************************************************
34  * decoder_sys_t : video decoder descriptor
35  *****************************************************************************/
36 struct decoder_sys_t
37 {
38     /*
39      * Common properties
40      */
41     mtime_t i_pts;
42     IntraP64Decoder *p_decoder;
43     vlc_bool_t b_inited;
44     int i_counter;
45
46 };
47
48 /****************************************************************************
49  * Local prototypes
50  ****************************************************************************/
51 static int  OpenDecoder   ( vlc_object_t * );
52 static void CloseDecoder  ( vlc_object_t * );
53
54 static void *DecodeBlock  ( decoder_t *, block_t ** );
55
56 #if 0
57 static picture_t *DecodeFrame( decoder_t *, block_t * );
58 static block_t   *SendFrame  ( decoder_t *, block_t * );
59 #endif
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 vlc_module_begin();
65     set_description( _("Video decoder using openmash") );
66     set_capability( "decoder", 50 );
67     set_category( CAT_INPUT );
68     set_subcategory( SUBCAT_INPUT_VCODEC );
69     set_callbacks( OpenDecoder, CloseDecoder );
70 vlc_module_end();
71
72 /*****************************************************************************
73  * OpenDecoder: probe the decoder and return score
74  *****************************************************************************/
75 static int OpenDecoder( vlc_object_t *p_this )
76 {
77     decoder_t *p_dec = (decoder_t*)p_this;
78     decoder_sys_t *p_sys;
79
80     switch( p_dec->fmt_in.i_codec )
81     {
82         /* Planar YUV */
83         case VLC_FOURCC('h','2','6','1'):
84         case VLC_FOURCC('H','2','6','1'):
85             break;
86
87         default:
88             return VLC_EGENERIC;
89     }
90
91     /* Allocate the memory needed to store the decoder's structure */
92     if( ( p_dec->p_sys = p_sys =
93           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
94     {
95         msg_Err( p_dec, "out of memory" );
96         return VLC_EGENERIC;
97     }
98     /* Misc init */
99     p_sys->i_pts = 0;
100     p_sys->b_inited = VLC_FALSE;
101     p_sys->i_counter = 0;
102
103     /* Set output properties */
104     p_dec->fmt_out.i_cat = VIDEO_ES;
105     p_dec->fmt_out.i_codec = VLC_FOURCC('I','4','2','0');
106
107     /* Set callbacks */
108     p_dec->pf_decode_video = (picture_t *(*)(decoder_t *, block_t **))
109         DecodeBlock;
110     p_sys->p_decoder = new IntraP64Decoder();
111 //     foo->sync();
112
113     return VLC_SUCCESS;
114 }
115
116 /*****************************************************************************
117  * CloseDecoder: decoder destruction
118  *****************************************************************************/
119 static void CloseDecoder( vlc_object_t *p_this )
120 {
121     decoder_t *p_dec = (decoder_t*)p_this;
122     free( p_dec->p_sys );
123 }
124
125
126 /****************************************************************************
127  * DecodeBlock: the whole thing
128  ****************************************************************************
129  * This function must be fed with complete frames.
130  ****************************************************************************/
131 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
132 {
133     decoder_sys_t *p_sys = p_dec->p_sys;
134     block_t *p_block;
135     picture_t *p_pic;
136     uint32_t i_video_header;
137     uint8_t *p_frame;
138     int cc, sbit, ebit, mba, gob, quant, mvdh, mvdv;
139     int i_width, i_height;
140
141     if( !pp_block || !*pp_block ) return NULL;
142
143     p_block = *pp_block;
144
145     if( !p_sys->i_pts && !p_block->i_pts && !p_block->i_dts )
146     {
147         /* We've just started the stream, wait for the first PTS. */
148         block_Release( p_block );
149         return NULL;
150     }
151
152
153     /* Date management */
154     if( p_block->i_pts > 0 || p_block->i_dts > 0 )
155     {
156         if( p_block->i_pts > 0 ) p_sys->i_pts = p_block->i_pts;
157         else if( p_block->i_dts > 0 ) p_sys->i_pts = p_block->i_dts;
158     }
159
160     i_video_header = *(uint32_t*)p_block->p_buffer; /* yes, it is native endian */
161     sbit = i_video_header >> 29; /* start bit position */
162     ebit = (i_video_header >> 26) & 7; /* end bit position */
163     msg_Dbg( p_dec, "sbit, ebit: %d,%d", sbit, ebit );
164     gob = (i_video_header >> 20) & 0xf; /* GOB number */
165     if( gob > 12 )
166     {
167         msg_Warn( p_dec, "invalid gob, buggy vic streamer?");
168     }
169     mba = (i_video_header >> 15) & 0x1f; /* Macroblock address predictor */
170     quant = (i_video_header >> 10) & 0x1f; /* quantizer */
171     mvdh = (i_video_header >> 5) & 0x1f; /* horizontal motion vector data */
172     mvdv = i_video_header & 0x1f; /* vertical motion vector data */
173     cc = p_block->i_buffer - 4;
174     msg_Dbg( p_dec, "packet size %d", cc );
175     
176     /* Find out p_vdec->i_raw_size */
177     p_sys->p_decoder->decode( p_block->p_buffer + 4 /*bp?*/,
178                               cc /*cc?*/,
179                               sbit /*sbit?*/,
180                               ebit /*ebit?*/,
181                               mba /* mba?*/,
182                               gob /* gob?*/,
183                               quant /* quant?*/,
184                               mvdh /* mvdh?*/,
185                               mvdv /* mvdv?*/ );
186     i_width = p_sys->p_decoder->width();
187     i_height = p_sys->p_decoder->height();
188     if( !p_sys->b_inited )
189     {
190         msg_Dbg( p_dec, "video size is perhaps %dx%d", i_width,
191                   i_height);
192         vout_InitFormat( &p_dec->fmt_out.video, VLC_FOURCC('I','4','2','0'),
193                          i_width, i_height,
194                          VOUT_ASPECT_FACTOR * i_width / i_height );
195         p_sys->b_inited = VLC_TRUE;
196     }
197     p_pic = NULL;
198     p_sys->i_counter++;
199 //    p_sys->p_decoder->sync();
200     if( p_block->i_flags & BLOCK_FLAG_END_OF_FRAME )
201     {
202         p_pic = p_dec->pf_vout_buffer_new( p_dec );
203         if( !p_pic )
204         {
205             block_Release( p_block );
206             return NULL;
207         }
208         p_sys->p_decoder->sync();
209         p_sys->i_counter = 0;
210         p_frame = p_sys->p_decoder->frame();
211         p_dec->p_vlc->pf_memcpy( p_pic->p[0].p_pixels, p_frame, i_width*i_height );
212         p_frame += i_width * i_height;
213         p_dec->p_vlc->pf_memcpy( p_pic->p[1].p_pixels, p_frame, i_width*i_height/4 );
214         p_frame += i_width * i_height/4;
215         p_dec->p_vlc->pf_memcpy( p_pic->p[2].p_pixels, p_frame, i_width*i_height/4 );
216         p_pic->date = p_sys->i_pts;
217     }
218     block_Release( p_block);
219     *pp_block = NULL;
220     return p_pic;
221 //    return NULL;
222 }