dirac_parser.cpp

00001 /* ***** BEGIN LICENSE BLOCK *****
00002 *
00003 * $Id: dirac_parser.cpp,v 1.4 2005/01/30 05:11:41 gabest Exp $ $Name:  $
00004 *
00005 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006 *
00007 * The contents of this file are subject to the Mozilla Public License
00008 * Version 1.1 (the "License"); you may not use this file except in compliance
00009 * with the License. You may obtain a copy of the License at
00010 * http://www.mozilla.org/MPL/
00011 *
00012 * Software distributed under the License is distributed on an "AS IS" basis,
00013 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
00014 * the specific language governing rights and limitations under the License.
00015 *
00016 * The Original Code is BBC Research and Development code.
00017 *
00018 * The Initial Developer of the Original Code is the British Broadcasting
00019 * Corporation.
00020 * Portions created by the Initial Developer are Copyright (C) 2004.
00021 * All Rights Reserved.
00022 *
00023 * Contributor(s): Anuradha Suraparaju (Original Author)
00024 *
00025 * Alternatively, the contents of this file may be used under the terms of
00026 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
00027 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
00028 * the GPL or the LGPL are applicable instead of those above. If you wish to
00029 * allow use of your version of this file only under the terms of the either
00030 * the GPL or LGPL and not to allow others to use your version of this file
00031 * under the MPL, indicate your decision by deleting the provisions above
00032 * and replace them with the notice and other provisions required by the GPL
00033 * or LGPL. If you do not delete the provisions above, a recipient may use
00034 * your version of this file under the terms of any one of the MPL, the GPL
00035 * or the LGPL.
00036 * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <cstring>
00039 #include <libdirac_common/dirac_assertions.h>
00040 #include <libdirac_decoder/dirac_cppparser.h>
00041 #include <libdirac_decoder/dirac_parser.h>
00042 #include <libdirac_common/frame.h>
00043 using namespace dirac;
00044 
00045 #ifdef __cplusplus
00046 extern "C" {
00047 #endif
00048 
00049 extern DllExport dirac_decoder_t *dirac_decoder_init(int verbose)
00050 {
00051     dirac_decoder_t* decoder = new dirac_decoder_t;
00052     memset (decoder, 0, sizeof(dirac_decoder_t));
00053 
00054     bool verbosity = verbose > 0 ? true : false;
00055     DiracParser *parser = new DiracParser(verbosity);
00056     decoder->parser = static_cast<void *>(parser);
00057 
00058     decoder->fbuf = new dirac_framebuf_t;
00059     decoder->fbuf->id = NULL;
00060     decoder->fbuf->buf[0] = decoder->fbuf->buf[1] = decoder->fbuf->buf[2] = NULL;
00061     return decoder;
00062 }
00063 
00064 extern DllExport void dirac_decoder_close(dirac_decoder_t *decoder)
00065 {
00066     TEST (decoder != NULL);
00067     TEST (decoder->parser != NULL);
00068     DiracParser *parser = static_cast<DiracParser *>(decoder->parser);
00069 
00070     delete parser;
00071 
00072     delete decoder->fbuf;
00073 
00074     delete decoder;
00075 
00076     decoder = NULL;
00077 }
00078 
00079 extern DllExport void dirac_buffer (dirac_decoder_t *decoder, unsigned char *start, unsigned char *end)
00080 {
00081     TEST (decoder != NULL);
00082     TEST (decoder->parser != NULL);
00083     DiracParser *parser = static_cast<DiracParser *>(decoder->parser);
00084 
00085     parser->SetBuffer((char *)start, (char *)end);
00086 }
00087 
00088 static void set_sequence_params (const  DiracParser * const parser, dirac_decoder_t *decoder)
00089 {
00090     TEST (parser != NULL);
00091     TEST (decoder != NULL);
00092 
00093     dirac_seqparams_t *seq_params = &decoder->seq_params;
00094     const SeqParams& sparams = parser->GetSeqParams();
00095 
00096     seq_params->width = sparams.Xl();
00097     seq_params->height = sparams.Yl();
00098 
00100     // with Chroma is common.h
00101     seq_params->chroma = (dirac_chroma_t)sparams.CFormat();
00102     switch(seq_params->chroma)
00103     {
00104     case format411:
00105             seq_params->chroma_width = seq_params->width/4;
00106             seq_params->chroma_height = seq_params->height;
00107         break;
00108 
00109     case format420:
00110           seq_params->chroma_width = seq_params->width/2;
00111             seq_params->chroma_height = seq_params->height/2;
00112         break;
00113 
00114     case format422:
00115             seq_params->chroma_width = seq_params->width/2;
00116             seq_params->chroma_height = seq_params->height;
00117             break;
00118     default:
00119             seq_params->chroma_width = seq_params->width;
00120             seq_params->chroma_height = seq_params->height;
00121         break;
00122 
00123    }
00124 
00125     // NOTE: frame rate will be replaced by a struct holding numerator
00126     //       and denominator values.
00127     seq_params->frame_rate.numerator = sparams.FrameRate();
00128     seq_params->frame_rate.denominator = 1;
00129     seq_params->interlace = sparams.Interlace() ? 1 : 0;
00130     seq_params->topfieldfirst = sparams.TopFieldFirst() ? 1 : 0;
00131 }
00132 
00133 static void set_component (const PicArray& pic_data,  const CompSort cs, dirac_decoder_t *decoder)
00134 {
00135     TEST (decoder->fbuf != NULL);
00136     int xl, yl;
00137 
00138     unsigned char *buf;
00139 
00140     switch (cs)
00141     {
00142     case U_COMP:
00143         xl = decoder->seq_params.chroma_width;
00144         yl = decoder->seq_params.chroma_height;
00145            buf = decoder->fbuf->buf[1];
00146         break;
00147     case V_COMP:
00148         xl = decoder->seq_params.chroma_width;
00149         yl = decoder->seq_params.chroma_height;
00150            buf = decoder->fbuf->buf[2];
00151         break;
00152 
00153        case Y_COMP:
00154     default:
00155         xl = decoder->seq_params.width;
00156         yl = decoder->seq_params.height;
00157         buf = decoder->fbuf->buf[0];
00158         break;
00159     }
00160 
00161     TEST (buf != NULL);
00162     ValueType tempv;
00163 
00164     for (int j=0 ; j<yl ;++j)
00165     {
00166         for (int i=0 ; i<xl ; ++i)
00167         {                
00168             tempv=pic_data[j][i]+2;
00169             tempv>>=2;
00170             buf[j*xl+i]=(unsigned char) tempv;                
00171         }//i
00172 
00173     }//j
00174 }
00175 
00176 static void set_frame_data (const  DiracParser * const parser, dirac_decoder_t *decoder)
00177 {
00178     TEST (parser != NULL);
00179     TEST (decoder != NULL);
00180     TEST (decoder->fbuf != NULL);
00181     TEST (decoder->state == STATE_PICTURE_AVAIL);
00182 
00183     const Frame& my_frame = parser->GetNextFrame();
00184 
00185     set_component (my_frame.Ydata(), Y_COMP, decoder);
00186     if (decoder->seq_params.chroma != Yonly)
00187     {
00188         set_component (my_frame.Udata(), U_COMP, decoder);
00189         set_component (my_frame.Vdata(), V_COMP, decoder);
00190     }
00191 
00192     return;
00193 }
00194 
00195 static void set_frame_params (const FrameParams& my_frame_params,  dirac_decoder_t *decoder)
00196 {
00197     TEST (decoder != NULL);
00198     dirac_frameparams_t *frame_params = &decoder->frame_params;
00199 
00200     TEST (decoder->state == STATE_PICTURE_AVAIL ||
00201           decoder->state == STATE_PICTURE_START);
00202 
00203     frame_params->ftype = (dirac_frame_type_t)my_frame_params.FSort();
00204     frame_params->fnum = my_frame_params.FrameNum();
00205 
00206     return;
00207 }
00208 
00209 extern DllExport dirac_decoder_state_t dirac_parse (dirac_decoder_t *decoder)
00210 {
00211     TEST (decoder != NULL);
00212     TEST (decoder->parser != NULL);
00213     DiracParser *parser = static_cast<DiracParser *>(decoder->parser);
00214 
00215     decoder->state = parser->Parse();
00216 
00217     switch (decoder->state)
00218     {
00219     case STATE_BUFFER:
00220         break;
00221 
00222     case STATE_SEQUENCE:
00223         set_sequence_params(parser, decoder);
00224         decoder->frame_avail = 0;
00225         break;
00226 
00227     case STATE_PICTURE_START:
00228         /* frame params of the frame being decoded in coding order */
00229         set_frame_params (parser->GetNextFrameParams(), decoder);
00230         decoder->frame_avail = 0;
00231         break;
00232 
00233     case STATE_PICTURE_AVAIL:
00234         decoder->frame_avail = 1;
00235         /* frame params of the frame available for display */
00236         set_frame_params (parser->GetNextFrame().GetFparams(), decoder);
00237         set_frame_data (parser, decoder);
00238         break;
00239 
00240     case STATE_INVALID:
00241         break;
00242 
00243     default:
00244         break;
00245     }
00246     return decoder->state;
00247 }
00248 
00249 extern DllExport void dirac_skip (dirac_decoder_t *decoder, int skip)
00250 {
00251     TEST (decoder != NULL);
00252     TEST (decoder->parser != NULL);
00253     DiracParser *parser = static_cast<DiracParser *>(decoder->parser);
00254 
00255     parser->SetSkip(skip > 0 ? true : false);
00256 }
00257 
00258 
00259 extern DllExport void dirac_set_buf (dirac_decoder_t *decoder, unsigned char *buf[3], void *id)
00260 {
00261     TEST (decoder != NULL);
00262     TEST (decoder->fbuf != NULL);
00263 
00264     decoder->fbuf->buf[0] = buf[0];
00265     decoder->fbuf->buf[1] = buf[1];
00266     decoder->fbuf->buf[2] = buf[2];
00267     decoder->fbuf->id  = id;
00268 }
00269 
00270 #ifdef __cplusplus
00271 }
00272 #endif

Generated on Tue Dec 13 14:47:15 2005 for guliverkli by  doxygen 1.4.5