libavcodec/libdiracdec.c
690bfceb
 /*
  * Dirac decoder support via libdirac library
  * Copyright (c) 2005 BBC, Andrew Kennedy <dirac at rd dot bbc dot co dot uk>
  * Copyright (c) 2006-2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
ba87f080
 * @file
690bfceb
 * Dirac decoder support via libdirac library; more details about the Dirac
 * project can be found at http://dirac.sourceforge.net/.
 * The libdirac_decoder library implements Dirac specification version 2.2
 * (http://dirac.sourceforge.net/specification.html).
 */
 
7ffe76e5
 #include "libavutil/imgutils.h"
690bfceb
 #include "libdirac.h"
 
 #undef NDEBUG
 #include <assert.h>
 
 #include <libdirac_decoder/dirac_parser.h>
 
 /** contains a single frame returned from Dirac */
0ebe3b8e
 typedef struct FfmpegDiracDecoderParams {
690bfceb
     /** decoder handle */
     dirac_decoder_t* p_decoder;
 
     /** buffer to hold decoded frame */
     unsigned char* p_out_frame_buf;
 } FfmpegDiracDecoderParams;
 
 
 /**
2d2b5a14
 * returns FFmpeg chroma format
690bfceb
 */
 static enum PixelFormat GetFfmpegChromaFormat(dirac_chroma_t dirac_pix_fmt)
 {
     int num_formats = sizeof(ffmpeg_dirac_pixel_format_map) /
                       sizeof(ffmpeg_dirac_pixel_format_map[0]);
     int idx;
 
735a3804
     for (idx = 0; idx < num_formats; ++idx)
         if (ffmpeg_dirac_pixel_format_map[idx].dirac_pix_fmt == dirac_pix_fmt)
690bfceb
             return ffmpeg_dirac_pixel_format_map[idx].ff_pix_fmt;
     return PIX_FMT_NONE;
 }
 
5ef251e5
 static av_cold int libdirac_decode_init(AVCodecContext *avccontext)
690bfceb
 {
 
0ebe3b8e
     FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
690bfceb
     p_dirac_params->p_decoder =  dirac_decoder_init(avccontext->debug);
 
     if (!p_dirac_params->p_decoder)
         return -1;
 
0ebe3b8e
     return 0;
690bfceb
 }
 
 static int libdirac_decode_frame(AVCodecContext *avccontext,
                                  void *data, int *data_size,
7a00bbad
                                  AVPacket *avpkt)
690bfceb
 {
7a00bbad
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
690bfceb
 
     FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
     AVPicture *picture = data;
     AVPicture pic;
     int pict_size;
     unsigned char *buffer[3];
 
     *data_size = 0;
 
0ebe3b8e
     if (buf_size > 0) {
690bfceb
         /* set data to decode into buffer */
0ebe3b8e
         dirac_buffer(p_dirac_params->p_decoder, buf, buf + buf_size);
         if ((buf[4] & 0x08) == 0x08 && (buf[4] & 0x03))
64bfc584
             avccontext->has_b_frames = 1;
     }
690bfceb
     while (1) {
          /* parse data and process result */
0ebe3b8e
         DecoderState state = dirac_parse(p_dirac_params->p_decoder);
         switch (state) {
690bfceb
         case STATE_BUFFER:
             return buf_size;
 
         case STATE_SEQUENCE:
         {
2d2b5a14
             /* tell FFmpeg about sequence details */
0ebe3b8e
             dirac_sourceparams_t *src_params = &p_dirac_params->p_decoder->src_params;
690bfceb
 
e16f217c
             if (av_image_check_size(src_params->width, src_params->height,
6ce9b431
                                     0, avccontext) < 0) {
690bfceb
                 av_log(avccontext, AV_LOG_ERROR, "Invalid dimensions (%dx%d)\n",
                        src_params->width, src_params->height);
                 avccontext->height = avccontext->width = 0;
                 return -1;
             }
 
             avccontext->height = src_params->height;
             avccontext->width  = src_params->width;
 
             avccontext->pix_fmt = GetFfmpegChromaFormat(src_params->chroma);
             if (avccontext->pix_fmt == PIX_FMT_NONE) {
0ebe3b8e
                 av_log(avccontext, AV_LOG_ERROR,
                        "Dirac chroma format %d not supported currently\n",
                        src_params->chroma);
690bfceb
                 return -1;
             }
 
             avccontext->time_base.den = src_params->frame_rate.numerator;
             avccontext->time_base.num = src_params->frame_rate.denominator;
 
             /* calculate output dimensions */
             avpicture_fill(&pic, NULL, avccontext->pix_fmt,
                            avccontext->width, avccontext->height);
 
             pict_size = avpicture_get_size(avccontext->pix_fmt,
                                            avccontext->width,
                                            avccontext->height);
 
             /* allocate output buffer */
7c809dc3
             if (!p_dirac_params->p_out_frame_buf)
0ebe3b8e
                 p_dirac_params->p_out_frame_buf = av_malloc(pict_size);
690bfceb
             buffer[0] = p_dirac_params->p_out_frame_buf;
             buffer[1] = p_dirac_params->p_out_frame_buf +
                         pic.linesize[0] * avccontext->height;
             buffer[2] = buffer[1] +
                         pic.linesize[1] * src_params->chroma_height;
 
             /* tell Dirac about output destination */
             dirac_set_buf(p_dirac_params->p_decoder, buffer, NULL);
             break;
         }
         case STATE_SEQUENCE_END:
             break;
 
         case STATE_PICTURE_AVAIL:
             /* fill picture with current buffer data from Dirac */
             avpicture_fill(picture, p_dirac_params->p_out_frame_buf,
                            avccontext->pix_fmt,
                            avccontext->width, avccontext->height);
             *data_size = sizeof(AVPicture);
             return buf_size;
 
         case STATE_INVALID:
             return -1;
 
         default:
             break;
         }
     }
 
     return buf_size;
 }
 
 
5ef251e5
 static av_cold int libdirac_decode_close(AVCodecContext *avccontext)
690bfceb
 {
     FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
0ebe3b8e
     dirac_decoder_close(p_dirac_params->p_decoder);
690bfceb
 
     av_freep(&p_dirac_params->p_out_frame_buf);
 
0ebe3b8e
     return 0;
690bfceb
 }
 
0ebe3b8e
 static void libdirac_flush(AVCodecContext *avccontext)
690bfceb
 {
     /* Got a seek request. We will need free memory held in the private
      * context and free the current Dirac decoder handle and then open
      * a new decoder handle. */
0ebe3b8e
     libdirac_decode_close(avccontext);
     libdirac_decode_init(avccontext);
690bfceb
     return;
 }
 
 
 
e7e2df27
 AVCodec ff_libdirac_decoder = {
690bfceb
     "libdirac",
72415b2a
     AVMEDIA_TYPE_VIDEO,
690bfceb
     CODEC_ID_DIRAC,
     sizeof(FfmpegDiracDecoderParams),
     libdirac_decode_init,
     NULL,
     libdirac_decode_close,
     libdirac_decode_frame,
     CODEC_CAP_DELAY,
9f4aa353
     .flush = libdirac_flush,
fe4bf374
     .long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
0ebe3b8e
 };