2008-05-03 06:47:26 +03:00
|
|
|
/*
|
|
|
|
* 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 >
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* This file is part of Libav.
|
2008-05-03 06:47:26 +03:00
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2008-05-03 06:47:26 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 19:35:10 +02:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2008-05-03 06:47:26 +03:00
|
|
|
* 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
|
2011-03-18 19:35:10 +02:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2008-05-03 06:47:26 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2008-05-03 06:47:26 +03:00
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
|
2011-02-07 15:37:08 +02:00
|
|
|
#include "libavutil/imgutils.h"
|
2008-05-03 06:47:26 +03:00
|
|
|
#include "libdirac.h"
|
|
|
|
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <libdirac_decoder/dirac_parser.h>
|
|
|
|
|
|
|
|
/** contains a single frame returned from Dirac */
|
2011-10-30 15:19:50 +03:00
|
|
|
typedef struct DiracDecoderParams {
|
2008-05-03 06:47:26 +03:00
|
|
|
/** decoder handle */
|
|
|
|
dirac_decoder_t* p_decoder;
|
|
|
|
|
|
|
|
/** buffer to hold decoded frame */
|
|
|
|
unsigned char* p_out_frame_buf;
|
2011-10-30 15:19:50 +03:00
|
|
|
} DiracDecoderParams;
|
2008-05-03 06:47:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-04-15 23:30:26 +03:00
|
|
|
* returns Libav chroma format
|
2008-05-03 06:47:26 +03:00
|
|
|
*/
|
2011-10-30 15:19:50 +03:00
|
|
|
static enum PixelFormat get_chroma_format(dirac_chroma_t dirac_pix_fmt)
|
2008-05-03 06:47:26 +03:00
|
|
|
{
|
2011-10-30 15:19:50 +03:00
|
|
|
int num_formats = sizeof(dirac_pixel_format_map) /
|
|
|
|
sizeof(dirac_pixel_format_map[0]);
|
2008-05-03 06:47:26 +03:00
|
|
|
int idx;
|
|
|
|
|
2009-08-15 14:12:47 +03:00
|
|
|
for (idx = 0; idx < num_formats; ++idx)
|
2011-10-30 15:19:50 +03:00
|
|
|
if (dirac_pixel_format_map[idx].dirac_pix_fmt == dirac_pix_fmt)
|
|
|
|
return dirac_pixel_format_map[idx].ff_pix_fmt;
|
2008-05-03 06:47:26 +03:00
|
|
|
return PIX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
2009-02-22 15:48:55 +02:00
|
|
|
static av_cold int libdirac_decode_init(AVCodecContext *avccontext)
|
2008-05-03 06:47:26 +03:00
|
|
|
{
|
|
|
|
|
2011-10-30 15:19:50 +03:00
|
|
|
DiracDecoderParams *p_dirac_params = avccontext->priv_data;
|
2008-05-03 06:47:26 +03:00
|
|
|
p_dirac_params->p_decoder = dirac_decoder_init(avccontext->debug);
|
|
|
|
|
|
|
|
if (!p_dirac_params->p_decoder)
|
|
|
|
return -1;
|
|
|
|
|
2009-08-15 14:42:15 +03:00
|
|
|
return 0;
|
2008-05-03 06:47:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int libdirac_decode_frame(AVCodecContext *avccontext,
|
|
|
|
void *data, int *data_size,
|
2009-04-07 18:59:50 +03:00
|
|
|
AVPacket *avpkt)
|
2008-05-03 06:47:26 +03:00
|
|
|
{
|
2009-04-07 18:59:50 +03:00
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2008-05-03 06:47:26 +03:00
|
|
|
|
2011-10-30 15:19:50 +03:00
|
|
|
DiracDecoderParams *p_dirac_params = avccontext->priv_data;
|
2008-05-03 06:47:26 +03:00
|
|
|
AVPicture *picture = data;
|
|
|
|
AVPicture pic;
|
|
|
|
int pict_size;
|
|
|
|
unsigned char *buffer[3];
|
|
|
|
|
|
|
|
*data_size = 0;
|
|
|
|
|
2009-08-15 14:42:15 +03:00
|
|
|
if (buf_size > 0) {
|
2008-05-03 06:47:26 +03:00
|
|
|
/* set data to decode into buffer */
|
2009-08-15 14:42:15 +03:00
|
|
|
dirac_buffer(p_dirac_params->p_decoder, buf, buf + buf_size);
|
|
|
|
if ((buf[4] & 0x08) == 0x08 && (buf[4] & 0x03))
|
2008-12-22 02:01:39 +02:00
|
|
|
avccontext->has_b_frames = 1;
|
|
|
|
}
|
2008-05-03 06:47:26 +03:00
|
|
|
while (1) {
|
|
|
|
/* parse data and process result */
|
2009-08-15 14:42:15 +03:00
|
|
|
DecoderState state = dirac_parse(p_dirac_params->p_decoder);
|
|
|
|
switch (state) {
|
2008-05-03 06:47:26 +03:00
|
|
|
case STATE_BUFFER:
|
|
|
|
return buf_size;
|
|
|
|
|
|
|
|
case STATE_SEQUENCE:
|
|
|
|
{
|
2011-04-15 23:30:26 +03:00
|
|
|
/* tell Libav about sequence details */
|
2009-08-15 14:42:15 +03:00
|
|
|
dirac_sourceparams_t *src_params = &p_dirac_params->p_decoder->src_params;
|
2008-05-03 06:47:26 +03:00
|
|
|
|
2010-09-07 22:15:29 +03:00
|
|
|
if (av_image_check_size(src_params->width, src_params->height,
|
2010-08-06 12:37:04 +03:00
|
|
|
0, avccontext) < 0) {
|
2008-05-03 06:47:26 +03:00
|
|
|
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;
|
|
|
|
|
2011-10-30 15:19:50 +03:00
|
|
|
avccontext->pix_fmt = get_chroma_format(src_params->chroma);
|
2008-05-03 06:47:26 +03:00
|
|
|
if (avccontext->pix_fmt == PIX_FMT_NONE) {
|
2009-08-15 14:42:15 +03:00
|
|
|
av_log(avccontext, AV_LOG_ERROR,
|
|
|
|
"Dirac chroma format %d not supported currently\n",
|
|
|
|
src_params->chroma);
|
2008-05-03 06:47:26 +03:00
|
|
|
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 */
|
2009-08-15 14:02:50 +03:00
|
|
|
if (!p_dirac_params->p_out_frame_buf)
|
2009-08-15 14:42:15 +03:00
|
|
|
p_dirac_params->p_out_frame_buf = av_malloc(pict_size);
|
2008-05-03 06:47:26 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-22 15:48:55 +02:00
|
|
|
static av_cold int libdirac_decode_close(AVCodecContext *avccontext)
|
2008-05-03 06:47:26 +03:00
|
|
|
{
|
2011-10-30 15:19:50 +03:00
|
|
|
DiracDecoderParams *p_dirac_params = avccontext->priv_data;
|
2009-08-15 14:42:15 +03:00
|
|
|
dirac_decoder_close(p_dirac_params->p_decoder);
|
2008-05-03 06:47:26 +03:00
|
|
|
|
|
|
|
av_freep(&p_dirac_params->p_out_frame_buf);
|
|
|
|
|
2009-08-15 14:42:15 +03:00
|
|
|
return 0;
|
2008-05-03 06:47:26 +03:00
|
|
|
}
|
|
|
|
|
2009-08-15 14:42:15 +03:00
|
|
|
static void libdirac_flush(AVCodecContext *avccontext)
|
2008-05-03 06:47:26 +03:00
|
|
|
{
|
|
|
|
/* 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. */
|
2009-08-15 14:42:15 +03:00
|
|
|
libdirac_decode_close(avccontext);
|
|
|
|
libdirac_decode_init(avccontext);
|
2008-05-03 06:47:26 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-25 23:40:11 +02:00
|
|
|
AVCodec ff_libdirac_decoder = {
|
2011-07-17 13:54:31 +03:00
|
|
|
.name = "libdirac",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.id = CODEC_ID_DIRAC,
|
2011-10-30 15:19:50 +03:00
|
|
|
.priv_data_size = sizeof(DiracDecoderParams),
|
2011-07-17 13:54:31 +03:00
|
|
|
.init = libdirac_decode_init,
|
|
|
|
.close = libdirac_decode_close,
|
|
|
|
.decode = libdirac_decode_frame,
|
|
|
|
.capabilities = CODEC_CAP_DELAY,
|
2008-05-17 13:20:01 +03:00
|
|
|
.flush = libdirac_flush,
|
2008-06-13 00:50:13 +03:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
|
2009-08-15 14:42:15 +03:00
|
|
|
};
|