Use (u)int16_t instead of (unsigned) short
[FFMpeg-mirror/DVCPRO-HD.git] / libavcodec / libschroedingerdec.c
blob5a07899db641b6d835661901c923c7f0d9d119c6
1 /*
2 * Dirac decoder support via Schroedinger libraries
3 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com >
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 /**
23 * @file libschroedingerdec.c
24 * Dirac decoder support via libschroedinger-1.0 libraries. More details about
25 * the Schroedinger project can be found at http://www.diracvideo.org/.
26 * The library implements Dirac Specification Version 2.2.
27 * (http://dirac.sourceforge.net/specification.html).
30 #include "avcodec.h"
31 #include "libdirac_libschro.h"
32 #include "libschroedinger.h"
34 #undef NDEBUG
35 #include <assert.h>
38 #include <schroedinger/schro.h>
39 #include <schroedinger/schrodebug.h>
40 #include <schroedinger/schrovideoformat.h>
42 /** libschroedinger decoder private data */
43 typedef struct FfmpegSchroDecoderParams
45 /** Schroedinger video format */
46 SchroVideoFormat *format;
48 /** Schroedinger frame format */
49 SchroFrameFormat frame_format;
51 /** decoder handle */
52 SchroDecoder* decoder;
54 /** queue storing decoded frames */
55 FfmpegDiracSchroQueue dec_frame_queue;
57 /** end of sequence signalled */
58 int eos_signalled;
60 /** end of sequence pulled */
61 int eos_pulled;
63 /** decoded picture */
64 AVPicture dec_pic;
65 } FfmpegSchroDecoderParams;
67 /**
68 * Returns FFmpeg chroma format.
70 static enum PixelFormat GetFfmpegChromaFormat(SchroChromaFormat schro_pix_fmt)
72 int num_formats = sizeof(ffmpeg_schro_pixel_format_map) /
73 sizeof(ffmpeg_schro_pixel_format_map[0]);
74 int idx;
76 for (idx = 0; idx < num_formats; ++idx) {
77 if (ffmpeg_schro_pixel_format_map[idx].schro_pix_fmt == schro_pix_fmt) {
78 return ffmpeg_schro_pixel_format_map[idx].ff_pix_fmt;
81 return PIX_FMT_NONE;
84 static int libschroedinger_decode_init(AVCodecContext *avccontext)
87 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data ;
88 /* First of all, initialize our supporting libraries. */
89 schro_init();
91 schro_debug_set_level(avccontext->debug);
92 p_schro_params->decoder = schro_decoder_new();
93 schro_decoder_set_skip_ratio(p_schro_params->decoder, 1);
95 if (!p_schro_params->decoder)
96 return -1;
98 /* Initialize the decoded frame queue. */
99 ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
100 return 0 ;
103 static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf,
104 void *priv)
106 av_freep(&priv);
109 static void libschroedinger_decode_frame_free (void *frame)
111 schro_frame_unref(frame);
114 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext)
116 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
117 SchroDecoder *decoder = p_schro_params->decoder;
119 p_schro_params->format = schro_decoder_get_video_format (decoder);
121 /* Tell FFmpeg about sequence details. */
122 if(avcodec_check_dimensions(avccontext, p_schro_params->format->width,
123 p_schro_params->format->height) < 0) {
124 av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n",
125 p_schro_params->format->width, p_schro_params->format->height);
126 avccontext->height = avccontext->width = 0;
127 return;
129 avccontext->height = p_schro_params->format->height;
130 avccontext->width = p_schro_params->format->width;
131 avccontext->pix_fmt =
132 GetFfmpegChromaFormat(p_schro_params->format->chroma_format);
134 if (ff_get_schro_frame_format( p_schro_params->format->chroma_format,
135 &p_schro_params->frame_format) == -1) {
136 av_log (avccontext, AV_LOG_ERROR,
137 "This codec currently only supports planar YUV 4:2:0, 4:2:2 "
138 "and 4:4:4 formats.\n");
139 return;
142 avccontext->time_base.den = p_schro_params->format->frame_rate_numerator;
143 avccontext->time_base.num = p_schro_params->format->frame_rate_denominator;
145 if (p_schro_params->dec_pic.data[0] == NULL)
147 avpicture_alloc(&p_schro_params->dec_pic,
148 avccontext->pix_fmt,
149 avccontext->width,
150 avccontext->height);
154 static int libschroedinger_decode_frame(AVCodecContext *avccontext,
155 void *data, int *data_size,
156 const uint8_t *buf, int buf_size)
159 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
160 SchroDecoder *decoder = p_schro_params->decoder;
161 SchroVideoFormat *format;
162 AVPicture *picture = data;
163 SchroBuffer *enc_buf;
164 SchroFrame* frame;
165 int state;
166 int go = 1;
168 *data_size = 0;
170 if (buf_size>0) {
171 unsigned char *in_buf = av_malloc(buf_size);
172 memcpy (in_buf, buf, buf_size);
173 enc_buf = schro_buffer_new_with_data (in_buf, buf_size);
174 enc_buf->free = libschroedinger_decode_buffer_free;
175 enc_buf->priv = in_buf;
176 /* Push buffer into decoder. */
177 state = schro_decoder_push (decoder, enc_buf);
178 if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT)
179 libschroedinger_handle_first_access_unit(avccontext);
180 } else {
181 if (!p_schro_params->eos_signalled) {
182 state = schro_decoder_push_end_of_stream(decoder);
183 p_schro_params->eos_signalled = 1;
187 format = p_schro_params->format;
189 while (go) {
190 /* Parse data and process result. */
191 state = schro_decoder_wait (decoder);
192 switch (state)
194 case SCHRO_DECODER_FIRST_ACCESS_UNIT:
195 libschroedinger_handle_first_access_unit (avccontext);
196 break;
198 case SCHRO_DECODER_NEED_BITS:
199 /* Need more input data - stop iterating over what we have. */
200 go = 0;
201 break;
203 case SCHRO_DECODER_NEED_FRAME:
204 /* Decoder needs a frame - create one and push it in. */
206 frame = schro_frame_new_and_alloc(NULL,
207 p_schro_params->frame_format,
208 format->width,
209 format->height);
210 schro_decoder_add_output_picture (decoder, frame);
211 break;
213 case SCHRO_DECODER_OK:
214 /* Pull a frame out of the decoder. */
215 frame = schro_decoder_pull (decoder);
217 if (frame) {
218 ff_dirac_schro_queue_push_back(
219 &p_schro_params->dec_frame_queue,
220 frame);
222 break;
223 case SCHRO_DECODER_EOS:
224 go = 0;
225 p_schro_params->eos_pulled = 1;
226 schro_decoder_reset (decoder);
227 break;
229 case SCHRO_DECODER_ERROR:
230 return -1;
231 break;
235 /* Grab next frame to be returned from the top of the queue. */
236 frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue);
238 if (frame != NULL) {
239 memcpy (p_schro_params->dec_pic.data[0],
240 frame->components[0].data,
241 frame->components[0].length);
243 memcpy (p_schro_params->dec_pic.data[1],
244 frame->components[1].data,
245 frame->components[1].length);
247 memcpy (p_schro_params->dec_pic.data[2],
248 frame->components[2].data,
249 frame->components[2].length);
251 /* Fill picture with current buffer data from Schroedinger. */
252 avpicture_fill(picture, p_schro_params->dec_pic.data[0],
253 avccontext->pix_fmt,
254 avccontext->width, avccontext->height);
256 *data_size = sizeof(AVPicture);
258 /* Now free the frame resources. */
259 libschroedinger_decode_frame_free (frame);
261 return buf_size;
265 static int libschroedinger_decode_close(AVCodecContext *avccontext)
267 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
268 /* Free the decoder. */
269 schro_decoder_free (p_schro_params->decoder);
270 av_freep(&p_schro_params->format);
272 avpicture_free (&p_schro_params->dec_pic);
274 /* Free data in the output frame queue. */
275 ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
276 libschroedinger_decode_frame_free);
278 return 0 ;
281 static void libschroedinger_flush (AVCodecContext *avccontext)
283 /* Got a seek request. Free the decoded frames queue and then reset
284 * the decoder */
285 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data;
287 /* Free data in the output frame queue. */
288 ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue,
289 libschroedinger_decode_frame_free);
291 ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue);
292 schro_decoder_reset(p_schro_params->decoder);
293 p_schro_params->eos_pulled = 0;
294 p_schro_params->eos_signalled = 0;
297 AVCodec libschroedinger_decoder = {
298 "libschroedinger",
299 CODEC_TYPE_VIDEO,
300 CODEC_ID_DIRAC,
301 sizeof(FfmpegSchroDecoderParams),
302 libschroedinger_decode_init,
303 NULL,
304 libschroedinger_decode_close,
305 libschroedinger_decode_frame,
306 CODEC_CAP_DELAY,
307 .flush = libschroedinger_flush,
308 .long_name = "libschroedinger Dirac 2.2",