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