2008-05-03 05:47:26 +02: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 >
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2008-05-03 05:47:26 +02: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 14:37:08 +01:00
|
|
|
#include "libavutil/imgutils.h"
|
2008-05-03 05:47:26 +02:00
|
|
|
#include "libdirac.h"
|
|
|
|
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <libdirac_decoder/dirac_parser.h>
|
|
|
|
|
|
|
|
/** contains a single frame returned from Dirac */
|
2009-08-15 13:42:15 +02:00
|
|
|
typedef struct FfmpegDiracDecoderParams {
|
2008-05-03 05:47:26 +02:00
|
|
|
/** decoder handle */
|
|
|
|
dirac_decoder_t* p_decoder;
|
|
|
|
|
|
|
|
/** buffer to hold decoded frame */
|
|
|
|
unsigned char* p_out_frame_buf;
|
|
|
|
} FfmpegDiracDecoderParams;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-04-17 23:57:50 +02:00
|
|
|
* returns FFmpeg chroma format
|
2008-05-03 05:47:26 +02:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
|
2009-08-15 13:12:47 +02:00
|
|
|
for (idx = 0; idx < num_formats; ++idx)
|
|
|
|
if (ffmpeg_dirac_pixel_format_map[idx].dirac_pix_fmt == dirac_pix_fmt)
|
2008-05-03 05:47:26 +02:00
|
|
|
return ffmpeg_dirac_pixel_format_map[idx].ff_pix_fmt;
|
|
|
|
return PIX_FMT_NONE;
|
|
|
|
}
|
|
|
|
|
2009-02-22 14:48:55 +01:00
|
|
|
static av_cold int libdirac_decode_init(AVCodecContext *avccontext)
|
2008-05-03 05:47:26 +02:00
|
|
|
{
|
|
|
|
|
2009-08-15 13:42:15 +02:00
|
|
|
FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
|
2008-05-03 05:47:26 +02:00
|
|
|
p_dirac_params->p_decoder = dirac_decoder_init(avccontext->debug);
|
|
|
|
|
|
|
|
if (!p_dirac_params->p_decoder)
|
|
|
|
return -1;
|
|
|
|
|
2009-08-15 13:42:15 +02:00
|
|
|
return 0;
|
2008-05-03 05:47:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int libdirac_decode_frame(AVCodecContext *avccontext,
|
|
|
|
void *data, int *data_size,
|
2009-04-07 17:59:50 +02:00
|
|
|
AVPacket *avpkt)
|
2008-05-03 05:47:26 +02:00
|
|
|
{
|
2009-04-07 17:59:50 +02:00
|
|
|
const uint8_t *buf = avpkt->data;
|
|
|
|
int buf_size = avpkt->size;
|
2008-05-03 05:47:26 +02:00
|
|
|
|
|
|
|
FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
|
|
|
|
AVPicture *picture = data;
|
|
|
|
AVPicture pic;
|
|
|
|
int pict_size;
|
|
|
|
unsigned char *buffer[3];
|
|
|
|
|
|
|
|
*data_size = 0;
|
|
|
|
|
2009-08-15 13:42:15 +02:00
|
|
|
if (buf_size > 0) {
|
2008-05-03 05:47:26 +02:00
|
|
|
/* set data to decode into buffer */
|
2009-08-15 13:42:15 +02:00
|
|
|
dirac_buffer(p_dirac_params->p_decoder, buf, buf + buf_size);
|
|
|
|
if ((buf[4] & 0x08) == 0x08 && (buf[4] & 0x03))
|
2008-12-22 01:01:39 +01:00
|
|
|
avccontext->has_b_frames = 1;
|
|
|
|
}
|
2008-05-03 05:47:26 +02:00
|
|
|
while (1) {
|
|
|
|
/* parse data and process result */
|
2009-08-15 13:42:15 +02:00
|
|
|
DecoderState state = dirac_parse(p_dirac_params->p_decoder);
|
|
|
|
switch (state) {
|
2008-05-03 05:47:26 +02:00
|
|
|
case STATE_BUFFER:
|
|
|
|
return buf_size;
|
|
|
|
|
|
|
|
case STATE_SEQUENCE:
|
|
|
|
{
|
2011-04-17 23:57:50 +02:00
|
|
|
/* tell FFmpeg about sequence details */
|
2009-08-15 13:42:15 +02:00
|
|
|
dirac_sourceparams_t *src_params = &p_dirac_params->p_decoder->src_params;
|
2008-05-03 05:47:26 +02:00
|
|
|
|
2010-09-07 21:15:29 +02:00
|
|
|
if (av_image_check_size(src_params->width, src_params->height,
|
2010-08-06 11:37:04 +02:00
|
|
|
0, avccontext) < 0) {
|
2008-05-03 05:47:26 +02: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;
|
|
|
|
|
|
|
|
avccontext->pix_fmt = GetFfmpegChromaFormat(src_params->chroma);
|
|
|
|
if (avccontext->pix_fmt == PIX_FMT_NONE) {
|
2009-08-15 13:42:15 +02:00
|
|
|
av_log(avccontext, AV_LOG_ERROR,
|
|
|
|
"Dirac chroma format %d not supported currently\n",
|
|
|
|
src_params->chroma);
|
2008-05-03 05:47:26 +02: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 13:02:50 +02:00
|
|
|
if (!p_dirac_params->p_out_frame_buf)
|
2009-08-15 13:42:15 +02:00
|
|
|
p_dirac_params->p_out_frame_buf = av_malloc(pict_size);
|
2008-05-03 05:47:26 +02: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 14:48:55 +01:00
|
|
|
static av_cold int libdirac_decode_close(AVCodecContext *avccontext)
|
2008-05-03 05:47:26 +02:00
|
|
|
{
|
|
|
|
FfmpegDiracDecoderParams *p_dirac_params = avccontext->priv_data;
|
2009-08-15 13:42:15 +02:00
|
|
|
dirac_decoder_close(p_dirac_params->p_decoder);
|
2008-05-03 05:47:26 +02:00
|
|
|
|
|
|
|
av_freep(&p_dirac_params->p_out_frame_buf);
|
|
|
|
|
2009-08-15 13:42:15 +02:00
|
|
|
return 0;
|
2008-05-03 05:47:26 +02:00
|
|
|
}
|
|
|
|
|
2009-08-15 13:42:15 +02:00
|
|
|
static void libdirac_flush(AVCodecContext *avccontext)
|
2008-05-03 05:47:26 +02: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 13:42:15 +02:00
|
|
|
libdirac_decode_close(avccontext);
|
|
|
|
libdirac_decode_init(avccontext);
|
2008-05-03 05:47:26 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-25 22:40:11 +01:00
|
|
|
AVCodec ff_libdirac_decoder = {
|
2008-05-03 05:47:26 +02:00
|
|
|
"libdirac",
|
2010-03-31 01:30:55 +02:00
|
|
|
AVMEDIA_TYPE_VIDEO,
|
2008-05-03 05:47:26 +02:00
|
|
|
CODEC_ID_DIRAC,
|
|
|
|
sizeof(FfmpegDiracDecoderParams),
|
|
|
|
libdirac_decode_init,
|
|
|
|
NULL,
|
|
|
|
libdirac_decode_close,
|
|
|
|
libdirac_decode_frame,
|
|
|
|
CODEC_CAP_DELAY,
|
2008-05-17 12:20:01 +02:00
|
|
|
.flush = libdirac_flush,
|
2008-06-12 23:50:13 +02:00
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),
|
2009-08-15 13:42:15 +02:00
|
|
|
};
|