• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/libschroedingerdec.c

Go to the documentation of this file.
00001 /*
00002  * Dirac decoder support via Schroedinger libraries
00003  * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00030 #include "avcodec.h"
00031 #include "libdirac_libschro.h"
00032 #include "libschroedinger.h"
00033 
00034 #undef NDEBUG
00035 #include <assert.h>
00036 
00037 
00038 #include <schroedinger/schro.h>
00039 #include <schroedinger/schrodebug.h>
00040 #include <schroedinger/schrovideoformat.h>
00041 
00043 typedef struct FfmpegSchroDecoderParams
00044 {
00046     SchroVideoFormat *format;
00047 
00049     SchroFrameFormat frame_format;
00050 
00052     SchroDecoder* decoder;
00053 
00055     FfmpegDiracSchroQueue dec_frame_queue;
00056 
00058     int eos_signalled;
00059 
00061     int eos_pulled;
00062 
00064     AVPicture dec_pic;
00065 } FfmpegSchroDecoderParams;
00066 
00067 typedef struct FfmpegSchroParseUnitContext
00068 {
00069     const uint8_t *buf;
00070     int           buf_size;
00071 } FfmpegSchroParseUnitContext;
00072 
00073 
00074 static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf,
00075                                                 void *priv);
00076 
00077 static void FfmpegSchroParseContextInit (FfmpegSchroParseUnitContext *parse_ctx,
00078                                          const uint8_t *buf, int buf_size)
00079 {
00080     parse_ctx->buf           = buf;
00081     parse_ctx->buf_size      = buf_size;
00082 }
00083 
00084 static SchroBuffer* FfmpegFindNextSchroParseUnit (FfmpegSchroParseUnitContext *parse_ctx)
00085 {
00086     SchroBuffer *enc_buf = NULL;
00087     int next_pu_offset = 0;
00088     unsigned char *in_buf;
00089 
00090     if (parse_ctx->buf_size < 13 ||
00091         parse_ctx->buf[0] != 'B' ||
00092         parse_ctx->buf[1] != 'B' ||
00093         parse_ctx->buf[2] != 'C' ||
00094         parse_ctx->buf[3] != 'D')
00095         return NULL;
00096 
00097     next_pu_offset = (parse_ctx->buf[5] << 24) +
00098                      (parse_ctx->buf[6] << 16) +
00099                      (parse_ctx->buf[7] <<  8) +
00100                       parse_ctx->buf[8];
00101 
00102     if (next_pu_offset == 0 &&
00103         SCHRO_PARSE_CODE_IS_END_OF_SEQUENCE(parse_ctx->buf[4]))
00104         next_pu_offset = 13;
00105 
00106     if (next_pu_offset <= 0 || parse_ctx->buf_size < next_pu_offset)
00107         return NULL;
00108 
00109     in_buf = av_malloc(next_pu_offset);
00110     memcpy (in_buf, parse_ctx->buf, next_pu_offset);
00111     enc_buf = schro_buffer_new_with_data (in_buf, next_pu_offset);
00112     enc_buf->free = libschroedinger_decode_buffer_free;
00113     enc_buf->priv = in_buf;
00114 
00115     parse_ctx->buf += next_pu_offset;
00116     parse_ctx->buf_size -= next_pu_offset;
00117 
00118     return enc_buf;
00119 }
00120 
00124 static enum PixelFormat GetFfmpegChromaFormat(SchroChromaFormat schro_pix_fmt)
00125 {
00126     int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
00127                       sizeof(ffmpeg_schro_pixel_format_map[0]);
00128     int idx;
00129 
00130     for (idx = 0; idx < num_formats; ++idx) {
00131         if (ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
00132             return ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt;
00133         }
00134     }
00135     return PIX_FMT_NONE;
00136 }
00137 
00138 static av_cold int libschroedinger_decode_init(AVCodecContext *avccontext)
00139 {
00140 
00141     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data ;
00142     /* First of all, initialize our supporting libraries. */
00143     schro_init();
00144 
00145     schro_debug_set_level(avccontext->debug);
00146     p_schro_params->decoder =  schro_decoder_new();
00147     schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
00148 
00149     if (!p_schro_params->decoder)
00150         return -1;
00151 
00152     /* Initialize the decoded frame queue. */
00153     ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
00154     return 0 ;
00155 }
00156 
00157 static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf,
00158                                                 void *priv)
00159 {
00160     av_freep(&priv);
00161 }
00162 
00163 static void libschroedinger_decode_frame_free (void *frame)
00164 {
00165     schro_frame_unref(frame);
00166 }
00167 
00168 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
00169 {
00170     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
00171     SchroDecoder *decoder = p_schro_params->decoder;
00172 
00173     p_schro_params->format = schro_decoder_get_video_format (decoder);
00174 
00175     /* Tell FFmpeg about sequence details. */
00176     if(avcodec_check_dimensions(avccontext, p_schro_params->format->width,
00177                                 p_schro_params->format->height) < 0) {
00178         av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
00179                p_schro_params->format->width, p_schro_params->format->height);
00180         avccontext->height = avccontext->width = 0;
00181         return;
00182     }
00183     avccontext->height  = p_schro_params->format->height;
00184     avccontext->width   = p_schro_params->format->width;
00185     avccontext->pix_fmt =
00186                    GetFfmpegChromaFormat(p_schro_params->format->chroma_format);
00187 
00188     if (ff_get_schro_frame_format( p_schro_params->format->chroma_format,
00189                                    &p_schro_params->frame_format) == -1) {
00190         av_log (avccontext, AV_LOG_ERROR,
00191                 "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
00192                 "and 4:4:4 formats.\n");
00193         return;
00194     }
00195 
00196     avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
00197     avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
00198 
00199     if (p_schro_params->dec_pic.data[0] == NULL)
00200     {
00201         avpicture_alloc(&p_schro_params->dec_pic,
00202                         avccontext->pix_fmt,
00203                         avccontext->width,
00204                         avccontext->height);
00205     }
00206 }
00207 
00208 static int libschroedinger_decode_frame(AVCodecContext *avccontext,
00209                                         void *data, int *data_size,
00210                                         const uint8_t *buf, int buf_size)
00211 {
00212 
00213     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
00214     SchroDecoder *decoder = p_schro_params->decoder;
00215     SchroVideoFormat *format;
00216     AVPicture *picture = data;
00217     SchroBuffer *enc_buf;
00218     SchroFrame* frame;
00219     int state;
00220     int go = 1;
00221     int outer = 1;
00222     FfmpegSchroParseUnitContext parse_ctx;
00223 
00224     *data_size = 0;
00225 
00226     FfmpegSchroParseContextInit (&parse_ctx, buf, buf_size);
00227     if (buf_size == 0) {
00228         if (!p_schro_params->eos_signalled) {
00229             state = schro_decoder_push_end_of_stream(decoder);
00230             p_schro_params->eos_signalled = 1;
00231         }
00232     }
00233 
00234     /* Loop through all the individual parse units in the input buffer */
00235     do {
00236         if ((enc_buf = FfmpegFindNextSchroParseUnit(&parse_ctx))) {
00237             /* Push buffer into decoder. */
00238             if (SCHRO_PARSE_CODE_IS_PICTURE(enc_buf->data[4]) &&
00239                 SCHRO_PARSE_CODE_NUM_REFS(enc_buf->data[4]) > 0)
00240                 avccontext->has_b_frames = 1;
00241             state = schro_decoder_push (decoder, enc_buf);
00242             if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
00243                   libschroedinger_handle_first_access_unit(avccontext);
00244             go = 1;
00245         }
00246         else
00247             outer = 0;
00248     format = p_schro_params->format;
00249 
00250     while (go) {
00251         /* Parse data and process result. */
00252         state = schro_decoder_wait (decoder);
00253         switch (state)
00254         {
00255         case SCHRO_DECODER_FIRST_ACCESS_UNIT:
00256             libschroedinger_handle_first_access_unit (avccontext);
00257             break;
00258 
00259         case SCHRO_DECODER_NEED_BITS:
00260             /* Need more input data - stop iterating over what we have. */
00261             go = 0;
00262             break;
00263 
00264         case SCHRO_DECODER_NEED_FRAME:
00265             /* Decoder needs a frame - create one and push it in. */
00266 
00267             frame = schro_frame_new_and_alloc(NULL,
00268                                               p_schro_params->frame_format,
00269                                               format->width,
00270                                               format->height);
00271             schro_decoder_add_output_picture (decoder, frame);
00272             break;
00273 
00274         case SCHRO_DECODER_OK:
00275             /* Pull a frame out of the decoder. */
00276             frame = schro_decoder_pull (decoder);
00277 
00278             if (frame) {
00279                 ff_dirac_schro_queue_push_back(
00280                                              &p_schro_params->dec_frame_queue,
00281                                              frame);
00282             }
00283             break;
00284         case SCHRO_DECODER_EOS:
00285             go = 0;
00286             p_schro_params->eos_pulled = 1;
00287             schro_decoder_reset (decoder);
00288             outer = 0;
00289             break;
00290 
00291         case SCHRO_DECODER_ERROR:
00292             return -1;
00293             break;
00294         }
00295     }
00296     } while(outer);
00297 
00298     /* Grab next frame to be returned from the top of the queue. */
00299     frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);
00300 
00301     if (frame != NULL) {
00302         memcpy (p_schro_params->dec_pic.data[0],
00303                 frame->components[0].data,
00304                 frame->components[0].length);
00305 
00306         memcpy (p_schro_params->dec_pic.data[1],
00307                 frame->components[1].data,
00308                 frame->components[1].length);
00309 
00310         memcpy (p_schro_params->dec_pic.data[2],
00311                 frame->components[2].data,
00312                 frame->components[2].length);
00313 
00314         /* Fill picture with current buffer data from Schroedinger. */
00315         avpicture_fill(picture, p_schro_params->dec_pic.data[0],
00316                        avccontext->pix_fmt,
00317                        avccontext->width, avccontext->height);
00318 
00319         *data_size = sizeof(AVPicture);
00320 
00321         /* Now free the frame resources. */
00322         libschroedinger_decode_frame_free (frame);
00323     }
00324     return buf_size;
00325 }
00326 
00327 
00328 static av_cold int libschroedinger_decode_close(AVCodecContext *avccontext)
00329 {
00330     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
00331     /* Free the decoder. */
00332     schro_decoder_free (p_schro_params->decoder);
00333     av_freep(&p_schro_params->format);
00334 
00335     avpicture_free (&p_schro_params->dec_pic);
00336 
00337     /* Free data in the output frame queue. */
00338     ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
00339                                libschroedinger_decode_frame_free);
00340 
00341     return 0 ;
00342 }
00343 
00344 static void libschroedinger_flush (AVCodecContext *avccontext)
00345 {
00346     /* Got a seek request. Free the decoded frames queue and then reset
00347      * the decoder */
00348     FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
00349 
00350     /* Free data in the output frame queue. */
00351     ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
00352                                libschroedinger_decode_frame_free);
00353 
00354     ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
00355     schro_decoder_reset(p_schro_params->decoder);
00356     p_schro_params->eos_pulled = 0;
00357     p_schro_params->eos_signalled = 0;
00358 }
00359 
00360 AVCodec libschroedinger_decoder = {
00361      "libschroedinger",
00362     CODEC_TYPE_VIDEO,
00363     CODEC_ID_DIRAC,
00364     sizeof(FfmpegSchroDecoderParams),
00365     libschroedinger_decode_init,
00366     NULL,
00367     libschroedinger_decode_close,
00368     libschroedinger_decode_frame,
00369     CODEC_CAP_DELAY,
00370     .flush = libschroedinger_flush,
00371     .long_name = NULL_IF_CONFIG_SMALL("libschroedinger Dirac 2.2"),
00372 };

Generated on Sat Feb 16 2013 09:23:12 for ffmpeg by  doxygen 1.7.1