2 * Dirac decoder support via libdirac library
3 * Copyright (c) 2005 BBC, Andrew Kennedy <dirac at rd dot bbc dot co dot uk>
4 * Copyright (c) 2006-2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * @file libavcodec/libdiracdec.c
25 * Dirac decoder support via libdirac library; more details about the Dirac
26 * project can be found at http://dirac.sourceforge.net/.
27 * The libdirac_decoder library implements Dirac specification version 2.2
28 * (http://dirac.sourceforge.net/specification.html).
36 #include <libdirac_decoder/dirac_parser.h>
38 /** contains a single frame returned from Dirac */
39 typedef struct FfmpegDiracDecoderParams
{
41 dirac_decoder_t
* p_decoder
;
43 /** buffer to hold decoded frame */
44 unsigned char* p_out_frame_buf
;
45 } FfmpegDiracDecoderParams
;
49 * returns FFmpeg chroma format
51 static enum PixelFormat
GetFfmpegChromaFormat(dirac_chroma_t dirac_pix_fmt
)
53 int num_formats
= sizeof(ffmpeg_dirac_pixel_format_map
) /
54 sizeof(ffmpeg_dirac_pixel_format_map
[0]);
57 for (idx
= 0; idx
< num_formats
; ++idx
)
58 if (ffmpeg_dirac_pixel_format_map
[idx
].dirac_pix_fmt
== dirac_pix_fmt
)
59 return ffmpeg_dirac_pixel_format_map
[idx
].ff_pix_fmt
;
63 static av_cold
int libdirac_decode_init(AVCodecContext
*avccontext
)
66 FfmpegDiracDecoderParams
*p_dirac_params
= avccontext
->priv_data
;
67 p_dirac_params
->p_decoder
= dirac_decoder_init(avccontext
->debug
);
69 if (!p_dirac_params
->p_decoder
)
75 static int libdirac_decode_frame(AVCodecContext
*avccontext
,
76 void *data
, int *data_size
,
79 const uint8_t *buf
= avpkt
->data
;
80 int buf_size
= avpkt
->size
;
82 FfmpegDiracDecoderParams
*p_dirac_params
= avccontext
->priv_data
;
83 AVPicture
*picture
= data
;
86 unsigned char *buffer
[3];
91 /* set data to decode into buffer */
92 dirac_buffer(p_dirac_params
->p_decoder
, buf
, buf
+ buf_size
);
93 if ((buf
[4] & 0x08) == 0x08 && (buf
[4] & 0x03))
94 avccontext
->has_b_frames
= 1;
97 /* parse data and process result */
98 DecoderState state
= dirac_parse(p_dirac_params
->p_decoder
);
105 /* tell FFmpeg about sequence details */
106 dirac_sourceparams_t
*src_params
= &p_dirac_params
->p_decoder
->src_params
;
108 if (avcodec_check_dimensions(avccontext
, src_params
->width
,
109 src_params
->height
) < 0) {
110 av_log(avccontext
, AV_LOG_ERROR
, "Invalid dimensions (%dx%d)\n",
111 src_params
->width
, src_params
->height
);
112 avccontext
->height
= avccontext
->width
= 0;
116 avccontext
->height
= src_params
->height
;
117 avccontext
->width
= src_params
->width
;
119 avccontext
->pix_fmt
= GetFfmpegChromaFormat(src_params
->chroma
);
120 if (avccontext
->pix_fmt
== PIX_FMT_NONE
) {
121 av_log(avccontext
, AV_LOG_ERROR
,
122 "Dirac chroma format %d not supported currently\n",
127 avccontext
->time_base
.den
= src_params
->frame_rate
.numerator
;
128 avccontext
->time_base
.num
= src_params
->frame_rate
.denominator
;
130 /* calculate output dimensions */
131 avpicture_fill(&pic
, NULL
, avccontext
->pix_fmt
,
132 avccontext
->width
, avccontext
->height
);
134 pict_size
= avpicture_get_size(avccontext
->pix_fmt
,
138 /* allocate output buffer */
139 if (!p_dirac_params
->p_out_frame_buf
)
140 p_dirac_params
->p_out_frame_buf
= av_malloc(pict_size
);
141 buffer
[0] = p_dirac_params
->p_out_frame_buf
;
142 buffer
[1] = p_dirac_params
->p_out_frame_buf
+
143 pic
.linesize
[0] * avccontext
->height
;
144 buffer
[2] = buffer
[1] +
145 pic
.linesize
[1] * src_params
->chroma_height
;
147 /* tell Dirac about output destination */
148 dirac_set_buf(p_dirac_params
->p_decoder
, buffer
, NULL
);
151 case STATE_SEQUENCE_END
:
154 case STATE_PICTURE_AVAIL
:
155 /* fill picture with current buffer data from Dirac */
156 avpicture_fill(picture
, p_dirac_params
->p_out_frame_buf
,
158 avccontext
->width
, avccontext
->height
);
159 *data_size
= sizeof(AVPicture
);
174 static av_cold
int libdirac_decode_close(AVCodecContext
*avccontext
)
176 FfmpegDiracDecoderParams
*p_dirac_params
= avccontext
->priv_data
;
177 dirac_decoder_close(p_dirac_params
->p_decoder
);
179 av_freep(&p_dirac_params
->p_out_frame_buf
);
184 static void libdirac_flush(AVCodecContext
*avccontext
)
186 /* Got a seek request. We will need free memory held in the private
187 * context and free the current Dirac decoder handle and then open
188 * a new decoder handle. */
189 libdirac_decode_close(avccontext
);
190 libdirac_decode_init(avccontext
);
196 AVCodec libdirac_decoder
= {
200 sizeof(FfmpegDiracDecoderParams
),
201 libdirac_decode_init
,
203 libdirac_decode_close
,
204 libdirac_decode_frame
,
206 .flush
= libdirac_flush
,
207 .long_name
= NULL_IF_CONFIG_SMALL("libdirac Dirac 2.2"),