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