Mercurial > libavcodec.hg
comparison libschroedingerdec.c @ 6738:bdacae101076 libavcodec
Add Dirac support through libschroedinger.
patch by Anuradha Suraparaju, anuradha rd.bbc.co uk
| author | diego |
|---|---|
| date | Sat, 03 May 2008 13:59:45 +0000 |
| parents | |
| children | 7094ae690809 |
comparison
equal
deleted
inserted
replaced
| 6737:dcb3b40c7b71 | 6738:bdacae101076 |
|---|---|
| 1 /* | |
| 2 * Dirac decoder support via Schroedinger libraries | |
| 3 * Copyright (c) 2008 BBC, Anuradha Suraparaju <asuraparaju at gmail dot com > | |
| 4 * | |
| 5 * This file is part of FFmpeg. | |
| 6 * | |
| 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. | |
| 11 * | |
| 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. | |
| 16 * | |
| 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 | |
| 20 */ | |
| 21 | |
| 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). | |
| 28 */ | |
| 29 | |
| 30 #include "avcodec.h" | |
| 31 #include "libdirac_libschro.h" | |
| 32 #include "libschroedinger.h" | |
| 33 | |
| 34 #undef NDEBUG | |
| 35 #include <assert.h> | |
| 36 | |
| 37 | |
| 38 #include <schroedinger/schro.h> | |
| 39 #include <schroedinger/schrodebug.h> | |
| 40 #include <schroedinger/schrovideoformat.h> | |
| 41 | |
| 42 /** libschroedinger decoder private data */ | |
| 43 typedef struct FfmpegSchroDecoderParams | |
| 44 { | |
| 45 /** Schroedinger video format */ | |
| 46 SchroVideoFormat *format; | |
| 47 | |
| 48 /** Schroedinger frame format */ | |
| 49 SchroFrameFormat frame_format; | |
| 50 | |
| 51 /** decoder handle */ | |
| 52 SchroDecoder* decoder; | |
| 53 | |
| 54 /** queue storing decoded frames */ | |
| 55 FfmpegDiracSchroQueue dec_frame_queue; | |
| 56 | |
| 57 /** end of sequence signalled */ | |
| 58 int eos_signalled; | |
| 59 | |
| 60 /** end of sequence pulled */ | |
| 61 int eos_pulled; | |
| 62 | |
| 63 /** decoded picture */ | |
| 64 AVPicture dec_pic; | |
| 65 } FfmpegSchroDecoderParams; | |
| 66 | |
| 67 /** | |
| 68 * Returns FFmpeg chroma format. | |
| 69 */ | |
| 70 static enum PixelFormat GetFfmpegChromaFormat(SchroChromaFormat schro_pix_fmt) | |
| 71 { | |
| 72 int num_formats = sizeof(ffmpeg_schro_pixel_format_map) / | |
| 73 sizeof(ffmpeg_schro_pixel_format_map[0]); | |
| 74 int idx; | |
| 75 | |
| 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; | |
| 79 } | |
| 80 } | |
| 81 return PIX_FMT_NONE; | |
| 82 } | |
| 83 | |
| 84 /*-------------------------DECODER------------------------------------------*/ | |
| 85 | |
| 86 static int libschroedinger_decode_init(AVCodecContext *avccontext) | |
| 87 { | |
| 88 | |
| 89 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data ; | |
| 90 /* First of all, initialize our supporting libraries. */ | |
| 91 schro_init(); | |
| 92 | |
| 93 schro_debug_set_level(avccontext->debug); | |
| 94 p_schro_params->decoder = schro_decoder_new(); | |
| 95 schro_decoder_set_skip_ratio(p_schro_params->decoder, 1); | |
| 96 | |
| 97 if (!p_schro_params->decoder) | |
| 98 return -1; | |
| 99 | |
| 100 /* Initialize the decoded frame queue. */ | |
| 101 ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue); | |
| 102 return 0 ; | |
| 103 } | |
| 104 | |
| 105 static void libschroedinger_decode_buffer_free (SchroBuffer *schro_buf, | |
| 106 void *priv) | |
| 107 { | |
| 108 av_freep(&priv); | |
| 109 } | |
| 110 | |
| 111 static void libschroedinger_decode_frame_free (void *frame) | |
| 112 { | |
| 113 schro_frame_unref(frame); | |
| 114 } | |
| 115 | |
| 116 static void libschroedinger_handle_first_access_unit(AVCodecContext *avccontext) | |
| 117 { | |
| 118 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; | |
| 119 SchroDecoder *decoder = p_schro_params->decoder; | |
| 120 | |
| 121 p_schro_params->format = schro_decoder_get_video_format (decoder); | |
| 122 | |
| 123 /* Tell FFmpeg about sequence details. */ | |
| 124 if(avcodec_check_dimensions(avccontext, p_schro_params->format->width, | |
| 125 p_schro_params->format->height) < 0) { | |
| 126 av_log(avccontext, AV_LOG_ERROR, "invalid dimensions (%dx%d)\n", | |
| 127 p_schro_params->format->width, p_schro_params->format->height); | |
| 128 avccontext->height = avccontext->width = 0; | |
| 129 return -1; | |
| 130 } | |
| 131 avccontext->height = p_schro_params->format->height; | |
| 132 avccontext->width = p_schro_params->format->width; | |
| 133 avccontext->pix_fmt = | |
| 134 GetFfmpegChromaFormat(p_schro_params->format->chroma_format); | |
| 135 | |
| 136 if (ff_get_schro_frame_format( p_schro_params->format->chroma_format, | |
| 137 &p_schro_params->frame_format) == -1) { | |
| 138 av_log (avccontext, AV_LOG_ERROR, | |
| 139 "This codec currently only supports planar YUV 4:2:0, 4:2:2 " | |
| 140 "and 4:4:4 formats.\n"); | |
| 141 return -1; | |
| 142 } | |
| 143 | |
| 144 avccontext->time_base.den = p_schro_params->format->frame_rate_numerator; | |
| 145 avccontext->time_base.num = p_schro_params->format->frame_rate_denominator; | |
| 146 | |
| 147 if (p_schro_params->dec_pic.data[0] == NULL) | |
| 148 { | |
| 149 avpicture_alloc(&p_schro_params->dec_pic, | |
| 150 avccontext->pix_fmt, | |
| 151 avccontext->width, | |
| 152 avccontext->height); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 static int libschroedinger_decode_frame(AVCodecContext *avccontext, | |
| 157 void *data, int *data_size, | |
| 158 const uint8_t *buf, int buf_size) | |
| 159 { | |
| 160 | |
| 161 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; | |
| 162 SchroDecoder *decoder = p_schro_params->decoder; | |
| 163 SchroVideoFormat *format; | |
| 164 AVPicture *picture = data; | |
| 165 SchroBuffer *enc_buf; | |
| 166 SchroFrame* frame; | |
| 167 int state; | |
| 168 int go = 1; | |
| 169 | |
| 170 *data_size = 0; | |
| 171 | |
| 172 if (buf_size>0) { | |
| 173 unsigned char *in_buf = av_malloc(buf_size); | |
| 174 memcpy (in_buf, buf, buf_size); | |
| 175 enc_buf = schro_buffer_new_with_data (in_buf, buf_size); | |
| 176 enc_buf->free = libschroedinger_decode_buffer_free; | |
| 177 enc_buf->priv = in_buf; | |
| 178 /* Push buffer into decoder. */ | |
| 179 state = schro_decoder_push (decoder, enc_buf); | |
| 180 if (state == SCHRO_DECODER_FIRST_ACCESS_UNIT) | |
| 181 libschroedinger_handle_first_access_unit(avccontext); | |
| 182 } else { | |
| 183 if (!p_schro_params->eos_signalled) { | |
| 184 state = schro_decoder_push_end_of_stream(decoder); | |
| 185 p_schro_params->eos_signalled = 1; | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 format = p_schro_params->format; | |
| 190 | |
| 191 while (go) { | |
| 192 /* Parse data and process result. */ | |
| 193 state = schro_decoder_wait (decoder); | |
| 194 switch (state) | |
| 195 { | |
| 196 case SCHRO_DECODER_FIRST_ACCESS_UNIT: | |
| 197 libschroedinger_handle_first_access_unit (avccontext); | |
| 198 break; | |
| 199 | |
| 200 case SCHRO_DECODER_NEED_BITS: | |
| 201 /* Need more input data - stop iterating over what we have. */ | |
| 202 go = 0; | |
| 203 break; | |
| 204 | |
| 205 case SCHRO_DECODER_NEED_FRAME: | |
| 206 /* Decoder needs a frame - create one and push it in. */ | |
| 207 | |
| 208 frame = schro_frame_new_and_alloc(NULL, | |
| 209 p_schro_params->frame_format, | |
| 210 format->width, | |
| 211 format->height); | |
| 212 schro_decoder_add_output_picture (decoder, frame); | |
| 213 break; | |
| 214 | |
| 215 case SCHRO_DECODER_OK: | |
| 216 /* Pull a frame out of the decoder. */ | |
| 217 frame = schro_decoder_pull (decoder); | |
| 218 | |
| 219 if (frame) { | |
| 220 ff_dirac_schro_queue_push_back( | |
| 221 &p_schro_params->dec_frame_queue, | |
| 222 frame); | |
| 223 } | |
| 224 break; | |
| 225 case SCHRO_DECODER_EOS: | |
| 226 go = 0; | |
| 227 p_schro_params->eos_pulled = 1; | |
| 228 schro_decoder_reset (decoder); | |
| 229 break; | |
| 230 | |
| 231 case SCHRO_DECODER_ERROR: | |
| 232 return -1; | |
| 233 break; | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 /* Grab next frame to be returned from the top of the queue. */ | |
| 238 frame = ff_dirac_schro_queue_pop(&p_schro_params->dec_frame_queue); | |
| 239 | |
| 240 if (frame != NULL) { | |
| 241 memcpy (p_schro_params->dec_pic.data[0], | |
| 242 frame->components[0].data, | |
| 243 frame->components[0].length); | |
| 244 | |
| 245 memcpy (p_schro_params->dec_pic.data[1], | |
| 246 frame->components[1].data, | |
| 247 frame->components[1].length); | |
| 248 | |
| 249 memcpy (p_schro_params->dec_pic.data[2], | |
| 250 frame->components[2].data, | |
| 251 frame->components[2].length); | |
| 252 | |
| 253 /* Fill picture with current buffer data from Schroedinger. */ | |
| 254 avpicture_fill(picture, p_schro_params->dec_pic.data[0], | |
| 255 avccontext->pix_fmt, | |
| 256 avccontext->width, avccontext->height); | |
| 257 | |
| 258 *data_size = sizeof(AVPicture); | |
| 259 | |
| 260 /* Now free the frame resources. */ | |
| 261 libschroedinger_decode_frame_free (frame); | |
| 262 } | |
| 263 return buf_size; | |
| 264 } | |
| 265 | |
| 266 | |
| 267 static int libschroedinger_decode_close(AVCodecContext *avccontext) | |
| 268 { | |
| 269 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; | |
| 270 /* Free the decoder. */ | |
| 271 schro_decoder_free (p_schro_params->decoder); | |
| 272 av_freep(&p_schro_params->format); | |
| 273 | |
| 274 avpicture_free (&p_schro_params->dec_pic); | |
| 275 | |
| 276 /* Free data in the output frame queue. */ | |
| 277 ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue, | |
| 278 libschroedinger_decode_frame_free); | |
| 279 | |
| 280 return 0 ; | |
| 281 } | |
| 282 | |
| 283 static void libschroedinger_flush (AVCodecContext *avccontext) | |
| 284 { | |
| 285 /* Got a seek request. Free the decoded frames queue and then reset | |
| 286 * the decoder */ | |
| 287 FfmpegSchroDecoderParams *p_schro_params = avccontext->priv_data; | |
| 288 | |
| 289 /* Free data in the output frame queue. */ | |
| 290 ff_dirac_schro_queue_free (&p_schro_params->dec_frame_queue, | |
| 291 libschroedinger_decode_frame_free); | |
| 292 | |
| 293 ff_dirac_schro_queue_init (&p_schro_params->dec_frame_queue); | |
| 294 schro_decoder_reset(p_schro_params->decoder); | |
| 295 p_schro_params->eos_pulled = 0; | |
| 296 p_schro_params->eos_signalled = 0; | |
| 297 } | |
| 298 | |
| 299 AVCodec libschroedinger_decoder = { | |
| 300 "libschroedinger", | |
| 301 CODEC_TYPE_VIDEO, | |
| 302 CODEC_ID_DIRAC, | |
| 303 sizeof(FfmpegSchroDecoderParams), | |
| 304 libschroedinger_decode_init, | |
| 305 NULL, | |
| 306 libschroedinger_decode_close, | |
| 307 libschroedinger_decode_frame, | |
| 308 CODEC_CAP_DELAY, | |
| 309 .flush = libschroedinger_flush | |
| 310 }; |
